使用SQlite JDBC库在Java/Scala中使用独占锁打开Sqlite连接

J P*_*lar 5 sqlite connection locking

我希望只读和写sqlite的连接,当我打开一个写连接时,我希望它有一个独占锁.这看起来应该有效

val config = new SQLiteConfig();
config.setLockingMode(org.sqlite.SQLiteConfig.LockingMode.EXCLUSIVE)

val connection = DriverManager.getConnection("jdbc:sqlite:" + this.getPath() +"\\" + this.dbName, config.toProperties)
Run Code Online (Sandbox Code Playgroud)

但不幸的是我得到了一个例外

Exception in thread "main" java.sql.BatchUpdateException: batch entry 0: query returns results
Run Code Online (Sandbox Code Playgroud)

我也尝试过直接设置属性,而不是使用Sqlite jdbc SQLiteConfig类

val prop = new Properties();
prop.setProperty("locking_mode", "EXCLUSIVE");
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 3

我个人能够使其工作的唯一方法是对数据库本身执行查询,如下所示:

conn.createStatement().execute("PRAGMA locking_mode = EXCLUSIVE");
Run Code Online (Sandbox Code Playgroud)