Java Vertx JDBC sqlite:如何设置Pragma Syncronous =正常

发布于 2025-02-06 05:24:35 字数 1168 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2025-02-13 05:24:36

因此,这些Arent确定的答案,但是我尝试了其他一些选择,并希望在这里概述它们。

例如,VERTX可以在没有池的情况下实例化SQLCLCLIENT:

JsonObject config = new JsonObject()
    .put("url", "jdbc:sqlite:"+databaseFile)
    .put("driver_class", "org.sqlite.jdbcDriver")
    .put("max_pool_size", 1);

Vertx vertx = Vertx.vertx();
SQLClient client = JDBCClient.create(vertx, config);

尽管这仍然使用连接池,因此我必须进行相同的调整以在池中设置单个连接,以便Pragma粘贴。

还有一个来自SQLite库中的SQLiteConfig类,但是我不知道如何将其连接到Vertx JDBC包装器中

org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.setSynchronous(SynchronousMode.NORMAL);

是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:

JsonObject config = new JsonObject()
    .put("url", "jdbc:sqlite:"+databaseFile)
    .put("driver_class", "org.sqlite.jdbcDriver")
    .put("max_pool_size", 1);

Vertx vertx = Vertx.vertx();
SQLClient client = JDBCClient.create(vertx, config);

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 wrappers

org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.setSynchronous(SynchronousMode.NORMAL);

is 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文