使用org.springframework.transaction.support.TransactionTemplate类,
TransactionTemplate封装了事务管理的功能,包括异常时的事务回滚,以及操作成
功后的事务提交。在doInTransaction中进行的操作,如果抛出未捕获异常将被自动回滚,
如果成功执行,则将被自动提交。使用TransactionTemplate类,必须实现TransactionCallback接口,
transactionCallback只有一个实现方法,使用匿名内部类,doInTransaction()处理事务。
类似与JDbcTemplate,Callback类的编写违背很多人的编程习惯,传进匿名类之前要改成final。
配置文件
<bean id="transactionTemplate" class="org.springframework.
transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean>
DAO中
<property name=" transactionTemplate">
<ref bean=" transactionTemplate"/>
</property>
使用TransactionTemplate的doInTransaction() 不能抛出任何checked exceptions,必须将异常包装为unchecked exceptions,
因为TransactionTemplate不支持throws
下面是代码
transactionTemplate.execute(
new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
try {
// do stuff
} catch (Exception e) {
ts.setRollbackOnly();
}
return null;
}
}
);
使用PlatformTransactionManager效率更高
package module;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.dao.DataAccessException;
import javax.sql.DataSource;
public class testTranORacleDAO {
private static DataSource dataSource;
private static PlatformTransactionManager transactionManager;
private static JdbcTemplate jt;
public static void main(String[] args) {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:ILoveMM");
dataSource.setUsername("system");
dataSource.setPassword("manager");
DataSourceTransactionManager tm = new DataSourceTransactionManager();
tm.setDataSource(dataSource);
transactionManager = tm;
doUpdate(dataSource);
}
public static void doUpdate(DataSource dataSource) {
DefaultTransactionDefinition td = new DefaultTransactionDefinition(
TransactionDefinition.PROPAGATION_REQUIRED);
td.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
td.setTimeout(10);
TransactionStatus status = transactionManager.getTransaction(td);
try {
update(dataSource);
} catch (DataAccessException e) {
transactionManager.rollback(status);
throw e;
}
transactionManager.commit(status);
}
public static void update(DataSource dataSource) {
jt = new JdbcTemplate(dataSource);
jt.update("delete from beers where id=2 ");
jt.update("insert into beers(id,BRAND,price) values(1,'3',100)");
}
}
分享到:
相关推荐
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
演示了spring编程式事务的实现,通过TransactionTemplate模板进行事务控制
Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可
Spring 声明式事务和Spring 编程式事务
全面分析_Spring_的编程式事务管理及声明式事务管理.
Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握。本教程从基础知识开始,详细分析了 Spring 事务管理的使用方法,为读者理清思路。
全面分析_Spring_的编程式事务管理及声明式事务管理,本教程假定您已经掌握了 Java 基础知识,并对 Spring 有一定了解。您还需要具备基本的事务管理的知识,比如:事务的定义,隔离级别的概念,等等。本文将直接使用...
spring编程式事务与声明式事务详解,超详细!
Spring事务原理是指Spring框架中的一种机制,用于管理事务,并提供了多种配置方式。事务是指一系列的操作,作为一个整体执行,如果其中某个操作失败,整个事务将回滚。Spring事务原理围绕着两个核心:...
Spring声明式事务让我们从复杂的事务处理中得到解脱。使得我们再也无需要去处理获得连接、关闭连接、事务提交和回滚等这些操作。再也无需要我们在与事务相关的方法中处理大量的try…catch…finally代码。
Spring 提供了多种方式来实现编程式事务管理,例如使用 TransactionTemplate、TransactionInterceptor 等。 六、声明式事务管理 声明式事务管理是指通过配置文件或注解的方式来管理事务的提交和回滚。 Spring ...
这种方法只需要在 Spring 配置文件中定义一个事务管理对象(如 DataSourceTransactionManager),然后加入 `<tx:annotation-driven/>` 节点,引用该事务管理对象,然后即可在需要进行事务处理的类和方法使用 `@...
NULL 博文链接:https://konglx.iteye.com/blog/1836826
相信对于很多初学者或者使用者,都了解spring事务,但是具体细节,实现方式,都没有系统的概念或者无从下手,本人周末闲来无事,坐下总结,时间紧促,写的不周全的地方还请提出宝贵意见。
Spring 中的事务处理可以分为两种方式:声明式事务处理和编程式事务处理。声明式事务处理通过 AOP 的实现,把事务管理代码作为方面封装到业务代码中,使得事务管理代码和业务代码解藕。这使得事务管理变得更加灵活...
Spring的事务框架将开发过程中事务管理相关的关注点进行适当的分离,并对这些关注点进行合 理的抽象,最终打造了一套使用方便,却功能强大的事务管理“利器”。通过Spring的事务框架,我 们可以按照统一的编程模型来...
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
NULL 博文链接:https://quicker.iteye.com/blog/674029
NULL 博文链接:https://vzhchv0010.iteye.com/blog/356137