java - SQLite JDBC PRAGMA 设置

标签 java sqlite jdbc connection-pooling pragma

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

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

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

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

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

    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;
}

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

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

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

有什么想法吗?

最佳答案

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

要考虑的一个选项是使用利用 ConnectionPoolDataSource 接口(interface)的 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 接口(interface)的实现,因此理论上,您应该能够使用 SQLiteConnectionPoolDataSource与 DBCP 。

关于java - SQLite JDBC PRAGMA 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958382/

相关文章:

java - J2me中读写数据

linux - 在 ubuntu 上安装 sqlite3 无需 sudo apt-get install

javascript - 使用cordova在sqlite中创建数据库

java - jsp 中出现 NullPointerException,但控制台中没有出现 NullPointerException

java - 无法使用 JDBC 连接到 Oracle 11g - 指定的 oracle URL 无效

java - Java常量如何变化?

java - 我可以测试是否存在对 java 中对象的另一个引用吗?

java - Eclipse 市场无法确认要安装的选定功能

c++ - 何时使用 sqlite3_blob_write/read?

java - 从 java 调用存储过程时出错 - 无效的 JDBC 数据类型 -10