本文整理汇总了Java中com.mchange.v2.c3p0.ComboPooledDataSource.setMaxStatementsPerConnection方法的典型用法代码示例。如果您正苦于以下问题:Java ComboPooledDataSource.setMaxStatementsPerConnection方法的具体用法?Java ComboPooledDataSource.setMaxStatementsPerConnection怎么用?Java ComboPooledDataSource.setMaxStatementsPerConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mchange.v2.c3p0.ComboPooledDataSource
的用法示例。
在下文中一共展示了ComboPooledDataSource.setMaxStatementsPerConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Method load properties and get connection pool to database.
* Use c3p0 library.
*/
public void connect() {
pool = new ComboPooledDataSource();
try {
pool.setDriverClass(dbPROP.getProperty("driver"));
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
pool.setJdbcUrl(dbPROP.getProperty("url"));
pool.setUser(dbPROP.getProperty("username"));
pool.setPassword(dbPROP.getProperty("password"));
pool.setMinPoolSize(2);
pool.setAcquireIncrement(1);
pool.setMaxPoolSize(5);
pool.setMaxStatements(50);
pool.setMaxStatementsPerConnection(10);
}
示例2: datasource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Bean(name = "NextRTCDataSource", destroyMethod = "close")
public ComboPooledDataSource datasource() throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setMinPoolSize(1);
ds.setMaxPoolSize(10);
ds.setCheckoutTimeout(30 * 60 * 100);
ds.setMaxIdleTime(30 * 60); // 30 minutes
ds.setMaxStatements(10);
ds.setMaxStatementsPerConnection(10);
ds.setAutoCommitOnClose(true);
ds.setDriverClass(environment.getRequiredProperty("nextrtc.db.driverClassName"));
ds.setJdbcUrl(environment.getRequiredProperty("nextrtc.db.url"));
ds.setUser(environment.getRequiredProperty("nextrtc.db.username"));
ds.setPassword(environment.getRequiredProperty("nextrtc.db.password"));
return ds;
}
示例3: getConnection
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
public synchronized Connection getConnection(Configuration conf) throws SQLException {
DriverConfig config = getDriverConfigfromConf(conf);
if (!dataSourceMap.containsKey(config)) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(config.driverClass);
} catch (PropertyVetoException e) {
throw new IllegalArgumentException("Unable to set driver class:" + config.driverClass, e);
}
cpds.setJdbcUrl(config.jdbcURI);
cpds.setProperties(config.properties);
cpds.setMaxPoolSize(Integer.parseInt(config.getProperty(JDBC_POOL_MAX_SIZE.getPoolProperty())));
cpds.setMaxIdleTime(Integer.parseInt(config.getProperty(JDBC_POOL_IDLE_TIME.getPoolProperty())));
cpds.setMaxIdleTimeExcessConnections(Integer.parseInt(config.getProperty(JDBC_MAX_IDLE_TIME_EXCESS_CONNECTIONS
.getPoolProperty())));
cpds.setMaxStatementsPerConnection(Integer.parseInt(config.getProperty(JDBC_MAX_STATEMENTS_PER_CONNECTION
.getPoolProperty())));
cpds.setCheckoutTimeout(Integer.parseInt(config.getProperty(JDBC_GET_CONNECTION_TIMEOUT.getPoolProperty())));
dataSourceMap.put(config, cpds);
log.info("Created new datasource for config: {}", config);
}
return dataSourceMap.get(config).getConnection();
}
示例4: DAOFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DAOFactory(String driverClassName, String jdbcURL, String userName, String userPassword, int initPoolSize,
int MinPoolSize, int MaxPoolSize, int maxIdleTime, int idleConnectionTestPeriod, String testQuerySQL) {
ds = new ComboPooledDataSource();
// 设置JDBC的Driver类
try {
ds.setDriverClass(driverClassName);
}
catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
// 设置JDBC的URL
ds.setJdbcUrl(jdbcURL);
// 设置数据库的登录用户名
ds.setUser(userName);
// 设置数据库的登录用户密码
ds.setPassword(userPassword);
// 设置连接池的最大连接数
ds.setMaxPoolSize(MaxPoolSize);
// 设置连接池的最小连接数
ds.setMinPoolSize(MinPoolSize);
// 设置初始化连接数
ds.setInitialPoolSize(initPoolSize);
// 设置最大闲置时间
ds.setMaxIdleTime(maxIdleTime);
// 设置测试SQL
ds.setPreferredTestQuery(testQuerySQL);
// 设置闲置测试周期
ds.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
// 增加单个连接的Statements数量
ds.setMaxStatements(0);
ds.setMaxStatementsPerConnection(200);
}
示例5: DAOFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DAOFactory(String facName, String driverClassName, String jdbcURL, String userName, String userPassword,
int initPoolSize, int MinPoolSize, int MaxPoolSize, int maxIdleTime, int idleConnectionTestPeriod,
String testQuerySQL) {
this.facName = facName;
ds = new ComboPooledDataSource();
// 设置JDBC的Driver类
try {
ds.setDriverClass(driverClassName);
}
catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
// 设置JDBC的URL
ds.setJdbcUrl(jdbcURL);
// 设置数据库的登录用户名
ds.setUser(userName);
// 设置数据库的登录用户密码
ds.setPassword(userPassword);
// 设置连接池的最大连接数
ds.setMaxPoolSize(MaxPoolSize);
// 设置连接池的最小连接数
ds.setMinPoolSize(MinPoolSize);
// 设置初始化连接数
ds.setInitialPoolSize(initPoolSize);
// 设置最大闲置时间
ds.setMaxIdleTime(maxIdleTime);
// 设置测试SQL
ds.setPreferredTestQuery(testQuerySQL);
// 设置闲置测试周期
ds.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
// 增加单个连接的Statements数量
ds.setMaxStatements(0);
// 连接池内单个连接所拥有的最大缓存statements数
ds.setMaxStatementsPerConnection(200);
// 获取空闲连接超时时间
ds.setCheckoutTimeout(10 * 1000);
}
示例6: createDelegate
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
protected DataSource createDelegate() {
final ComboPooledDataSource ds = new ComboPooledDataSource();
try {
ds.setDriverClass(context.getConnectionDriver());
} catch (final PropertyVetoException e1) {
throw Err.process(e1);
}
ds.setJdbcUrl(context.getConnectionUrl());
ds.setUser(context.getConnectionUser());
ds.setPassword(context.getConnectionPassword());
ds.setMaxStatements(5000);
ds.setMaxStatementsPerConnection(50);
ds.setStatementCacheNumDeferredCloseThreads(1); //fix apparent deadlocks
ds.setMaxPoolSize(100);
ds.setMinPoolSize(1);
ds.setMaxIdleTime(new Duration(1, FTimeUnit.MINUTES).intValue(FTimeUnit.SECONDS));
ds.setTestConnectionOnCheckout(true);
ds.setAutoCommitOnClose(true);
Assertions.assertThat(this.closeableDs).isNull();
this.closeableDs = ds;
if (logging) {
try {
final ConnectionPoolDataSourceProxy proxy = new ConnectionPoolDataSourceProxy();
proxy.setTargetDSDirect(ds);
return proxy;
} catch (final JdbcDsLogRuntimeException e) {
throw new RuntimeException(e);
}
} else {
return ds;
}
}
示例7: configure
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
public void configure(ComboPooledDataSource ds) {
ds.setMinPoolSize(poolSize);
ds.setInitialPoolSize(poolSize);
ds.setMaxPoolSize(poolSize * 3);
ds.setAcquireIncrement(aquireInc);
ds.setMaxIdleTime(maxIdle);
ds.setMaxConnectionAge(maxAge);
ds.setMaxStatementsPerConnection(maxStmts);
ds.setAcquireRetryAttempts(retry);
ds.setAcquireRetryDelay(retryDelay);
}
示例8: Dao
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public Dao(DbConfig config) {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(config.getUrl());
dataSource.setUser(config.getUser());
dataSource.setPassword(config.getPassword());
try {
dataSource.setDriverClass(config.getDriverClass());
} catch (PropertyVetoException e) {
throw new ServiceException(MessageFormat.format("{0} not found",
config.getDriverClass()));
}
logger.info("Dao init url={},user={},driverClass={}", new Object[] {
config.getUrl(), config.getUser(), config.getDriverClass() });
if (config.getInitialPoolSize() != 0) {
dataSource.setInitialPoolSize(config.getInitialPoolSize());
}
if (config.getMaxPoolSize() != 0) {
dataSource.setMaxPoolSize(config.getMaxPoolSize());
}
if (config.getMinPoolSize() != 0) {
dataSource.setMinPoolSize(config.getMinPoolSize());
}
if (config.getAcquireIncrement() != 0) {
dataSource.setAcquireIncrement(config.getAcquireIncrement());
}
if (config.getMaxIdleTime() != 0) {
dataSource.setMaxIdleTime(config.getMaxIdleTime());
}
if (config.getMaxStatements() != 0) {
dataSource.setMaxStatements(config.getMaxStatements());
}
if (config.getMaxStatementsPerConnection() != 0) {
dataSource.setMaxStatementsPerConnection(config
.getMaxStatementsPerConnection());
}
if (config.getPreferredTestQuery() != null) {
dataSource.setPreferredTestQuery(config.getPreferredTestQuery());
}
jdbc = new JDBCTemplate(dataSource);
}
示例9: L2DatabaseFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public L2DatabaseFactory() throws SQLException
{
try
{
if (Config.DATABASE_MAX_CONNECTIONS < 2)
{
Config.DATABASE_MAX_CONNECTIONS = 2;
_log.warning("at least " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
}
_source = new ComboPooledDataSource();
_source.setAutoCommitOnClose(true);
_source.setInitialPoolSize(10);
_source.setMinPoolSize(10);
_source.setMaxPoolSize(Config.DATABASE_MAX_CONNECTIONS);
_source.setAcquireRetryAttempts(0); // try to obtain connections indefinitely (0 = never quit)
_source.setAcquireRetryDelay(500); // 500 miliseconds wait before try to acquire connection again
_source.setCheckoutTimeout(0); // 0 = wait indefinitely for new connection
// if pool is exhausted
_source.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time
// cause there is a "long" delay on acquire connection
// so taking more than one connection at once will make connection pooling
// more effective.
// this "connection_test_table" is automatically created if not already there
_source.setAutomaticTestTable("connection_test_table");
_source.setTestConnectionOnCheckin(false);
// testing OnCheckin used with IdleConnectionTestPeriod is faster than testing on checkout
_source.setIdleConnectionTestPeriod(3600); // test idle connection every 60 sec
_source.setMaxIdleTime(0); // 0 = idle connections never expire
// *THANKS* to connection testing configured above
// but I prefer to disconnect all connections not used
// for more than 1 hour
// enables statement caching, there is a "semi-bug" in c3p0 0.9.0 but in 0.9.0.2 and later it's fixed
_source.setMaxStatementsPerConnection(100);
_source.setBreakAfterAcquireFailure(false); // never fail if any way possible
// setting this to true will make
// c3p0 "crash" and refuse to work
// till restart thus making acquire
// errors "FATAL" ... we don't want that
// it should be possible to recover
_source.setDriverClass(Config.DATABASE_DRIVER);
_source.setJdbcUrl(Config.DATABASE_URL);
_source.setUser(Config.DATABASE_LOGIN);
_source.setPassword(Config.DATABASE_PASSWORD);
/* Test the connection */
_source.getConnection().close();
if (Config.DEBUG) _log.fine("Database Connection Working");
if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
_providerType = ProviderType.MsSql;
else
_providerType = ProviderType.MySql;
}
catch (SQLException x)
{
if (Config.DEBUG) _log.fine("Database Connection FAILED");
// rethrow the exception
throw x;
}
catch (Exception e)
{
if (Config.DEBUG) _log.fine("Database Connection FAILED");
throw new SQLException("could not init DB connection:"+e);
}
}
示例10: initialize
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Create the underlying C3PO ComboPooledDataSource with the
* default supported properties.
* @throws SchedulerException
*/
private void initialize(
String dbDriver,
String dbURL,
String dbUser,
String dbPassword,
int maxConnections,
int maxStatementsPerConnection,
String dbValidationQuery,
boolean validateOnCheckout,
int idleValidationSeconds,
int maxIdleSeconds) throws SQLException, SchedulerException {
if (dbURL == null) {
throw new SQLException(
"DBPool could not be created: DB URL cannot be null");
}
if (dbDriver == null) {
throw new SQLException(
"DBPool '" + dbURL + "' could not be created: " +
"DB driver class name cannot be null!");
}
if (maxConnections < 0) {
throw new SQLException(
"DBPool '" + dbURL + "' could not be created: " +
"Max connections must be greater than zero!");
}
datasource = new ComboPooledDataSource();
try {
datasource.setDriverClass(dbDriver);
} catch (PropertyVetoException e) {
throw new SchedulerException("Problem setting driver class name on datasource: " + e.getMessage(), e);
}
datasource.setJdbcUrl(dbURL);
datasource.setUser(dbUser);
datasource.setPassword(dbPassword);
datasource.setMaxPoolSize(maxConnections);
datasource.setMinPoolSize(1);
datasource.setMaxIdleTime(maxIdleSeconds);
datasource.setMaxStatementsPerConnection(maxStatementsPerConnection);
if (dbValidationQuery != null) {
datasource.setPreferredTestQuery(dbValidationQuery);
if(!validateOnCheckout)
datasource.setTestConnectionOnCheckin(true);
else
datasource.setTestConnectionOnCheckout(true);
datasource.setIdleConnectionTestPeriod(idleValidationSeconds);
}
}
示例11: setupDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static void setupDataSource(Isolation isolation) {
ds = new ComboPooledDataSource();
Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
String hostname = getHostNameFromEndpoint(locatorEndPoint);
int port = getPortFromEndpoint(locatorEndPoint);
connProp.putAll(getExtraConnProp()); //singlehop conn properties and any additional prop
try {
ds.setProperties(connProp);
ds.setDriverClass(driver);
ds.setJdbcUrl(protocol + hostname+ ":" + port);
ds.setMinPoolSize(5);
ds.setAcquireIncrement(5);
ds.setMaxPoolSize(numOfWorkers + 100);
ds.setMaxStatementsPerConnection(10);
if (isolation == Isolation.NONE) {
ds.setConnectionCustomizerClassName( "sql.sqlutil.MyConnectionCustomizer");
} else if (isolation == Isolation.READ_COMMITTED) {
ds.setConnectionCustomizerClassName("sql.sqlutil.IsolationRCConnectionCustomizer");
} else {
ds.setConnectionCustomizerClassName("sql.sqlutil.IsolationRRConnectionCustomizer");
}
Log.getLogWriter().info("Pooled data source url is set as " + ds.getJdbcUrl());
StringBuilder sb = new StringBuilder();
for (Iterator iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
sb.append(entry.getKey() + " is set to " + entry.getValue() +"\n");
}
Log.getLogWriter().info("Pooled data source setting the following connection prop: " + sb.toString());
} catch (Exception e) {
throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
}
}
示例12: createDataPool
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DataSource createDataPool(PoolConfig conf) {
if (!conf.isValid()) {
throw new RuntimeException("数据库驱动为空或者数据库用户名或者密码或者连接字符串为空");
}
log.info("db config:{}", conf.toString());
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(conf.jdbcUrl);
cpds.setUser(conf.userName);
cpds.setPassword(conf.password);
try {
cpds.setDriverClass(conf.driverClass);
} catch (PropertyVetoException e) {
throw new RuntimeException("数据库驱动设置失败");
}
cpds.setPreferredTestQuery("select 1");
cpds.setIdleConnectionTestPeriod(60);
if (conf.initialPoolSize != null && !conf.initialPoolSize.isEmpty()) {
cpds.setInitialPoolSize(Integer.parseInt(conf.initialPoolSize));
}
if (conf.acquireIncrement != null && !conf.acquireIncrement.isEmpty()) {
cpds.setAcquireIncrement(Integer.parseInt(conf.acquireIncrement));
}
if (conf.maxPoolSize != null && !conf.maxPoolSize.isEmpty()) {
cpds.setMaxPoolSize(Integer.parseInt(conf.maxPoolSize));
}
if (conf.minPoolSize != null && !conf.minPoolSize.isEmpty()) {
cpds.setMinPoolSize(Integer.parseInt(conf.minPoolSize));
}
if (conf.checkoutTimeout != null && !conf.checkoutTimeout.isEmpty()) {
cpds.setCheckoutTimeout(Integer.parseInt(conf.checkoutTimeout));
}
if (conf.maxStatements != null && !conf.maxStatements.isEmpty()) {
cpds.setMaxStatements(Integer.parseInt(conf.maxStatements));
}
if (conf.maxStatementsPerConnection != null && !conf.maxStatementsPerConnection.isEmpty()) {
cpds.setMaxStatementsPerConnection(Integer.parseInt(conf.maxStatementsPerConnection));
}
if (conf.maxIdleTime != null && !conf.maxIdleTime.isEmpty()) {
cpds.setMaxIdleTime(Integer.parseInt(conf.maxIdleTime));
}
if (conf.unreturnedConnectionTimeout != null && !conf.unreturnedConnectionTimeout.isEmpty()) {
cpds.setDebugUnreturnedConnectionStackTraces(true);
cpds.setUnreturnedConnectionTimeout(Integer.parseInt(conf.unreturnedConnectionTimeout));
} else {
cpds.setDebugUnreturnedConnectionStackTraces(false);
}
return cpds;
}
示例13: initDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Initializes the connection pool.
* @param dbSpec the object representing the chosen DB target system.
* @param jdbcUrl the JDBC URL to connect to.
* @throws SQLException
*/
private static void initDataSource(DBSpecifics dbSpec, String jdbcUrl)
throws SQLException {
dataSource = new ComboPooledDataSource();
String username = Settings.get(ArchiveSettings.DB_USERNAME);
if (!username.isEmpty()) {
dataSource.setUser(username);
}
String password = Settings.get(ArchiveSettings.DB_PASSWORD);
if (!password.isEmpty()) {
dataSource.setPassword(password);
}
try {
dataSource.setDriverClass(dbSpec.getDriverClassName());
} catch (PropertyVetoException e) {
final String message =
"Failed to set datasource JDBC driver class '"
+ dbSpec.getDriverClassName() + "'" + "\n";
throw new IOFailure(message, e);
}
dataSource.setJdbcUrl(jdbcUrl);
// Configure pool size
dataSource.setMinPoolSize(
Settings.getInt(ArchiveSettings.DB_POOL_MIN_SIZE));
dataSource.setMaxPoolSize(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_SIZE));
dataSource.setAcquireIncrement(
Settings.getInt(ArchiveSettings.DB_POOL_ACQ_INC));
// Configure idle connection testing
int testPeriod =
Settings.getInt(ArchiveSettings.DB_POOL_IDLE_CONN_TEST_PERIOD);
if (testPeriod > 0) {
dataSource.setIdleConnectionTestPeriod(testPeriod);
dataSource.setTestConnectionOnCheckin(
Settings.getBoolean(
ArchiveSettings.DB_POOL_IDLE_CONN_TEST_ON_CHECKIN));
String testQuery =
Settings.get(ArchiveSettings.DB_POOL_IDLE_CONN_TEST_QUERY);
if (!testQuery.isEmpty()) {
dataSource.setPreferredTestQuery(testQuery);
}
}
// Configure statement pooling
dataSource.setMaxStatements(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_STM));
dataSource.setMaxStatementsPerConnection(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_STM_PER_CONN));
if (log.isInfoEnabled()) {
String msg =
"Connection pool initialized with the following values:";
msg += "\n- minPoolSize=" + dataSource.getMinPoolSize();
msg += "\n- maxPoolSize=" + dataSource.getMaxPoolSize();
msg += "\n- acquireIncrement=" + dataSource.getAcquireIncrement();
msg += "\n- maxStatements=" + dataSource.getMaxStatements();
msg += "\n- maxStatementsPerConnection="
+ dataSource.getMaxStatementsPerConnection();
msg += "\n- idleConnTestPeriod="
+ dataSource.getIdleConnectionTestPeriod();
msg += "\n- idleConnTestQuery='"
+ dataSource.getPreferredTestQuery() + "'";
msg += "\n- idleConnTestOnCheckin="
+ dataSource.isTestConnectionOnCheckin();
log.info(msg.toString());
}
}
示例14: initDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Initializes the connection pool.
* @param dbSpec the object representing the chosen DB target system.
* @param jdbcUrl the JDBC URL to connect to.
* @throws SQLException
*/
private static void initDataSource(DBSpecifics dbSpec, String jdbcUrl)
throws SQLException {
dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass(dbSpec.getDriverClassName());
} catch (PropertyVetoException e) {
final String message =
"Failed to set datasource JDBC driver class '"
+ dbSpec.getDriverClassName() + "'" + "\n";
throw new IOFailure(message, e);
}
dataSource.setJdbcUrl(jdbcUrl);
String username = Settings.get(CommonSettings.DB_USERNAME);
if (!username.isEmpty()) {
dataSource.setUser(username);
}
String password = Settings.get(CommonSettings.DB_PASSWORD);
if (!password.isEmpty()) {
dataSource.setPassword(password);
}
// Configure pool size
dataSource.setMinPoolSize(
Settings.getInt(CommonSettings.DB_POOL_MIN_SIZE));
dataSource.setMaxPoolSize(
Settings.getInt(CommonSettings.DB_POOL_MAX_SIZE));
dataSource.setAcquireIncrement(
Settings.getInt(CommonSettings.DB_POOL_ACQ_INC));
// Configure idle connection testing
int testPeriod =
Settings.getInt(CommonSettings.DB_POOL_IDLE_CONN_TEST_PERIOD);
//TODO This looks odd. Why is checkin-testing inside this if statement?
if (testPeriod > 0) {
dataSource.setIdleConnectionTestPeriod(testPeriod);
dataSource.setTestConnectionOnCheckin(
Settings.getBoolean(
CommonSettings.DB_POOL_IDLE_CONN_TEST_ON_CHECKIN));
String testQuery =
Settings.get(CommonSettings.DB_POOL_IDLE_CONN_TEST_QUERY);
if (!testQuery.isEmpty()) {
dataSource.setPreferredTestQuery(testQuery);
}
}
// Configure statement pooling
dataSource.setMaxStatements(
Settings.getInt(CommonSettings.DB_POOL_MAX_STM));
dataSource.setMaxStatementsPerConnection(
Settings.getInt(CommonSettings.DB_POOL_MAX_STM_PER_CONN));
//dataSource.setTestConnectionOnCheckout(true);
//dataSource.setBreakAfterAcquireFailure(false);
//dataSource.setAcquireRetryAttempts(10000);
//dataSource.setAcquireRetryDelay(10);
if (log.isInfoEnabled()) {
String msg =
"Connection pool initialized with the following values:";
msg += "\n- minPoolSize=" + dataSource.getMinPoolSize();
msg += "\n- maxPoolSize=" + dataSource.getMaxPoolSize();
msg += "\n- acquireIncrement=" + dataSource.getAcquireIncrement();
msg += "\n- maxStatements=" + dataSource.getMaxStatements();
msg += "\n- maxStatementsPerConnection="
+ dataSource.getMaxStatementsPerConnection();
msg += "\n- idleConnTestPeriod="
+ dataSource.getIdleConnectionTestPeriod();
msg += "\n- idleConnTestQuery='"
+ dataSource.getPreferredTestQuery() + "'";
msg += "\n- idleConnTestOnCheckin="
+ dataSource.isTestConnectionOnCheckin();
log.info(msg.toString());
}
}
示例15: getDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
public DataSource getDataSource(JsonObject config) throws SQLException {
String url = config.getString("url");
if (url == null) throw new NullPointerException("url cannot be null");
String driverClass = config.getString("driver_class");
String user = config.getString("user");
String password = config.getString("password");
Integer maxPoolSize = config.getInteger("max_pool_size");
Integer initialPoolSize = config.getInteger("initial_pool_size");
Integer minPoolSize = config.getInteger("min_pool_size");
Integer maxStatements = config.getInteger("max_statements");
Integer maxStatementsPerConnection = config.getInteger("max_statements_per_connection");
Integer maxIdleTime = config.getInteger("max_idle_time");
Integer acquireRetryAttempts = config.getInteger("acquire_retry_attempts");
Integer acquireRetryDelay = config.getInteger("acquire_retry_delay");
Boolean breakAfterAcquireFailure = config.getBoolean("break_after_acquire_failure");
// If you want to configure any other C3P0 properties you can add a file c3p0.properties to the classpath
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(url);
if (driverClass != null) {
try {
cpds.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
throw new IllegalArgumentException(e);
}
}
if (user != null) {
cpds.setUser(user);
}
if (password != null) {
cpds.setPassword(password);
}
if (maxPoolSize != null) {
cpds.setMaxPoolSize(maxPoolSize);
}
if (minPoolSize != null) {
cpds.setMinPoolSize(minPoolSize);
}
if (initialPoolSize != null) {
cpds.setInitialPoolSize(initialPoolSize);
}
if (maxStatements != null) {
cpds.setMaxStatements(maxStatements);
}
if (maxStatementsPerConnection != null) {
cpds.setMaxStatementsPerConnection(maxStatementsPerConnection);
}
if (maxIdleTime != null) {
cpds.setMaxIdleTime(maxIdleTime);
}
if(acquireRetryAttempts != null){
cpds.setAcquireRetryAttempts(acquireRetryAttempts);
}
if(acquireRetryDelay != null){
cpds.setAcquireRetryDelay(acquireRetryDelay);
}
if(breakAfterAcquireFailure != null){
cpds.setBreakAfterAcquireFailure(breakAfterAcquireFailure);
}
return cpds;
}