日期:2014-05-16  浏览次数:20486 次

Spring + Hibernate+SQL2005 实现两个数据库实例 跨库DAO事务

开发中需要在同一台数据库服务器上,不同的数据库实例之间,执行跨库的事务。本文给出Spring+Hibernate+SQL2005的例子。

?

1 Spring配置文件,applicationContext-db.xml:

??? 拦截的类是PersonDS,方法是transferPerson,

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="placeholderConfig"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:db.properties</value>
		</property>
	</bean>

	<bean id="local_dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${db.driverClassName}" />
		<property name="url" value="${db.url};databaseName=${db.name.local}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>

	<bean id="local_sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="local_dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>edu/dlut/nihao/lvhl/domain/Person.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<!--transactionManager-->
	<bean id="local_transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="local_sessionFactory" />
		</property>
	</bean>

	<bean id="local_transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="local_transactionManager" />
		<property name="transactionAttributeSource" ref="txAttributeSource" />
	</bean>

	<bean id="txAttributeSource"
		class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
		<property name="properties">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="cancel*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="buildXml*">PROPAGATION_REQUIRED</prop>
				<prop key="publish*">PROPAGATION_REQUIRED</prop>
				<prop key="copy*">PROPAGATION_REQUIRED</prop>
				<prop key="list*">PROPAGATION_REQUIRED</prop>
				<prop key="find*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED</prop>
				<prop key="deal*">PROPAGATION_REQUIRED</prop>
				<prop key="search*">PROPAGATION_REQUIRED</prop>
				<prop key="is*">PROPAGATION_REQUIRED</prop>
				<prop key="has*">PROPAGATION_REQUIRED</prop>
				<prop key="checkPass*">PROPAGATION_REQUIRED</prop>
				<prop key="changePass*">PROPAGATION_REQUIRED</prop>
				<prop key="login*">PROPAGATION_REQUIRED</prop>
				<prop key="logout*">PROPAGATION_REQUIRED</prop>
				<prop key="transfer*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">