Java事务API(Java Transaction API,简称JTA ) 是一个Java企业版 的应用程序接口,在Java环境中,允许完成跨越多个XA资源的分布式事务.
一个分布式的事务涉及一个事务管理器和一个或者多个资源管理器。一个资源管理器是任何类型的持久性的数据存储。事务管理器负责协调所有事务参与者之间的通信。
常见的JTA实现有以下几种:
1.J2EE容器所提供的JTA实现(JBoss中jndi容器事物配置)
2.独立的JTA实现:如JOTM,Atomikos等开源事物管理器
这些实现可以应用在那些不使用J2EE应用服务器的环境里用以提供分布事事务保证。如Tomcat,Jetty以及普通的java应用。
1.pom依赖
<properties>
<spring.version>4.0.2.RELEASEspring.version>
<mybatis.version>3.2.6mybatis.version>
<slf4j.version>1.7.7slf4j.version>
<log4j.version>1.2.17log4j.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.1.41version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-mapper-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.9version>
dependency>
<dependency>
<groupId>com.atomikosgroupId>
<artifactId>transactions-jdbcartifactId>
<version>3.9.2version>
dependency>
<dependency>
<groupId>com.atomikosgroupId>
<artifactId>transactions-hibernate3artifactId>
<version>3.9.2version>
dependency>
<dependency>
<groupId>javax.transactiongroupId>
<artifactId>jtaartifactId>
<version>1.1version>
dependency>
<dependency>
<groupId>oraclegroupId>
<artifactId>ojdbcartifactId>
<version>6version>
dependency>
dependencies>
2.web.xml配置
<web-app id="web" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>applicationdisplay-name>
<distributable/>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springMVC-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
3.spring核心配置文件配置
<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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.xy.service">
context:component-scan>
<context:property-placeholder location="classpath:context/database.properties"/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="oracleDS" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<description>oracle xa datasourcedescription>
<property name="uniqueResourceName">
<value>oracle_dsvalue>
property>
<property name="xaDataSourceClassName">
<value>oracle.jdbc.xa.client.OracleXADataSourcevalue>
property>
<property name="xaProperties">
<props>
<prop key="user">${dev.db.user}prop>
<prop key="password">${dev.db.password}prop>
<prop key="URL">${dev.db.url}prop>
props>
property>
<property name="testQuery">
<value>select 1 from dualvalue>
property>
bean>
<bean id="oracleDS2" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<description>oracle xa datasourcedescription>
<property name="uniqueResourceName">
<value>oracle_ds1value>
property>
<property name="xaDataSourceClassName">
<value>oracle.jdbc.xa.client.OracleXADataSourcevalue>
property>
<property name="xaProperties">
<props>
<prop key="user">${qa.db.user}prop>
<prop key="password">${qa.db.password}prop>
<prop key="URL">${qa.db.url}prop>
props>
property>
<property name="testQuery">
<value>select 1 from dualvalue>
property>
bean>
<bean id="qasqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleDS2" />
<property name="mapperLocations" value="classpath:com/xy/dao/*.xml" />
bean>
<bean id="devsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleDS" />
<property name="mapperLocations" value="classpath:com/xy/daodev/*.xml" />
bean>
<bean id="atomikosUserTransaction"
class="com.atomikos.icatch.jta.UserTransactionImp">
<description>UserTransactionImpdescription>
<property name="transactionTimeout" value="300"/>
bean>
<bean id="atomikosTransactionManager"
class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<description>UserTransactionManagerdescription>
<property name="forceShutdown">
<value>truevalue>
property>
bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<description>JtaTransactionManagerdescription>
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
property>
<property name="allowCustomIsolationLevels" value="true"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xy.dao"/>
<property name="sqlSessionFactoryBeanName" value="qasqlSessionFactory" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xy.daodev"/>
<property name="sqlSessionFactoryBeanName" value="devsqlSessionFactory" />
bean>
beans>
4.spring-MVC配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:annotation-config />
<context:component-scan base-package="com.xy.controller">context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
5.Controller层代码
6.Service层代码
7.Dao层代码
JTA事物区别JDBC事物之处在于可以JDBC事务的范围局限于一个数据库连接。一个 JDBC 事务不能跨越多个数据库,而JTA事物可以接入多个资源管理器,实现事物的统一管理。
JTA编程之ATOMIKOS实现注意事项,要配置ATOMIKOS对应的数据源,对于不同的数据库驱动都要实现XA接口:
oracle数据库:oracle.jdbc.xa.client.OracleXADataSource;
myslq数据库:com.mysql.jdbc.jdbc2.optional.MysqlXADataSource.
配置对应的事物管理器:
atomikosTransactionManager;
atomikosUserTransaction将其注入到spring管理的JTA事物管理器中 org.springframework.transaction.jta.JtaTransactionManager
到此 SMM+ATOMIKOS分布式事务就实现,后续会继续测试SSM+JOTM,以及JTA-容器事物的实现.