n-driven>注解驱动自动进行事务增强:
Java代码
package com.service.impl;
import com.dao.GradeDao;
import com.dao.UserDao;
import com.domain.*;
import org.springframework.transaction.annotation.Transactional;
import com.service.MyService;
@Transactional
public class MyServiceImpl implements MyService {
private UserDao userDao;
private GradeDao gradeDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void setGradeDao(GradeDao gradeDao){
this.gradeDao = gradeDao;
}
@Transactional(readOnly=false)
public void addGrade(User user,Grade grade){
//假如希望两个添加数据的事务,其中有一个添加失败时,均回滚,
//由于两个操作是在两个不同的数据库上进行的,故要JTA事务来进行管理
//否则,将会出现添加一个,回滚一个的情形
gradeDao.addGrade(grade);
userDao.addUser(user);
}
}
package com.service.impl;
import com.dao.GradeDao;
import com.dao.UserDao;
import com.domain.*;
import org.springframework.transaction.annotation.Transactional;
import com.service.MyService;
@Transactional
public class MyServiceImpl implements MyService {
private UserDao userDao;
private GradeDao gradeDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void setGradeDao(GradeDao gradeDao){
this.gradeDao = gradeDao;
}
@Transactional(readOnly=false)
public void addGrade(User user,Grade grade){
//假如希望两个添加数据的事务,其中有一个添加失败时,均回滚,
//由于两个操作是在两个不同的数据库上进行的,故要JTA事务来进行管理
//否则,将会出现添加一个,回滚一个的情形
gradeDao.addGrade(grade);
userDao.addUser(user);
}
}
Spring集成JOTM的JTA事务管理(4)
时间:2011-02-06 mrzhangtufu
6、spring为JOTM提供了一个org.springframework.transaction.jta.JotmFactoryBean 支持类,可以用其方便地创建本地JOTM实例。
具体的配置文件app_jta.xml如下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsp="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsp:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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">
<!--JOTM本地实例-->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<!--JTA事务管理器-->
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
|