Spring+iBatis+JOTM实现JTA事务
JOTM是个开源的JTA事务管理组件,可以让程序脱离J2EE容器而获得分布式事务管理的能力。
测试过程如下:
一、环境
1、准备软件环境
spring-framework-2.5.6.SEC01-with-dependencies.zip
ibatis-2.3.4
ow2-jotm-dist-2.1.
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,
log4j2 delete 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
│
│ 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
│
│
└─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
l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-////DTD SQL Map 2.0//EN" "/dtd/sql-map-2.dtd" >
<sqlMap namespace="tab_a">
<typeAlias alias="TabA" type="com.lavasoft.ity.TabA"/>
<resultMap id="result_base" class="TabA">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="address" column="address"/>
</resultMap>
<insert id="insert" parameterClass="TabA">
insert into tab_a(
id,
name,
address
) values (
#id#,
#name#,
#address#
)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<update id="update" parameterClass="TabA">
update tab_a set
id = #id#,
name = #name#,
address = #address#
where id = #id#
</update>
<delete id="deleteById" parameterClass="long">
delete from tab_a
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论