C3P0和DBCP的區別
C3P0是一個開源的JDBC連接池,它實現了數據源和JNDI綁定,支持JDBC3規范和JDBC2的標准擴展。目前使用它的開源項目有Hibernate,Spring等。
dbcp簡介:
DBCP(DataBase connection pool),數據庫連接池。是 apache 上的一個 java 連接池項目,也是 tomcat 使用的連接池組件。單獨使用dbcp需要3個包:common-dbcp.jar,common-pool.jar,common-collections.jar由於建立數據庫連接是一個非常耗時耗資源的行為,所以通過連接池預先同數據庫建立一些連接,放在內存中,應用程序需要建立數據庫連接時直接到連接池中申請一個就行,用完后再放回去。
c3p0與dbcp區別:
dbcp沒有自動的去回收空閑連接的功能 c3p0有自動回收空閑連接功能
兩者主要是對數據連接的處理方式不同!C3P0提供最大空閑時間,DBCP提供最大連接數。
前者當連接超過最大空閑連接時間時,當前連接就會被斷掉。DBCP當連接數超過最大連接數時,所有連接都會被斷開
1、首先配置多個datasource
<!-- 主數據庫的數據據源 --> <bean id="masterDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.10.11:1521:trew" /> <property name="username" value="poi" /> <property name="password" value="poi" /> </bean> <!-- 備份庫的數據據源 --> <bean id="slaveDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.10.12:1521:trew" /> <property name="username" value="poi2" /> <property name="password" value="poi2" /> </bean>
2、寫一個DynamicDataSource類繼承AbstractRoutingDataSource,並實現determineCurrentLookupKey方法
public class DynamicDataSource extends AbstractRoutingDataSource {
@SuppressWarnings("unused")
private Log logger = LogFactory.getLog(getClass());
@Override
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
}
public class DbContextHolder {
@SuppressWarnings("rawtypes")
private static final ThreadLocal contextHolder = new ThreadLocal();
@SuppressWarnings("unchecked")
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
3. 配置動態數據源
<!--將DynamicDataSource Bean加入到Spring的上下文xml配置文件中去,同時配置DynamicDataSource的targetDataSources(多數據源目標)屬性的Map映射。--> <bean id="dataSource" class="cn.com.core.datasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="masterDataSource" value-ref="masterDataSource" /> <entry key="slaveDataSource" value-ref="slaveDataSource" /> </map> </property> <property name="defaultTargetDataSource" ref="masterDataSource"/> </bean>
4.使用動態數據源(hibernate)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="lobHandler" ref="lobHandler"/> <property name="eventListeners"> <map> <entry key="post-insert"> <ref bean="logListener"/> </entry> <entry key="post-update"> <ref bean="logListener"/> </entry> <entry key="post-delete"> <ref bean="logListener"/> </entry> </map> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect <!-- org.hibernate.dialect.OracleDerbyDialect --> </prop> <prop key="hibernate.show_sql">true</prop> <!-- <prop key="hibernate.generate_statistics">true</prop> --> <prop key="hibernate.connection.release_mode"> auto </prop> <prop key="hibernate.autoReconnect">true</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> <prop key="hibernate.cache.use_second_level_cache">false</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </prop> <prop key="hibernate.cache.use_query_cache">false</prop> </props> </property> </bean>
使用Hibernate時的事務管理配置示例:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name="sessionFactory" ref="sessionFactory" /> bean>
6.動態數據源的管理控制
1.可以根據不同的DAO注入目標sessionfactory
<bean id="demoDao" class="cn.com.dao.impl.demoDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="demoDao1" class="cn.com.dao.impl.demoDao1Impl"> <property name="sessionFactory" ref="sessionFactory1"/> </bean>
2.可以采用代碼手動控制
DBContextHolder.setCustomerType(DBContextHolder.masterDataSource);
DBContextHolder.setCustomerType(DBContextHolder.slaveDataSource);
3.可以采用AOP的控制方式
@Aspect
public class DynamicDataSourceAspect {
@Pointcut("execution (public service.impl..*.*(..))")
public void serviceExecution(){}
@Before("serviceExecution()")
public void setDynamicDataSource(JoinPoint jp) {
for(Object o : jp.getArgs()) {
//處理具體的邏輯 ,根據具體的境況
DBContextHolder.setCustomerType()選取DataSource
}
}
}
7.總結
通過擴展Spring的AbstractRoutingDataSource可以很好的實現多數據源的rout效果,而且對擴展更多的數據源有良好的伸縮 性,只要增加數據源和修改DynamicDataSource的targetDataSources屬性配置就好。
