---
title: "Spring+iBatis+JOTM实现JTA事务"
author: "baggioback"
date: "2012-07-12T15:10:43Z"
tags: []
categories: ["开源软件"]
slug: "spring-ibatis-jotm-jta"
draft: false
lastmod: "2012-07-12T15:10:43Z"
id: 1586622
url: "https://baggioback.iteye.com/blog/1586622"
---

JOTM是个开源的JTA事务管理组件，可以让程序脱离J2EE容器而获得分布式事务管理的能力。

 

测试过程如下：

 

**一、环境**

 

1、准备软件环境

 spring-framework-2.5.6.SEC01-with-dependencies.zip  
 ibatis-2.3.4  
 ow2-jotm-dist-2.1.4-bin.tar.gz  
 MySQL-5.1  
 JDK1.5

 

2、创建数据库环境，注意数据库引擎为InnoDB，只有这样才能支持事务。

 

CREATE DATABASE IF NOT EXISTS testdb\_a    DEFAULT CHARACTER SET utf8;

USE testdb\_a;

DROP TABLE IF EXISTS tab\_a;

CREATE TABLE tab\_a (  
    id bigint (20) NOT NULL ,  
    name varchar (60) DEFAULT NULL ,  
    address varchar (120) DEFAULT NULL ,  
    PRIMARY KEY (id)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE DATABASE IF NOT EXISTS testdb\_b    DEFAULT CHARACTER SET utf8;

USE testdb\_b;

DROP TABLE IF EXISTS tab\_b;

CREATE TABLE tab\_b (  
    id bigint (20) NOT NULL ,  
    name varchar (60) DEFAULT NULL ,  
    address varchar (120) DEFAULT NULL ,  
    PRIMARY KEY (id)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

**二、建立项目testJOTM**

 

1、建立项目后，准备依赖的类库，结构如下：

│    spring-aop.jar  
│    spring-beans.jar  
│    spring-context-support.jar  
│    spring-context.jar  
│    spring-core.jar  
│    spring-jdbc.jar  
│    spring-jms.jar  
│    spring-orm.jar  
│    spring-test.jar  
│    spring-tx.jar  
│    spring-web.jar  
│    spring-webmvc-portlet.jar  
│    spring-webmvc-struts.jar  
│    spring-webmvc.jar  
│    aspectjrt.jar  
│    aspectjweaver.jar  
│    cglib-nodep-2.1\_3.jar  
│    asm-2.2.3.jar  
│    log4j-1.2.15.jar  
│    asm-commons-2.2.3.jar  
│    asm-util-2.2.3.jar  
│    aopalliance.jar  
│    mysql-connector-java-5.1.6-bin.jar  
│  
├─ibatis  
│            ibatis-2.3.4.726.jar  
│            sql-map-2.dtd  
│            sql-map-config-2.dtd  
│  
├─jotm  
│            license.txt  
│            xapool.jar  
│            jotm-core.jar  
│            jotm-standalone.jar  
│            jotm-jms.jar  
│            jotm-datasource.jar  
│            ow2-jta-1.1-spec.jar  
│            jotm-client.jar  
│  
├─jakarta-commons  
│            commons-attributes-api.jar  
│            commons-attributes-compiler.jar  
│            commons-beanutils.jar  
│            commons-codec.jar  
│            commons-collections.jar  
│            commons-dbcp.jar  
│            commons-digester.jar  
│            commons-discovery.jar  
│            commons-fileupload.jar  
│            commons-httpclient.jar  
│            commons-io.jar  
│            commons-lang.jar  
│            commons-logging.jar  
│            commons-pool.jar  
│            commons-validator.jar  
│  
├─junit  
│            junit-3.8.2.jar  
│            junit-4.4.jar  
│            license.txt  
│  
└─log4j  
                log4j-1.2.15.jar

 

2、根据表建立entity和SQLMap

public class TabA implements Serializable {  
        private Long id;  
        private String name;  
        private String address;  
        //省略getter/setter

 

public class TabB implements Serializable {  
        private Long id;  
        private String name;  
        private String address;  
        //省略getter/setter

TabA.xml

&lt;? xml version ="1.0" encoding ="UTF-8" ?&gt;  
&lt;!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" &gt;

&lt;!-- 表名:tab\_a --&gt;  
&lt; sqlMap namespace ="tab\_a" &gt;  
        &lt; typeAlias alias ="TabA" type ="com.lavasoft.stu.jtom.entity.TabA" /&gt;  
        &lt; resultMap id ="result\_base" class ="TabA" &gt;  
                &lt; result property ="id" column ="id" /&gt;  
                &lt; result property ="name" column ="name" /&gt;  
                &lt; result property ="address" column ="address" /&gt;  
        &lt;/ resultMap &gt;  
        &lt;!-- 添加 --&gt;  
        &lt; insert id ="insert" parameterClass ="TabA" &gt;  
                insert into tab\_a(  
                id,  
                name,  
                address  
                ) values (  
                #id#,  
                #name#,  
                #address#  
                )  
                &lt; selectKey keyProperty ="id" resultClass ="long" &gt;  
                        select LAST\_INSERT\_ID()  
                &lt;/ selectKey &gt;  
        &lt;/ insert &gt;  
        &lt;!-- 更新 --&gt;  
        &lt; update id ="update" parameterClass ="TabA" &gt;  
                update tab\_a set  
                id = #id#,  
                name = #name#,  
                address = #address#  
                where id = #id#  
        &lt;/ update &gt;  
        &lt;!-- 删除 --&gt;  
        &lt; delete id ="deleteById" parameterClass ="long" &gt;  
                delete from tab\_a  
                where id = #value#  
        &lt;/ delete &gt;  
        &lt;!-- 根据ID获取 --&gt;  
        &lt; select id ="findById" parameterClass ="long" resultMap ="tab\_a.result\_base" &gt;  
                select *  
                from tab\_a  
                where id = #value#  
        &lt;/ select &gt;  
&lt;/ sqlMap &gt;

 

TabB.xml

&lt;? xml version ="1.0" encoding ="UTF-8" ?&gt;  
&lt;!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" &gt;

&lt;!-- 表名:tab\_b --&gt;  
&lt; sqlMap namespace ="tab\_b" &gt;  
        &lt; typeAlias alias ="TabB" type ="com.lavasoft.stu.jtom.entity.TabB" /&gt;  
        &lt; resultMap id ="result\_base" class ="TabB" &gt;  
                &lt; result property ="id" column ="id" /&gt;  
                &lt; result property ="name" column ="name" /&gt;  
                &lt; result property ="address" column ="address" /&gt;  
        &lt;/ resultMap &gt;  
        &lt;!-- 添加 --&gt;  
        &lt; insert id ="insert" parameterClass ="TabB" &gt;  
                insert into tab\_b(  
                id,  
                name,  
                address  
                ) values (  
                #id#,  
                #name#,  
                #address#  
                )  
                &lt; selectKey keyProperty ="id" resultClass ="long" &gt;  
                        select LAST\_INSERT\_ID()  
                &lt;/ selectKey &gt;  
        &lt;/ insert &gt;  
        &lt;!-- 更新 --&gt;  
        &lt; update id ="update" parameterClass ="TabB" &gt;  
                update tab\_b set  
                id = #id#,  
                name = #name#,  
                address = #address#  
                where id = #id#  
        &lt;/ update &gt;  
        &lt;!-- 删除 --&gt;  
        &lt; delete id ="deleteById" parameterClass ="long" &gt;  
                delete from tab\_b  
                where id = #value#  
        &lt;/ delete &gt;  
        &lt;!-- 根据ID获取 --&gt;  
        &lt; select id ="findById" parameterClass ="long" resultMap ="tab\_b.result\_base" &gt;  
                select *  
                from tab\_b  
                where id = #value#  
        &lt;/ select &gt;  
&lt;/ sqlMap &gt;

 

/\**  
\* TabADAO  
\*  
\* @author leizhimin 2009-6-25 12:39:19  
\*/  
public interface TabADAO {

        /\**  
         * 保存一个TabA对象  
         *  
         * @param tabA TabA对象  
         * @return 返回保存后的对象  
         \*/  
        TabA saveTabA(TabA tabA);

        /\**  
         * 更新一个TabA  
         *  
         * @param tabA TabA对象  
         * @return 返回更新后的对象  
         \*/  
        TabA updateTabA(TabA tabA);

        /\**  
         * 删除指定标识的一个TabA  
         *  
         * @param id TabA标识  
         \*/  
        void deleteTabAById(Long id);

        /\**  
         * 获取指定标识的TabA  
         *  
         * @param id TabA标识  
         * @return 所查询到的TabA  
         \*/  
        TabA findTabAById(Long id);  
}

 

/\**  
\* TabADAO  
\*  
\* @author leizhimin 2009-6-25 12:43:55  
\*/  
public class TabADAOImpl extends SqlMapClientDaoSupport implements TabADAO {

        /\**  
         * 保存一个TabA对象  
         *  
         * @param tabA TabA对象  
         * @return 返回保存后的对象  
         \*/  
        public TabA saveTabA(TabA tabA) {  
                Long id = (Long) getSqlMapClientTemplate().insert("tab\_a.insert" , tabA);  
                tabA.setId(id);  
                return tabA;  
        }

        /\**  
         * 更新一个TabA  
         *  
         * @param tabA TabA对象  
         * @return 返回更新后的对象  
         \*/  
        public TabA updateTabA(TabA tabA) {  
                getSqlMapClientTemplate().update("tab\_a.update" , tabA);  
                return tabA;  
        }

        /\**  
         * 删除指定标识的一个TabA  
         *  
         * @param id TabA标识  
         \*/  
        public void deleteTabAById(Long id) {  
                getSqlMapClientTemplate().delete("tab\_a.deleteById" ,id);  
        }

        /\**  
         * 获取指定标识的TabA  
         *  
         * @param id TabA标识  
         * @return 所查询到的TabA  
         \*/  
        public TabA findTabAById(Long id) {  
                return (TabA) getSqlMapClientTemplate().queryForObject("tab\_a.findById" ,id);  
        }  
}

 

B的DAO和A类似，就不写了。

 

/\**  
\* 测试JOTM的Service  
\*  
\* @author leizhimin 2009-6-25 12:53:55  
\*/  
public interface StuJotmService {  
        /\**  
         * 同时保存TabA、TabB  
         *  
         * @param a TabA对象  
         * @param b TabB对象  
         \*/  
        void saveAB(TabA a, TabB b);

        /\**  
         * 同时更新TabA、TabB  
         *  
         * @param a TabA对象  
         * @param b TabB对象  
         \*/  
        void updateAB(TabA a, TabB b);

        /\**  
         * 删除指定id的TabA、TabB记录  
         *  
         * @param id 指定id  
         \*/  
        void deleteABif(Long id);  
}

 

/\**  
\* Created by IntelliJ IDEA.  
\*  
\* @author leizhimin 2009-6-25 12:58:48  
\*/  
//@Transactional  
public class StuJotmServiceImpl implements StuJotmService {  
        private TabADAO tabADAO;  
        private TabBDAO tabBDAO;

        /\**  
         * 同时保存TabA、TabB  
         *  
         * @param a TabA对象  
         * @param b TabB对象  
         \*/  
//        @Transactional(readOnly=false)  
        public void saveAB(TabA a, TabB b) {  
                tabADAO.saveTabA(a);  
                tabBDAO.saveTabB(b);  
        }

        /\**  
         * 同时更新TabA、TabB  
         *  
         * @param a TabA对象  
         * @param b TabB对象  
         \*/  
//        @Transactional(readOnly=false)  
        public void updateAB(TabA a, TabB b) {  
                tabADAO.updateTabA(a);  
                tabBDAO.updateTabB(b);  
        }

        /\**  
         * 删除指定id的TabA、TabB记录  
         *  
         * @param id 指定id  
         \*/  
//        @Transactional(readOnly=false)  
        public void deleteABif(Long id) {  
                tabADAO.deleteTabAById(id);  
                tabBDAO.deleteTabBById(id);  
        }

        public void setTabADAO(TabADAO tabADAO) {  
                this .tabADAO = tabADAO;  
        }

        public void setTabBDAO(TabBDAO tabBDAO) {  
                this .tabBDAO = tabBDAO;  
        }  
}

 

/\**  
\* Spring上下文工具  
\*  
\* @author leizhimin 2008-8-13 14:42:58  
\*/

public class ApplicationContextUtil {  
        private static ApplicationContext applicationContext;

        static {  
                if (applicationContext == null )  
                        applicationContext = rebuildApplicationContext();  
        }

        /\**  
         * 重新构建Spring应用上下文环境  
         *  
         * @return ApplicationContext  
         \*/  
        public static ApplicationContext rebuildApplicationContext() {  
                return new ClassPathXmlApplicationContext("/ApplicationContext.xml" );  
        }

        /\**  
         * 获取Spring应用上下文环境  
         *  
         * @return  
         \*/  
        public static ApplicationContext getApplicationContext() {  
                return applicationContext;  
        }

        /\**  
         * 简单的上下文环境测试  
         \*/  
        public static void main(String\[] args) {  
                rebuildApplicationContext();  
                if (applicationContext == null ) {  
                        System.out.println("ApplicationContext is null " );  
                } else {  
                        System.out.println("ApplicationContext is not null !" );  
                }  
        }  
}

 

**三、做JTOM、Spring、iBatis、Log4j等配置**

 

JOTM配置：carol.properties

#JNDI调用协议  
carol.protocols=jrmp  
#不使用CAROL JNDI封装器          
carol.start.jndi=false  
#不启动命名服务器  
carol.start.ns=false

 

Spring配置：ApplicationContext.xml

&lt;? xml version ="1.0" encoding ="UTF-8" ?&gt;  
&lt;!-- 局部单元测试使用，不正式发布，不要删除 --&gt;  
&lt; beans xmlns ="http://www.springframework.org/schema/beans"  
             xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  
             xmlns:jee ="http://www.springframework.org/schema/jee"  
             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-2.0.xsd  
                     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd  
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;

        &lt;!--指定Spring配置中用到的属性文件 --&gt;  
        &lt; bean id ="propertyConfig"  
                    class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" &gt;  
                &lt; property name ="locations" &gt;  
                        &lt; list &gt;  
                                &lt; value &gt; classpath:jdbc.properties&lt;/ value &gt;  
                        &lt;/ list &gt;  
                &lt;/ property &gt;  
        &lt;/ bean &gt;  
        &lt;!-- JOTM实例 --&gt;  
        &lt; bean id ="jotm" class ="org.springframework.transaction.jta.JotmFactoryBean" /&gt;  
        &lt;!-- JTA事务管理器 --&gt;  
        &lt; bean id ="myJtaManager"  
                    class ="org.springframework.transaction.jta.JtaTransactionManager" &gt;  
                &lt; property name ="userTransaction" &gt;  
                        &lt; ref local ="jotm" /&gt;  
                &lt;/ property &gt;  
        &lt;/ bean &gt;  
        &lt;!-- 数据源A --&gt;  
        &lt; bean id ="dataSourceA" class ="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method ="shutdown" &gt;  
                &lt; property name ="dataSource" &gt;  
                        &lt; bean class ="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method ="shutdown" &gt;  
                                &lt; property name ="transactionManager" ref ="jotm" /&gt;  
                                &lt; property name ="driverName" value ="${jdbc.driver}" /&gt;  
                                &lt; property name ="url" value ="${jdbc.url}" /&gt;  
                        &lt;/ bean &gt;  
                &lt;/ property &gt;  
                &lt; property name ="user" value ="${jdbc.username}" /&gt;  
                &lt; property name ="password" value ="${jdbc.password}" /&gt;  
        &lt;/ bean &gt;  
        &lt;!-- 数据源B --&gt;  
        &lt; bean id ="dataSourceB" class ="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method ="shutdown" &gt;  
                &lt; property name ="dataSource" &gt;  
                        &lt; bean class ="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method ="shutdown" &gt;  
                                &lt; property name ="transactionManager" ref ="jotm" /&gt;  
                                &lt; property name ="driverName" value ="${jdbc2.driver}" /&gt;  
                                &lt; property name ="url" value ="${jdbc2.url}" /&gt;  
                        &lt;/ bean &gt;  
                &lt;/ property &gt;  
                &lt; property name ="user" value ="${jdbc2.username}" /&gt;  
                &lt; property name ="password" value ="${jdbc.password}" /&gt;  
        &lt;/ bean &gt;  
        &lt;!-- 事务切面配置 --&gt;  
        &lt; aop:config &gt;  
                &lt; aop:pointcut id ="serviceOperation"  
                                            expression ="execution(* \*..servi1ce\*..\*(..))" /&gt;  
                &lt; aop:advisor pointcut-ref ="serviceOperation"  
                                         advice-ref ="txAdvice" /&gt;  
        &lt;/ aop:config &gt;  
        &lt;!-- 通知配置 --&gt;  
        &lt; tx:advice id ="txAdvice" transaction-manager ="myJtaManager" &gt;  
                &lt; tx:attributes &gt;  
                        &lt; tx:method name ="delete\*" rollback-for ="Exception" /&gt;  
                        &lt; tx:method name ="save\*" rollback-for ="Exception" /&gt;  
                        &lt; tx:method name ="update\*" rollback-for ="Exception" /&gt;  
                        &lt; tx:method name ="\*" read-only ="true" rollback-for ="Exception" /&gt;  
                &lt;/ tx:attributes &gt;  
        &lt;/ tx:advice &gt;

        &lt;!--根据dataSourceA和sql-map-config\_A.xml创建一个SqlMapClientA --&gt;  
        &lt; bean id ="sqlMapClientA"  
                    class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" &gt;  
                &lt; property name ="dataSource" &gt;  
                        &lt; ref local ="dataSourceA" /&gt;  
                &lt;/ property &gt;  
                &lt; property name ="configLocation" &gt;  
                        &lt; value &gt; sql-map-config\_A.xml&lt;/ value &gt;  
                &lt;/ property &gt;  
        &lt;/ bean &gt;  
        &lt;!--根据dataSourceB和sql-map-config\_B.xml创建一个SqlMapClientB --&gt;  
        &lt; bean id ="sqlMapClientB"  
                    class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" &gt;  
                &lt; property name ="dataSource" &gt;  
                        &lt; ref local ="dataSourceB" /&gt;  
                &lt;/ property &gt;  
                &lt; property name ="configLocation" &gt;  
                        &lt; value &gt; sql-map-config\_B.xml&lt;/ value &gt;  
                &lt;/ property &gt;  
        &lt;/ bean &gt;  
        &lt;!--根据sqlMapClientA创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateA --&gt;  
        &lt; bean id ="sqlMapClientTemplateA"  
                    class ="org.springframework.orm.ibatis.SqlMapClientTemplate" &gt;  
                &lt; property name ="sqlMapClient" ref ="sqlMapClientA" /&gt;  
        &lt;/ bean &gt;  
        &lt;!--根据sqlMapClientB创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateB --&gt;  
        &lt; bean id ="sqlMapClientTemplateB"  
                    class ="org.springframework.orm.ibatis.SqlMapClientTemplate" &gt;  
                &lt; property name ="sqlMapClient" ref ="sqlMapClientB" /&gt;  
        &lt;/ bean &gt;

        &lt;!-- 配置DAO，并注入所使用的sqlMapClientTemplate实例 --&gt;  
        &lt; bean id ="tabADAO" class ="com.lavasoft.stu.jtom.dao.impl.TabADAOImpl" &gt;  
                &lt; property name ="sqlMapClientTemplate" ref ="sqlMapClientTemplateA" /&gt;  
        &lt;/ bean &gt;  
        &lt; bean id ="tabBDAO" class ="com.lavasoft.stu.jtom.dao.impl.TabBDAOImpl" &gt;  
                &lt; property name ="sqlMapClientTemplate" ref ="sqlMapClientTemplateB" /&gt;  
        &lt;/ bean &gt;

        &lt;!-- Service配置，注入DAO --&gt;  
        &lt; bean id ="stuJotmService" class ="com.lavasoft.stu.jtom.service.StuJotmServiceImpl" &gt;  
                &lt; property name ="tabADAO" ref ="tabADAO" /&gt;  
                &lt; property name ="tabBDAO" ref ="tabBDAO" /&gt;  
        &lt;/ bean &gt;  
&lt;/ beans &gt;

 

数据库配置：jdbc.properties

jdbc.database=cms\_release  
jdbc.driver=com.mysql.jdbc.Driver  
jdbc.url=jdbc:mysql://192.168.0.2:3306/testdb\_a?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc.username=root  
jdbc.password=leizhimin

jdbc2.database=cms\_release  
jdbc2.driver=com.mysql.jdbc.Driver  
jdbc2.url=jdbc:mysql://192.168.0.1:3306/testdb\_b?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc2.username=root  
jdbc2.password=leizhimin

 

iBatis的SQLMap配置：

sql-map-config\_A.xml

&lt;? xml version ="1.0" encoding ="UTF-8" ?&gt;  
&lt;!DOCTYPE sqlMapConfig  
                PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
                "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"&gt;

&lt; sqlMapConfig &gt;  
        &lt; settings cacheModelsEnabled ="true" enhancementEnabled ="true"  
                            lazyLoadingEnabled ="true" errorTracingEnabled ="true"  
                            useStatementNamespaces ="true" /&gt;

        &lt; sqlMap resource ="com/lavasoft/stu/jtom/entity/sqlmap/TabA.xml" /&gt;

&lt;/ sqlMapConfig &gt;

 

sql-map-config\_B.xml

&lt;? xml version ="1.0" encoding ="UTF-8" ?&gt;  
&lt;!DOCTYPE sqlMapConfig  
                PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
                "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"&gt;

&lt; sqlMapConfig &gt;  
        &lt; settings cacheModelsEnabled ="true" enhancementEnabled ="true"  
                            lazyLoadingEnabled ="true" errorTracingEnabled ="true"  
                            useStatementNamespaces ="true" /&gt;

        &lt; sqlMap resource ="com/lavasoft/stu/jtom/entity/sqlmap/TabB.xml" /&gt;

&lt;/ sqlMapConfig &gt;

 

日志的配置：log4j.properties

log4j.rootLogger=INFO,CONSOLE,LOGFILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender  
log4j.appender.Threshold=INFO  
log4j.appender.CONSOLE.Target=System.out  
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout  
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss } - %-5p %c        %x - %m%n

log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender  
log4j.appender.LOGFILE.File=contestlog.log  
log4j.appender.LOGFILE.MaxFileSize=500KB  
log4j.appender.LOGFILE.MaxBackupIndex=10  
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout  
log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss } -    %-p %c     %x - %m%n

log4j.logger.com.ibatis=INFO  
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO  
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO  
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO  
log4j.logger.java.sql.Connection=debug  
log4j.logger.java.sql.Statement=INFO,CONSOLE  
log4j.logger.java.sql.PreparedStatement=INFO,CONSOLE

 

**四、测试**

 

public class Test {  
        private static ApplicationContext ctx = ApplicationContextUtil.getApplicationContext();  
        private static StuJotmService ser = (StuJotmService) ctx.getBean("stuJotmService" );

        public static void test\_() {  
                TabA a = new TabA();  
                a.setId(1L);  
                a.setName("aaa" );  
                a.setAddress("address a" );  
                TabB b = new TabB();  
                b.setId(1L);  
                b.setName("bbb" );  
                b.setAddress("address b" );  
                ser.saveAB(a, b);  
        }

        public static void main(String\[] args) {  
                test\_();  
        }  
}

 

然后将主键其中一个改掉进行测试，让另外一个主键重复，发现a、b数据库均不能成功写入，经过多次测试，验证jtom真正实现了分布式事务的管理。