Created
June 8, 2017 04:12
-
-
Save bertmaher/6bfadd9543a43b2d5c19cce93c7a2326 to your computer and use it in GitHub Desktop.
Test case demonstrating race in sqlite-jdbc 3.16.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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