DBCP 配置文件加Java code

本文详细介绍了使用Apache Commons DBCP进行数据库连接池配置的方法。包括核心配置参数如driverClassName、url、username及password等,并解释了如何通过initialSize、maxActive、maxIdle等属性调整连接池的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#Driver
driverClassName=com.mysql.jdbc.Driver
#UrL
url=jdbc:mysql://192.168.111.15:3306/archive_reader
#User name
username=trafficcast
#Password
password=naitou
#The initial number of connections that are created when the pool is started. 
initialSize=10
#The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
maxActive=50
#The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
maxIdle=20
#The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none.
minIdle=5
#The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.
maxWait=60000
#The connection properties that will be sent to our JDBC driver when establishing new connections. 
connectionProperties=useUnicode=true;characterEncoding=gbk
#The default auto-commit state of connections created by this pool.
defaultAutoCommit=true
#The default read-only state of connections created by this pool. If not set then the setReadOnly method will not be called.
defaultReadOnly=
#The default TransactionIsolation state of connections created by this pool.

defaultTransactionIsolation=READ_UNCOMMITTED



package com.trafficcast.jdbc;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public final class JdbcUtils {
	//Data source
	private static DataSource myDataSource = null;
	//Private Constructor
	private JdbcUtils() {}
    //Init the properties and init the data source.
	static {
		try {
			Properties prop = new Properties();
			InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
			prop.load(is);
			myDataSource = BasicDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
    /**
     * Get the data source
     * @return
     */
	public static DataSource getDataSource() {
		return myDataSource;
	}
	
	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值