gpt4 book ai didi

java - 获取 SQLite 只读 C3P0 ComboPooledDataSource

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:56 32 4
gpt4 key购买 nike

我需要一个 SQLite 只读 C3P0 ComboPooledDataSource。我在这里找到的这段代码 ( Set SQLite connection properties in c3p0 connection pool ) 创建了一个 SQLite 只读数据源:

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

它工作正常。但是我尝试调整的方法返回一个 ComboPooledDataSource。我怎样才能得到一个?

最佳答案

正如您所说,C3P0只读创建连接后更新

Occasionally it is useful to override the default values of standard Connection properties such as transactionIsolation, holdability, or readOnly. c3p0 provides a "hook" interface that you can implement, which gives you the opportunity to modify or track Connections just after they are checked out from the database, immediately just prior to being handed to clients on checkout, just prior to being returned to the pool on check-in, and just prior to final destruction by the pool. The Connections handed to ConnectionCustomizers are raw, physical Connections, with all vendor-specific API accessible. See the API docs for ConnectionCustomizer.

我建议,做一个类似于 Spring 框架的转换,从 C3P0 到 HikariCP连接池

HikariCP 允许您设置只读属性:

readOnly This property controls whether Connections obtained from the pool are in read-only mode by default. Note some databases do not support the concept of read-only mode, while others provide query optimizations when the Connection is set to read-only. Whether you need this property or not will depend largely on your application and database. Default: false

关于java - 获取 SQLite 只读 C3P0 ComboPooledDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57289230/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com