Open a Sqlite connection with an exclusive lock in

2019-02-23 04:33发布

问题:

I wish to have read only and write connections for sqlite, when I open a write connection, I wish it to have an exclusive lock. This looks like it should work

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

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

but unfortunately I get an exception

Exception in thread "main" java.sql.BatchUpdateException: batch entry 0: query returns results

I have also tried setting the properties directly, rather than using the Sqlite jdbc SQLiteConfig class

val prop = new Properties();
prop.setProperty("locking_mode", "EXCLUSIVE");

Any suggestions?

回答1:

The only way that I was able to personally make it work is by executing a query on the DB itself like this :

conn.createStatement().execute("PRAGMA locking_mode = EXCLUSIVE");