首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring+Hibernate兑现动态SessionFactory切换(改进版)

2012-11-22 
Spring+Hibernate实现动态SessionFactory切换(改进版)前面写了一篇关于动态切换Hibernate SessionFactory

Spring+Hibernate实现动态SessionFactory切换(改进版)

前面写了一篇关于动态切换Hibernate SessionFactory的文章,原文地址:http://tangyanbo.iteye.com/admin/blogs/1717402

发现存在一些问题:
需要配置多个HibernateTransactionManager和多个Spring 切面
这样带来两个问题
1. 程序效率降低,因为Spring进行多次Advice的拦截
2. 如果其中一个SessionFactory连接出现问题,会导致整个系统无法工作

今天研究出一种新的方法来解决此类问题
1. 数据源及Hibernate SessionFactory配置:

?

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- FOR SqlServer--> <bean id="SqlServer_DataSource" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="url" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean> <bean id="SqlServer_SessionFactory" p:mappingLocations="classpath:/com/entity/*.hbm.xml"> <property name="dataSource" ref="SqlServer_DataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- FOR Oracle --> <bean id="Oracle _DataSource" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521/orcl" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean> <bean id="Oracle_SessionFactory" p:mappingLocations="classpath:/com/entity/*.hbm.xml"> <property name="dataSource" ref="Oracle_DataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> </beans> ?

?

2. 定义扩展接口DynamicSessionFactoryInf继承SessionFactory

?

?

          <bean id="dynamicTransactionManager"ref="sessionFactory"/></bean>

          ?

          ?

          7.?为SessionFactory配置事务切面

          <tx:advice id="dynamicTxAdvice" transaction-manager="dynamicTransactionManager">          <tx:attributes>              <tx:method name="get*" read-only="true" />              <tx:method name="find*" read-only="true" />              <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />          </tx:attributes>      </tx:advice>                  <aop:config proxy-target-expression="execution(* com.service.*.*(..))"/>                  <aop:advisor advice-ref="dynamicTxAdvice" pointcut-ref="txPointcut" />     </aop:config>  
          ?

热点排行