spring c3p0多数据源怎么配置
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.动态数据源的管理控制
<!-- 主数据库的数据据源 -->
<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.动态数据源的管理控制
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |