标签:style blog http color io os 使用 java ar
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性。而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据。
package com.frogking.datasource; public class DataSourceConst { public static final String Admin="Admin"; public static final String User = "User"; }
package com.frogking.datasource;
public class DataSourceContextHolder { private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境 // 设置数据源类型 public static void setDataSourceType(String dataSourceType) { contextHolder.set(dataSourceType); } // 获取数据源类型 public static String getDataSourceType() { return (String) contextHolder.get(); } // 清除数据源类型 public static void clearDataSourceType () { contextHolder.remove(); } }
package com.frogking.datasource;
public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { // 在进行DAO操作前,通过上下文环境变量,获得数据源的类型 return DataSourceContextHolder. getDataSourceType(); } }
<!-- 数据源相同的内容 --> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="parentDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>1234</value> </property> </bean> <!-- 以下配置各个数据源的特性 --> <!-- 数据库test --> <bean parent="parentDataSource" id="adminDataSource"> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> </bean> <!-- 不同的数据库test2 --> <bean parent="parentDataSource" id="userDataSource"> <property name="url"> <value>jdbc:mysql://localhost:3306/test2</value> </property> </bean> <!-- end 配置各个数据源的特性 -->
<bean class="com.frogking.datasource.DynamicDataSource" id="dataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref=" adminDataSource " key="Admin"></entry> <entry value-ref=" userDataSource " key="User"></entry> </map> </property> <property name="defaultTargetDataSource" ref="adminDataSource"></property> </bean>
<!-- sessionFactory的配置 -->
<BEAN class="org".springframework.orm.hibernate3.LocalSessionFactoryBean id=sessionFactory> <property name="dataSource"> <REF local="dataSource"> </REF> </property> <!-- 实体类资源映射 --> <property name="mappingResources"> <LIST> <VALUE>com/frogking/entity/User.hbm.xml </VALUE> <VALUE>com/frogking/entity/Admin.hbm.xml</VALUE> </LIST> </property> <!-- 为sessionFactory 配置Hibernate属性 --> <property name="hibernateProperties"> <PROPS> <PROP key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</PROP> <PROP key="hibernate.show_sql">true</PROP>
<PROP key="hibernate.connection.autocommit">false</PROP>
<PROP key="hibernate.cache.use_query_cache">false</PROP>
<PROP key="hibernate.max_fetch_depth">2</PROP>
<PROP key="hibernate.bytecode.use_reflection_optimizer">true</PROP> </PROPS> </property> </BEAN> <!-- 为dao配置sessionFactory --> <BEAN class="com.frogking.dao.LoginHibernateDao" id=loginDao> <property name="sessionFactory"> <REF local="sessionFactory"> </REF> </property> </BEAN>
标签:style blog http color io os 使用 java ar
原文地址:http://www.cnblogs.com/dreammyle/p/3985253.html