当前位置: 代码迷 >> Java Web开发 >> spring+hibernate配置多个数据源时,事务如何配置
  详细解决方案

spring+hibernate配置多个数据源时,事务如何配置

热度:7816   发布时间:2016-04-10 22:36:58.0
spring+hibernate配置多个数据源时,事务怎么配置?
如题:

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" 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/jee
           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.mdao">
<context:exclude-filter type="regex" expression="com\.mdao\.framework\.persistence\.hibernate\.Hibernate.*Dao" />
</context:component-scan>

<aop:aspectj-autoproxy />

<tx:annotation-driven transaction-manager="jahwaTxManager" />

<bean id="dataSource" name="emapDataSource" class="com.mdao.framework.database.DbcpDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://192.168.1.10:1433;DatabaseName=emap8" />
<property name="username" value="devadmin" />
<property name="password" value="d123abc" />
<property name="initialSize" value="10" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1" />
<property name="validationQueryTimeout" value="10" />
</bean>

<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath*:mapping/**/*.hbm.xml</value>
<value>classpath*:com/mdao/emap/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.cache.use_structured_entries=true
</value>
</property>
</bean>

<bean id="jahwaDataSource" class="com.mdao.framework.database.DbcpDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://192.168.1.10:1433;DatabaseName=jahwa" />
<property name="username" value="devadmin" />
<property name="password" value="d123abc" />
<property name="initialSize" value="10" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1" />
<property name="validationQueryTimeout" value="10" />
</bean>

<bean id="jahwaSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="jahwaDataSource" />
<property name="mappingLocations">
<list>
<value>classpath*:mapping/**/*.hbm.xml</value>
<value>classpath*:com/mdao/emap/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.cache.use_structured_entries=true
</value>
</property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
  相关解决方案