【问题标题】:SQLite JDBC PRAGMA settingSQLite JDBC PRAGMA 设置
【发布时间】:2012-04-15 01:09:16
【问题描述】:

我正在尝试设置 PRAGMA foreign_key = ON;在 sqlite 数据库中。我使用用于 sqlite 的 jdbc 驱动程序在 java 中编写了一些软件,这个:http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC

我还使用连接池来加快对数据库的查询。我正在使用这个库: http://commons.apache.org/dbcp/.

到目前为止,一切都很好。现在,我需要设置 PRAGMA 设置,具体为 PRAGMA foreign_key = ON;在创建表之前,因为我需要确定 db 中某些列之间的一致性。

当我创建数据库时,它会自动设置为关闭。所以我必须打开它才能使用它。

但是我不知道怎么做,我准备poolable数据源的方式是这样的:

    public static DataSource getDataSource(String connectURI) {
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory =
    new DriverManagerConnectionFactory(connectURI, null);
PoolableConnectionFactory poolableConnectionFactory =
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
DataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
}

但我不知道如何正确设置该 pragma,我发现这是可能的:

SQLiteConfig config = new SQLiteConfig();
config.enforceForeignKeys(true);

但我不知道如何将它与可池化的棘手设置结合使用...

有什么想法吗?

【问题讨论】:

标签: java sqlite jdbc connection-pooling pragma


【解决方案1】:

不幸的是,这更像是一个 DBCP 问题而不是 SQLite。我同意,DBCP 中必须有一个地方来设置/更新给定数据源的配置。我希望在 PoolingDataSource 类中看到它,但它当然不存在。

要考虑的一个选项是使用利用 ConnectionPoolDataSource 接口的 jdbc 池库。如果是这样,您可以使用SQLiteConnectionPoolDataSource 设置连接池,如下所示:

  //Set config
    org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
    config.enforceForeignKeys(true);


  //Create JDBC Datasource
    SQLiteConnectionPoolDataSource dataSource = new SQLiteConnectionPoolDataSource();
    dataSource.setUrl("jdbc:sqlite:" + db.toString().replace("\\", "/"));
    dataSource.setConfig(config);

请注意,DBCP 有一个扩展名为DriverAdapterCPDS。它是 ConnectionPoolDataSource 接口的一个实现,所以理论上,您应该可以将 SQLiteConnectionPoolDataSource 与 DBCP 一起使用。

【讨论】:

猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 2011-05-30
  • 2011-05-30
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2014-08-08
相关资源
最近更新 更多