Set SQLite connection properties in c3p0 connection pool

安稳与你 提交于 2019-12-04 04:02:18

问题


To specify SQLite connection properties there is org.sqlite.SQLiteConfig and it goes something like this:

    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:

        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?


回答1:


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.

Please see c3p0's docs, "Using the DataSources factory class", and the API docs for the DataSources factory class.

See also the javadocs for SQLite JDBC, which I downloaded from here to answer this question.



来源:https://stackoverflow.com/questions/22303796/set-sqlite-connection-properties-in-c3p0-connection-pool

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!