日期:2014-05-16  浏览次数:20359 次

Ibatis2 整合BoneCP
最近开发项目使用Ibatis+Lucene,使用轻量框架实例自己管理,不使用Spring,Ibatis数据源原来使用JNDI,为统一配置加入dbconfig.properties,并使用boneCP,Ibatis2数据源只提供JNDI、SIMPLE、DBCP,无法对BoneCP提供支持,故参考DbcpDataSourceFactory自己创建工厂类,发布博客,以作备份.
dbconfig.properties
###============搜索数据库配置=============##
search.db.url=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =*)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = wcsdb)))
search.db.username=xiu_search
search.db.password=*

##bonecp
##每60秒检查所有连接池中的空闲连接   
search.db.idleConnectionTestPeriod=20
##设置连接空闲时间
search.db.idleMaxAge=20
##设置连接池在每个分区中的最大连接数
search.db.maxConnectionsPerPartition=15
##设置连接池设在每个分区中的最小连接数
search.db.minConnectionsPerPartition=10
##设置分区(设置 2个分区)
search.db.partitionCount=2
##连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数  
search.db.acquireIncrement=5
##每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能  
search.db.releaseHelperThreads=3
search.db.statementsCachedPerConnection=100


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<properties resource="dbconfig.properties"/>
	
	<settings cacheModelsEnabled="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" maxRequests="10" maxSessions="20"
		maxTransactions="50" useStatementNamespaces="true"
		defaultStatementTimeout="30" statementCachingEnabled="true"
		classInfoCacheEnabled="true" errorTracingEnabled="true" />

     <typeAlias alias="BONECP" type="com.pltfm.sys.util.BoneCPDataSourceFactory"/>
 <transactionManager type="JDBC" commitRequired="false"> 
		<dataSource type="BONECP">
                 <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
                 <property name="jdbcUrl" value="${search.db.url}" />
                 <property name="username" value="${search.db.username}" />
                 <property name="password" value="${search.db.password}" />
                 <property name="idleMaxAge" value="${search.db.idleMaxAge}" />
                 <property name="partitionCount" value="${search.db.partitionCount}" />
                 <property name="maxConnectionsPerPartition" value="${search.db.maxConnectionsPerPartition}" />
                 <property name="minConnectionsPerPartition" value="${search.db.minConnectionsPerPartition}" />
                 <property name="driver.encoding" value="UTF8" />
                 <property name="Driver.releaseHelperThreads" value="${search.db.releaseHelperThreads}" />
                 <property name="Driver.statementsCachedPerConnection" value="${search.db.statementsCachedPerConnection}" />
          </dataSource>
	</transactionManager> 
<sqlMap resource="com/pltfm/sys/sqlmap/sys_param_SqlMap.xml" />
</sqlMapConfig>



package com.pltfm.sys.util;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sql.DataSource;

import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
import com.jolbox.bonecp.BoneCPDataSource;

/**
 * <h1>支持Ibatis2 BoneCP连接池工厂</h2><br/>
 * <h2>默认提供参数如下:</h2><br/>
 * #driverClass<br/>
 * #jdbcUrl<br/>
 * #username<br/>
 * #password<br/>
 * #每60秒检查所有连接池中的空闲连接   
 * idleConnectionTestPeriod=20<br/>
 * ##设置连接空闲时间<br/>
 * idleMaxAge=20<br/>
 * ##设置连接池在每个分区中的最大连接数<br/>
 * maxConnectionsPerPartition=15<br/>
 * ##设置连接池设在每个分区中的最小连接数<br/>
 * minConnectionsPerPartition=10<br/>
 * ##设置分区(设置 2