spring c3p0多数据源怎么配置

 我来答
牛永安0HQ
2016-08-29 · 知道合伙人互联网行家
牛永安0HQ
知道合伙人互联网行家
采纳数:1224 获赞数:3261
启明教育老师

向TA提问 私信TA
展开全部
  首先配置多个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.动态数据源的管理控制
  
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式