简体   繁体   English

在c3p0连接池中设置SQLite连接属性

[英]Set SQLite connection properties in c3p0 connection pool

To specify SQLite connection properties there is org.sqlite.SQLiteConfig and it goes something like this: 要指定SQLite连接属性,请使用org.sqlite.SQLiteConfig,它是这样的:

    org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
    config.setReadOnly(true);
    config.setPageSize(4096); //in bytes
    config.setCacheSize(2000); //number of pages
    config.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
    config.setJournalMode(SQLiteConfig.JournalMode.OFF);
    SQLiteConnectionPoolDataSource dataSource = new SQLiteConnectionPoolDataSource();

Creating a connection pool with c3p0 goes something like this: 使用c3p0创建连接池的方法如下:

        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("org.sqlite.JDBC");
        cpds.setJdbcUrl("jdbc:sqlite:/foo/bar");
        cpds.setMaxPoolSize(10);

Question: how do I create a DataSource that combines the two, letting me set things like the connection pool's max-pool-size and sqlite's synchronous mode? 问题:如何创建一个结合了两者的DataSource,让我设置连接池的max-pool-size和sqlite的同步模式?

Try 尝试

//put the imports where they really go, obviously...
import javax.sql.*;
import org.sqlite.*;
import com.mchange.v2.c3p0.*;

// configure SQLite
SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.setReadOnly(true);
config.setPageSize(4096); //in bytes
config.setCacheSize(2000); //number of pages
config.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
config.setJournalMode(SQLiteConfig.JournalMode.OFF);

// get an unpooled SQLite DataSource with the desired configuration
SQLiteDataSource unpooled = new SQLiteDataSource( config );

// get a pooled c3p0 DataSource that wraps the unpooled SQLite DataSource
DataSource pooled = DataSources.pooledDataSource( unpooled );

The DataSource pooled will now be a c3p0 PooledDataSource that wraps an SQLite unpooled DataSource which has been configured as you wish. 现在pooled的DataSource将是一个c3p0 PooledDataSource,它包装了一个已根据需要配置的SQLite非池化数据源。

Please see c3p0's docs, "Using the DataSources factory class" , and the API docs for the DataSources factory class. 请参阅C3P0的文档, “使用数据源工厂类” ,并为API文档数据源工厂类。

See also the javadocs for SQLite JDBC, which I downloaded from here to answer this question. 另请参阅SQLite JDBC的javadoc,我从这里下载以回答这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM