`
bsr1983
  • 浏览: 1100967 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate结合Spring配置可动态切换的数据源

 
阅读更多

Hibernate结合Spring配置可动态切换的数据源,具体配置如下:

(1)Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<!-- 数据源配置 -->
	<bean id="databaseServer1"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/database1" />
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>
	<bean id="databaseServer2"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/database2" />
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>
	<bean id="databaseServer3"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/database3" />
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>
	<bean id="databaseCenter"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/database_center" />
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>
	<!-- 编写spring 配置文件的配置多数源映射关系 -->  
    <bean id="databaseSource" class="com.ibsrapp.database.datasource.DynamicDataSource">  
        <property name="targetDataSources">  
            <map key-type="java.lang.String">  
                <entry key="APPSERVER1" value-ref="databaseServer1"></entry>  
                <entry key="APPSERVER2" value-ref="databaseServer2"></entry>  
                <entry key="APPSERVER3" value-ref="databaseServer3"></entry>  
                <entry key="APPCENTER" value-ref="databaseCenter"></entry>  
            </map>  
        </property>  
        <property name="defaultTargetDataSource" ref="databaseServer1">  
        </property>  
    </bean>  
	<!--Ibsrapp数据库会话工厂配置 -->
	<bean id="databaseSessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="databaseSource"/>
		<property name="mappingResources">
		    <list>
			    <value>com/ibsrapp/domain/dbtable.hbm.xml</value>	
			</list>
		</property>
		<property name="hibernateProperties">
	        <props>
	            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
	            <prop key="hibernate.show_sql">false</prop>
	            <prop key="hibernate.format_sql">false</prop>
	            <prop key="hibernate.hbm2ddl.auto">true</prop>
	            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> 
	            <!-- 最大连接数 -->
		        <prop key="hibernate.c3p0.max_size">30</prop>
		        <!-- 最小连接数 -->
		        <prop key="hibernate.c3p0.min_size">5</prop>
		        <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
		        <!-- <prop key="hibernate.c3p0.timeout">60000</prop> -->
		        <!-- 最大的PreparedStatement的数量 -->
		        <prop key="hibernate.c3p0.max_statements">100</prop>
		        <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
		        <prop key="hibernate.c3p0.idle_test_period">120</prop>
		        <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
		        <prop key="hibernate.c3p0.acquire_increment">2</prop>
		        <!-- 每次都验证连接是否可用 -->
		        <prop key="hibernate.c3p0.validate">true</prop>
	        </props>
    	</property>
	</bean>
	<!-- database的template -->
	<bean id="databaseHibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="databaseSessionFactory" />
		</property>
	</bean>
  	<bean id="databaseHibernateDao"
		class="com.ibsrapp.database.persistent.dao.impl.IbsrappHibernateDaoImol">
		<property name="hibernateTemplate" ref="databaseHibernateTemplate" />
		<property name="template" ref="databaseHibernateTemplate" />
	</bean>
	<bean id="databaseJdbcTemplate"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="databaseSource" />
	</bean>
	<bean id="databaseJdbcDao" class="com.ibsrapp.database.persistent.dao.impl.IbsrappJdbcDaoImpl">
		<property name="jdbcTemplate" ref="databaseJdbcTemplate" />
		<property name="template" ref="databaseJdbcTemplate"/>
	</bean>
	<bean id="databaseJDBCCommonDao" class="com.ibsrapp.database.persistent.dao.IbsrappJDBCCommonDao">
		<property name="sessionFactory">
			<ref bean="databaseSessionFactory" />
		</property>
	</bean>
	
</beans>

 (2)涉及的Class

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override  
    protected Object determineCurrentLookupKey() {  
        return DynamicDataSourceHolder.getDataSourceType();  
    }

    /**
     * (non-Javadoc)
     * @see javax.sql.CommonDataSource#getParentLogger()
     */
    public Logger getParentLogger() throws SQLFeatureNotSupportedException
    {
        // TODO Auto-generated method stub
        return null;
    }  
}

 

public class DynamicDataSourceHolder
{
    // 线程本地环境  
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
      
    // 设置数据源类型  
    public static void setDataSourceType(String dataSourceType) {  
        contextHolder.set(dataSourceType);  
    }  
      
    // 获取数据源类型  
    public static String getDataSourceType() {  
        return (String) contextHolder.get();  
    }  
      
    // 清除数据源类型  
    public static void clearDataSourceType() {  
        contextHolder.remove();  
    }  
}

 (3)切换相关代码

DynamicDataSourceHolder.setDataSourceType("APPSERVER1");

 这里要使用的就是spring配置文件中所配置的key的字符串。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics