DBCP 配置

本文详细介绍了DBCP数据库连接池的配置步骤和关键参数设置,包括在DBCP.properties文件中的配置内容,帮助读者理解和实现高效稳定的数据库连接管理。

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

DBCP.properties 配置如下:


#打印明细日志-连接使用情况
isDetailLog=true


#初始化连接数
ODS_initialSize=5
#最大空闲连接数
ODS_maxIdle=5
#最小空闲连接数
ODS_minIdle=2
#最大连接数量
ODS_maxActive=50

#初始化连接数
EDI_initialSize=5
#最大空闲连接数
EDI_maxIdle=5
#最小空闲连接数
EDI_minIdle=2
#最大连接数量
EDI_maxActive=100

#初始化连接数
PROCESS_initialSize=5
#最大空闲连接数
PROCESS_maxIdle=5
#最小空闲连接数
PROCESS_minIdle=2
#最大连接数量
PROCESS_maxActive=100



#默认初始化连接数
initialSize=5

#默认最大空闲连接数
maxIdle=5

#默认最小空闲连接数
minIdle=2

#默认最大连接数量
maxActive=100


#超时等待时间以毫秒为单位
#maxWait代表当Connection用尽了,超时等待多久之后丢出异常
#maxWait=30000
#maxWait以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时无限期等待
#根据资料显示,maxWait过大容易引起连接池泄露
maxWait=30000

#是否自动回收超时连接
removeAbandoned=true

#多长时间被定义为超时连接(单位:秒)  
removeAbandonedTimeout=180

#是否在自动回收超时连接的时候打印连接的超时错误
logAbandoned=true

#连接池创建的连接的默认的auto-commit状态 
defaultAutoCommit=false

#连接池创建的连接的默认的read-only状态. 如果没有设置则setReadOnly方法将不会被调用. (某些驱动不支持只读模式,比如:Informix)  
#defaultReadOnly= driver default 

#连接池创建的连接的默认的TransactionIsolation状态. 下面列表当中的某一个: (参考javadoc) 
#defaultTransactionIsolation= driver default 
#NONE 
#READ_COMMITTED 
#READ_UNCOMMITTED 
#REPEATABLE_READ 
#SERIALIZABLE 


#连接池创建的连接的默认的catalog  
#defaultCatalog =
 


#SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录  
#validationQuery=select getdate()
validationQuery=select 1 from dual

#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串 
testOnBorrow= true

#指明是否在归还到池中前进行检验 
#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串  
testOnReturn =true

#指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串  
testWhileIdle =true

#在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. 如果设置为非正数,则不运行空闲连接回收器线程  
timeBetweenEvictionRunsMillis= 5000

#在每次空闲连接回收器线程(如果有)运行时检查的连接数量  
numTestsPerEvictionRun=100

#连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒,默认1000 * 60 * 10 
minEvictableIdleTimeMillis =30000
 



Java创建连接池:


	private static DBCPConfig getDBCPConfig(String DataSourceName) {

		//下面的读取配置文件可以根据实际的不同修改
		PropertyLoader properties = new PropertyLoader(DBCP_PROERTITES);
		Properties dbProps = properties.getProperties();
		DBCPConfig defaultDBCPConfig = new DBCPConfig();

		String driverClasses =
			properties1.getProperty(DataSourceName + "_DatabaseDriver");
		String user = properties1.getProperty(DataSourceName + "_USER");
		String password = properties1.getProperty(DataSourceName + "_PASSWORD");

		String url = properties1.getProperty(DataSourceName + "_URL");
		if (url == null) {
			logger.error("No URL specified for " + DataSourceName);
			return null;
		}
		String initialSize = dbProps.getProperty(DataSourceName+"_initialSize");
		if(initialSize == null)
			dbProps.getProperty("initialSize");
		String minIdle = dbProps.getProperty(DataSourceName+"_minIdle");
		if(minIdle == null)
			minIdle = dbProps.getProperty("minIdle");
		
		String maxIdle = dbProps.getProperty(DataSourceName+"_maxIdle");
		if(maxIdle == null)
			dbProps.getProperty("maxIdle");
		
		String maxWait = dbProps.getProperty(DataSourceName+"_maxWait");
		if( maxWait == null)
			 dbProps.getProperty("maxWait");
		
		String maxActive = dbProps.getProperty(DataSourceName+"_maxActive");
		if(maxActive == null)
			 dbProps.getProperty("maxActive");
		
		defaultDBCPConfig.setDriverClassName(driverClasses);
		defaultDBCPConfig.setUrl(url);
		defaultDBCPConfig.setUsername(user);
		defaultDBCPConfig.setPassword(password);

		//初始化连接数
		if (initialSize != null)
			defaultDBCPConfig.setInitialSize(Integer.parseInt(initialSize));

		//最小空闲连接
		if (minIdle != null)
			defaultDBCPConfig.setMinIdle(Integer.parseInt(minIdle));

		//最大空闲连接
		if (maxIdle != null)
			defaultDBCPConfig.setMaxIdle(Integer.parseInt(maxIdle));

		//maxWait以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时无限期等待
		//根据资料显示,maxWait过大容易引起连接池泄露
		if (maxWait != null)
			defaultDBCPConfig.setMaxWait(Long.parseLong(maxWait));

		//最大连接数
		if (maxActive != null) {
			if (!maxActive.trim().equals("0"))
				defaultDBCPConfig.setMaxActive(Integer.parseInt(maxActive));
		}

		//连接池创建的连接的默认的auto-commit状态
		boolean defaultAutoCommit =
			(Boolean.valueOf(dbProps.getProperty("defaultAutoCommit", "true")))
				.booleanValue();
		defaultDBCPConfig.setDefaultAutoCommit(defaultAutoCommit);

		//SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录  
		String validationQuery = dbProps.getProperty("validationQuery");
		defaultDBCPConfig.setValidationQuery(validationQuery);

		//指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
		//注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串 
		boolean testOnBorrow =
			(Boolean.valueOf(dbProps.getProperty("testOnBorrow", "true")))
				.booleanValue();
		defaultDBCPConfig.setTestOnBorrow(testOnBorrow);

		//指明是否在归还到池中前进行检验 
		//注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串  
		boolean testOnReturn =
			(Boolean.valueOf(dbProps.getProperty("testOnReturn", "false")))
				.booleanValue();
		defaultDBCPConfig.setTestOnReturn(testOnReturn);

		//指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
		//注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串  
		boolean testWhileIdle =
			(Boolean.valueOf(dbProps.getProperty("testWhileIdle", "true")))
				.booleanValue();
		defaultDBCPConfig.setTestWhileIdle(testWhileIdle);

		//在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. 如果设置为非正数,则不运行空闲连接回收器线程  
		String timeBetweenEvictionRunsMillis =
			dbProps.getProperty("timeBetweenEvictionRunsMillis", "30000");
		defaultDBCPConfig.setTimeBetweenEvictionRunsMillis(
			Long.parseLong(timeBetweenEvictionRunsMillis));

		//在每次空闲连接回收器线程(如果有)运行时检查的连接数量	
		String numTestsPerEvictionRun =
			dbProps.getProperty("numTestsPerEvictionRun", "3");
		defaultDBCPConfig.setNumTestsPerEvictionRun(
			Integer.parseInt(numTestsPerEvictionRun));

		//连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒,默认1000 * 60 * 10 

		String minEvictableIdleTimeMillis =
			dbProps.getProperty("minEvictableIdleTimeMillis", "600000");
		defaultDBCPConfig.setMinEvictableIdleTimeMillis(
			Long.parseLong(minEvictableIdleTimeMillis));

		//是否自动回收超时连接
		boolean removeAbandoned =
			(Boolean.valueOf(dbProps.getProperty("removeAbandoned", "true")))
				.booleanValue();
		defaultDBCPConfig.setRemoveAbandoned(removeAbandoned);

		//多长时间被定义为超时连接(单位:秒)  
		String removeAbandonedTimeout =
			dbProps.getProperty("removeAbandonedTimeout", "600");
		defaultDBCPConfig.setRemoveAbandonedTimeout(
			Integer.parseInt(removeAbandonedTimeout));

		//是否在自动回收超时连接的时候打印连接的超时错误
		boolean logAbandoned =
			(Boolean.valueOf(dbProps.getProperty("logAbandoned", "true")))
				.booleanValue();
		defaultDBCPConfig.setLogAbandoned(logAbandoned);

		return defaultDBCPConfig;

	}

	private static BasicDataSource createDataSource(DBCPConfig dbConfig) {
		
		if(dbConfig == null)
		{

			return null;
		}
		BasicDataSource dataSource = new BasicDataSource();

		dataSource.setDriverClassName(dbConfig.getDriverClassName());

		dataSource.setUrl(dbConfig.getUrl());
		dataSource.setUsername(dbConfig.getUsername());
		dataSource.setPassword(dbConfig.getPassword());

		dataSource.setDefaultAutoCommit(dbConfig.isDefaultAutoCommit());
		dataSource.setInitialSize(dbConfig.getInitialSize());
		dataSource.setMaxActive(dbConfig.getMaxActive());
		dataSource.setMaxIdle(dbConfig.getMaxIdle());
		dataSource.setMaxWait(dbConfig.getMaxWait());
		dataSource.setMinIdle(dbConfig.getMinIdle());
		dataSource.setMinEvictableIdleTimeMillis(
			dbConfig.getMinEvictableIdleTimeMillis());
		
		// validationQuery
		dataSource.setValidationQuery(dbConfig.getValidationQuery());
		
		dataSource.setTestOnBorrow(dbConfig.isTestOnBorrow());
		dataSource.setTestOnReturn(dbConfig.isTestOnReturn());
		dataSource.setTestWhileIdle(dbConfig.isTestWhileIdle());
		dataSource.setTimeBetweenEvictionRunsMillis(
			dbConfig.getTimeBetweenEvictionRunsMillis());

		dataSource.setRemoveAbandoned(dbConfig.isRemoveAbandoned());
		dataSource.setRemoveAbandonedTimeout(
			dbConfig.getRemoveAbandonedTimeout());
		dataSource.setLogAbandoned(dbConfig.isLogAbandoned());

		return dataSource;

	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值