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

时间:2014-03-10 15:04:06

标签: java sqlite c3p0

要指定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();

使用c3p0创建连接池的方法如下:

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

问题:如何创建一个结合了两者的DataSource,让我设置连接池的max-pool-size和sqlite的同步模式?

1 个答案:

答案 0 :(得分:3)

尝试

//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 );

DataSource pooled现在将成为一个c3p0 PooledDataSource,它包装了一个已根据需要配置的SQLite非池化数据源。

请参阅c3p0的文档,"Using the DataSources factory class"以及DataSources工厂类的API文档。

另请参阅我从here下载的SQLite JDBC的javadocs来回答这个问题。