Spring JtaTransactionManager事务管理

 

如果容器支持jta,那么spring配置起来非常简单,因为spring提供了JtaTransactionManager,在这里面提供了几乎所有的服务器的jta的jndi名字,比如
* "java:comp/UserTransaction" for Resin 2.x, Oracle OC4J (Orion), JOnAS (JOTM), BEA WebLogic
* "java:comp/TransactionManager" for Resin 3.x
* "java:appserver/TransactionManager" for GlassFish
* "java:pm/TransactionManager" for Borland Enterprise Server and Sun Application Server (Sun ONE  7 and later)
* "java:/TransactionManager" for JBoss Application Server
 
      由于大部分服务器获取jta的方式都不尽相同,spring还针对不同的服务器提供了不同的jta实现,例如:
      WebLogicJtaTransactionManager
      WebSphereUowTransactionManager
      OC4JJtaTransactionManager
 
      不管是jdbc、hibernate还是ibatis,都可以通过JtaTransactionManager来配置分布式事务
      分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持。如果使用WAS的JTA支持,把它的属性改为WebSphere对应的TransactionManager。
 
    在tomcat下,是没有分布式事务的,不过可以借助于第三方软件jotm(Java Open Transaction Manager )和AtomikosTransactionsEssentials实现,在spring中分布式事务是通过jta(jotm,atomikos)来进行实现。
1.ProductDao.java
package com.spring.jtaTransactionManager;


public interface ProductDao {
	
	public void save(String sql);
	
	public void save(String sqlA, String sqlB);
}
 2.ProductDaoImpl.java
package 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.java
package com.spring.jtaTransactionManager;


public interface ProductManager {
	
	public void save(String sql);
	
	public void save(String sqlA, String sqlB);

}
 4.ProductManagerImpl.java
package 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.java
package 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.properties
database.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,jta)