Java Vertx JDBC sqlite:如何设置Pragma Syncronous =正常
VERTX概述这是连接到数据库的正常方法
String databaseFile = "sqlite.db";
JDBCPool pool = JDBCPool.pool(
this.context.getVertx(),
new JDBCConnectOptions()
.setJdbcUrl("jdbc:sqlite:".concat(databaseFile)),
new PoolOptions()
.setMaxSize(1)
.setConnectionTimeout(CONNECTION_TIMEOUT)
);
WAL PRAGMA(pragma Journal_model = wal
)设置为数据库本身,因此我无需在应用程序启动时担心它。但是,同步
pragma 是每个连接设置的设置,因此我需要在应用程序启动时设置该设置。当前看起来像这样:
// await this future
pool
.preparedQuery("PRAGMA synchronous=NORMAL")
.execute()
我可以在数据库连接上设置的同步Pragma ins 上确认。
pool
.preparedQuery("PRAGMA synchronous")
.execute()
.map(rows -> {
for (Row row : rows) {
System.out.println("pragma synchronous is " + row.getInteger("synchronous"))
}
})
而且由于我在池中强制执行一个连接,因此该应该很好。但是,我忍不住觉得有更好的方法可以做到这一点。
附带说明,我选择了一个连接,因为SQLite本质上是同步的,只有一次写入数据库。在单个应用程序中创建写入争论听起来有害而不是有用,我设计的应用程序在单个过程中的同时写入尽可能少,尽管过程间并发是真实的。
Vertx outlines that this is the normal way to connect to a database here https://vertx.io/docs/vertx-jdbc-client/java/ :
String databaseFile = "sqlite.db";
JDBCPool pool = JDBCPool.pool(
this.context.getVertx(),
new JDBCConnectOptions()
.setJdbcUrl("jdbc:sqlite:".concat(databaseFile)),
new PoolOptions()
.setMaxSize(1)
.setConnectionTimeout(CONNECTION_TIMEOUT)
);
This application I am writing has interprocess communication, so I want to use WAL mode, and synchronous=NORMAL to avoid heavy disk usage. The WAL pragma (PRAGMA journal_model=WAL
) is set to the database itself, so I dont need to worry about it on application startup. However, the synchronous
pragma is set per connection, so I need to set that when the appplication starts. Currently that looks like this:
// await this future
pool
.preparedQuery("PRAGMA synchronous=NORMAL")
.execute()
I can confirm that later on the synchronous pragma is set on the database connection.
pool
.preparedQuery("PRAGMA synchronous")
.execute()
.map(rows -> {
for (Row row : rows) {
System.out.println("pragma synchronous is " + row.getInteger("synchronous"))
}
})
and since I enforce a single connection in the pool, this should be fine. However I cant help but feel that there is a better way of doing this.
As a side note, I chose a single connection because sqlite is synchronous in nature, there is only ever one write happening at a time to the database. Creating write contention within a single application sounds detrimental rather than helpful, and I have designed my application to have as little concurrent writes within a single process as possible, though inter-process concurrency is real.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,这些Arent确定的答案,但是我尝试了其他一些选择,并希望在这里概述它们。
例如,VERTX可以在没有池的情况下实例化SQLCLCLIENT:
尽管这仍然使用连接池,因此我必须进行相同的调整以在池中设置单个连接,以便Pragma粘贴。
还有一个来自SQLite库中的
SQLiteConfig
类,但是我不知道如何将其连接到Vertx JDBC包装器中是VERTX所需的池?我确实尝试直接运行SQLite JDBC驱动程序,而无需无VERTX包装器。但是,这遇到了各种
sqlite_busy
异常。So these arent definitive answers, but I have tried a few other options, and want to outline them here.
For instance, vertx can instantiate a SQLClient without a pool:
though this still uses a connection pool, so I have to make the same adjustments to set a single connection in the pool, so that the pragma sticks.
There is also a
SQLiteConfig
class from the sqlite library, but I have no idea how to connect that into the vertx jdbc wrappersis a pool required with vertx? I did try running the sqlite jdbc driver directly, without a vertx wrapper. But this ran into all kinds of
SQLITE_BUSY
exceptions.