spring+jotm 多数据源事务管理(二)hibernate

3 篇文章 0 订阅

spring+jotm 多数据源事务管理系列

spring+jotm 多数据源事务管理(一)jdbc

spring+jotm 多数据源事务管理(二)hibernate

spring+jotm 多数据源事务管理(三)JNDI+Tomcat

 

使用hibernate关联jotm也很方便,先看一个没有使用jotm的例子:

 

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">


    <bean id="dataSource1"  
        class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName">  
            <value>${datasource1.driver}</value>  
        </property>  
        <property name="url">  
            <value>${datasource1.url}</value>  
        </property>  
        <property name="username">  
            <value>${datasource1.username}</value>  
        </property>  
        <property name="password">  
            <value>${datasource1.password}</value>  
        </property>  
        <property name="initialSize">  
            <value>5</value>  
        </property>  
        <property name="maxActive">  
            <value>10</value>  
        </property>  
    </bean>  
      
    <bean id="dataSource2"  
        class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName">  
            <value>${datasource2.driver}</value>  
        </property>  
        <property name="url">  
            <value>${datasource2.url}</value>  
        </property>  
        <property name="username">  
            <value>${datasource2.username}</value>  
        </property>  
        <property name="password">  
            <value>${datasource2.password}</value>  
        </property>  
        <property name="initialSize">  
            <value>5</value>  
        </property>  
        <property name="maxActive">  
            <value>10</value>  
        </property>  
    </bean>  

	<bean id="sessionFactory1"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource1" />
		<property name="mappingLocations" value="${das.mapping.locations}" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.use_sql_comments">
					${hibernate.use_sql_comments}
				</prop>
			</props>
		</property>
	</bean>


	<bean id="sessionFactory2"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource2" />
		<property name="mappingLocations" value="${history.mapping.locations}" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.use_sql_comments">
					${hibernate.use_sql_comments}
				</prop>
			</props>
		</property>
	</bean>


	

	<bean id="transactionManager1"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory1" />
	</bean>
	<bean id="transactionManager2"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory2" />
	</bean>

	<aop:config proxy-target-class="true">
		<aop:pointcut id="servicePoint1"
			expression="execution (* com.xxx.function..service.*.*Service*.*(..))" />
		<aop:advisor pointcut-ref="servicePoint1" id="txAdvisor1"
			advice-ref="txAdvice1" />
	</aop:config>

	<aop:config proxy-target-class="true">
		<aop:pointcut id="servicePoint2"
			expression="execution (* com.xxx.function..service.*.*Service*.*(..))" />
		<aop:advisor pointcut-ref="servicePoint2" id="txAdvisor2"
			advice-ref="txAdvice2" />
	</aop:config>


	<tx:advice id="txAdvice1" transaction-manager="transactionManager1">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="create*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="modify*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="remove*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="apply*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="handle*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>
	<tx:advice id="txAdvice2" transaction-manager="transactionManager2">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="create*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="modify*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="remove*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="apply*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="handle*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
				<tx:method name="do*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>


</beans>

 

    下面看加入jotm的例子:

 

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd"
	default-lazy-init="true">


	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>

	<bean id="dataSource1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="${datasource1.driver}" />
				<property name="url" value="${datasource1.url}" />
			</bean>
		</property>
		<property name="user" value="${datasource1.username}" />
		<property name="password" value="${datasource1.password}" />
	</bean>

	<bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
		destroy-method="shutdown">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
				destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="${datasource2.driver}" />
				<property name="url" value="${datasource2.url}" />
			</bean>
		</property>
		<property name="user" value="${datasource2.username}" />
		<property name="password" value="${datasource2.password}" />
	</bean>


	

	<bean id="sessionFactory1"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource1" />
		<property name="mappingLocations" value="${hibernate.mapping.locations}" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.use_sql_comments">
					${hibernate.use_sql_comments}
				</prop>
			</props>
		</property>
	</bean>

	
	<bean id="sessionFactory2"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource2" />
		<property name="mappingLocations" value="${hibernate.mapping.special}" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.use_sql_comments">
					${hibernate.use_sql_comments}
				</prop>
			</props>
		</property>
	</bean>

	<aop:config proxy-target-class="true">
		<aop:pointcut id="servicePoint"
			expression="execution (* com.xxx.function.*.service.*.*Service*.*(..))" />
		<aop:advisor pointcut-ref="servicePoint" id="txAdvisor"
			advice-ref="defaultTxAdvice" />
	</aop:config>

	<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="create*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="modify*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="remove*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="apply*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="handle*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>

	

</beans>  

  ok。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值