gpt4 book ai didi

sqlite - ScalikeJDBC + SQlite : Cannot change read-only flag after establishing a connection

转载 作者:行者123 更新时间:2023-12-03 03:54:01 67 4
gpt4 key购买 nike

尝试使用 ScalikeJDBC 和 SQLite。根据提供的示例编写一个简单的代码:

import scalikejdbc._, SQLInterpolation._

object Test extends App {
Class.forName("org.sqlite.JDBC")
ConnectionPool.singleton("jdbc:sqlite:test.db", null, null)

implicit val session = AutoSession

println(sql"""SELECT * FROM kv WHERE key == 'seq' LIMIT 1""".map(identity).single().apply()))
}

它失败并出现异常:

Exception in thread "main" java.sql.SQLException: Cannot change read-only flag after establishing a connection. Use SQLiteConfig#setReadOnly and QLiteConfig.createConnection().
at org.sqlite.SQLiteConnection.setReadOnly(SQLiteConnection.java:447)
at org.apache.commons.dbcp.DelegatingConnection.setReadOnly(DelegatingConnection.java:377)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setReadOnly(PoolingDataSource.java:338)
at scalikejdbc.DBConnection$class.readOnlySession(DB.scala:138)
at scalikejdbc.DB.readOnlySession(DB.scala:498)
...

我已经尝试了 scalikejdbc 1.7 和 2.0,错误仍然存​​在。作为 sqlite 驱动程序,我使用 "org.xerial"% "sqlite-jdbc"% "3.7.+"

我该如何修复该错误?

最佳答案

以下将创建两个单独的连接,一个用于只读操作,另一个用于写入。

ConnectionPool.add("mydb", s"jdbc:sqlite:${db.getAbsolutePath}", "", "")
ConnectionPool.add(
"mydb_ro", {
val conf = new SQLiteConfig()
conf.setReadOnly(true)
val source = new SQLiteDataSource(conf)
source.setUrl(s"jdbc:sqlite:${db.getAbsolutePath}")
new DataSourceConnectionPool(source)
}
)

关于sqlite - ScalikeJDBC + SQlite : Cannot change read-only flag after establishing a connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390278/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com