Spring怎样高效的配置多套数据源

发布网友 发布时间:2022-04-23 14:36

我来回答

1个回答

热心网友 时间:2022-04-09 14:43

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>

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com