Skip to content

Instantly share code, notes, and snippets.

@bertmaher
Created June 8, 2017 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertmaher/6bfadd9543a43b2d5c19cce93c7a2326 to your computer and use it in GitHub Desktop.
Save bertmaher/6bfadd9543a43b2d5c19cce93c7a2326 to your computer and use it in GitHub Desktop.
Test case demonstrating race in sqlite-jdbc 3.16.1
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TestSqlite {
public static void main(String[] args) throws Throwable {
Object lock = new Object();
ExecutorService pool = Executors.newFixedThreadPool(32);
for (int i = 0; i < 32; i++) {
final String connStr = "jdbc:sqlite:sample-" + i + ".db";
final int sleepMillis = i;
pool.execute(() -> {
try {
Thread.sleep(sleepMillis * 10);
} catch (InterruptedException e) {
}
try {
// Uncomment the synchronized block and everything works.
//synchronized (TestSqlite.class) {
Connection conn = DriverManager.getConnection(connStr);
//}
System.out.println("Connected!");
} catch (SQLException e) {
e.printStackTrace();
}
});
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment