在c3p0连接池中设置SQLite连接属性 [英] Set SQLite connection properties in c3p0 connection pool

查看:253
本文介绍了在c3p0连接池中设置SQLite连接属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要指定SQLite连接属性,请使用org.sqlite.SQLiteConfig,它的内容如下:

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

使用c3p0创建连接池的过程如下:

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

问题:如何创建将两者结合在一起的数据源,让我设置诸如连接池max-pool-size和sqlite的同步模式?

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?

推荐答案

尝试

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

数据源池化现在将是c3p0

The DataSource pooled will now be a c3p0 PooledDataSource that wraps an SQLite unpooled DataSource which has been configured as you wish.

请参阅c3p0的文档,使用DataSources工厂类 ,以及数据源工厂类。

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

另请参见Javadocs for SQLite JDBC ,我是从此处下载的,以回答此问题。

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

这篇关于在c3p0连接池中设置SQLite连接属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆