문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top