?
package com.spring.jtaTransactionManager;
public interface ProductDao {
public void save(String sql);
public void save(String sqlA, String sqlB);
}
?2.ProductDaoImpl.javapackage com.spring.jtaTransactionManager;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("productDao")
public class ProductDaoImpl implements ProductDao {
@Resource
private JdbcTemplate jdbcTemplateA;
@Resource
private JdbcTemplate jdbcTemplateB;
@Override
public void save(String sql) {
jdbcTemplateA.execute(sql);
}
@Override
public void save(String sqlA, String sqlB) {
jdbcTemplateA.execute(sqlA);
jdbcTemplateB.execute(sqlB);
}
}
?3.ProductManager.javapackage com.spring.jtaTransactionManager;
public interface ProductManager {
public void save(String sql);
public void save(String sqlA, String sqlB);
}
?4.ProductManagerImpl.javapackage com.spring.jtaTransactionManager;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("productManager")
public class ProductManagerImpl implements ProductManager{
@Resource
private ProductDao productDao;
@Override
public void save(String sql) {
productDao.save(sql);
}
@Override
public void save(String sqlA, String sqlB) {
productDao.save(sqlA, sqlB);
}
}
?5.SimpleSpringJtaDemo.javapackage com.spring.jtaTransactionManager;
import java.sql.SQLException;
import javax.naming.NamingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SimpleSpringJtaDemo {
public static void main(String[] args) throws NamingException, SQLException {
ApplicationContext app = new FileSystemXmlApplicationContext("src/com/spring/jtaTransactionManager/spring-jta.xml");
ProductManager p = (ProductManager)app.getBean("productManager");
String sqlA = "INSERT INTO t_product VALUES (1, ‘Jta测试事务‘);";
String sqlB = "INSERT INTO t_receiver VALUES (1,‘Jta测试事务‘);";
p.save(sqlA, sqlB);
}
}
?6.configuration.propertiesdatabase.driver=com.mysql.jdbc.Driver database.url=jdbc:mysql://127.0.0.1/jinhonglun?useEncoding=true&characterEncoding=UTF-8 database.username=root database.password=root hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.show_sql=true database2.driver=com.mysql.jdbc.Driver database2.url=jdbc:mysql://127.0.0.1/winchannel?useEncoding=true&characterEncoding=UTF-8 database2.username=root database2.password=root hibernate2.dialect=org.hibernate.dialect.MySQL5Dialect hibernate2.show_sql=true?7.spring-jta.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<description>springJTA</description>
<!-- 自动扫描包,自动将@Repository、@Service、@Controller 和 @Component自动实例化 -->
<context:component-scan base-package="com.spring.jtaTransactionManager" />
<!--指定Spring配置中用到的属性文件-->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/com/spring/jtaTransactionManager/configuration.properties</value>
</list>
</property>
</bean>
<!-- JOTM实例 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean">
<property name="defaultTimeout" value="500000"/>
</bean>
<!-- JTA事务管理器 -->
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<!-- 数据源A -->
<bean id="dataSourceA" 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="${database.driver}"/>
<property name="url" value="${database.url}"/>
</bean>
</property>
<property name="user" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
<!-- 数据源B -->
<bean id="dataSourceB" 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="${database2.driver}"/>
<property name="url" value="${database2.url}"/>
</bean>
</property>
<property name="user" value="${database2.username}"/>
<property name="password" value="${database2.password}"/>
</bean>
<bean id = "jdbcTemplateA"
class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSourceA"/>
</bean>
<bean id = "jdbcTemplateB"
class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSourceB"/>
</bean>
<!-- 事务切面配置 -->
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.spring.jtaTransactionManager..*.*(..))"/><!-- 包及其子包下的所有方法 -->
<aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/>
</aop:config>
<!-- 通知配置 -->
<tx:advice id="txAdvice" transaction-manager="jtaTransactionManager">
<tx:attributes>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="find*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
</beans>
?后续还会,补充对应的其他hibernate的JtaTransactionManager处理方式Spring JtaTransactionManager事务管理
原文:http://tuoni.iteye.com/blog/2175074