spring的事务管理
Spring提供了编程性事务管理与声明式的事务管理。Spring事务管理的抽象关键在于org.springframework.transaction.PlatformTransactionManager接口的实现。
PlatfromTransactionManager接口有许多具体的事务实现类,例如:DataSourceTransactionManager、HibernateTransactionManager、JdoTransactionManager,JtaTransactionManager等等。
此处需要注意的是,要使用Mysql数据库进行事务处理,必须建立支持事务的表类型,例如:InnoDB的表类型。
1、 Spring提供的编程性事务
可以清楚的控制事务的边界,自行实现事务的开始时间,撤销时间,结束时间等,可以实现细粒度的事务控制。
编码例子如下:
private PlatformTransactionManager txManager;
public void setTxManager(PlatformTransactionManager txManager) {
this.txManager = txManager;
}
public void insertUser(User user) {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try{
JdbcTemplate tmp = new JdbcTemplate(dataSource);
tmp.execute("insert into tbl_user (name,pwd) values ('"+user.getName()+"','"+user.getPwd()+"')");
//写错的sql,等着抛出运行时异常
tmp.update("sssssssssssss");
UserUpdate uu = new UserUpdate(dataSource);
uu.update(new Object[]{user.getName(),user.getPwd()});
}catch(DataAccessException e){
txManager.rollback(status);
throw e;
}
txManager.commit(status);
}
配置文件的写法如下:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="com.itcast.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="txManager" ref="txManager"></property>
</bean>
2、 声明式的事务管理
Spring事务的相关API可以不用介入程序之中,只是在配置文件上修改配置下就可以移去事务管理服务。Spring的声明式事务是通过spring的AOP来实现的,所以执行程序的时候,请记得将spring-aop.jar给放到classpath中。
1)、最基础的事务配置如下:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="com.itcast.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="txManager" ref="txManager"></property>
</bean>
<!-- 事务代理 -->
<bean id="userDaoTxProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="userDao"></property>
<property name="transactionManager" ref="txManager"></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
我们在使用的时候只是需要取得userDaoTxProxy 而不是userDao就可以完成对这个Dao的事务控制了。
2)、将事务抽象提取,让我们的代理类取得一个拦截器方式的事务,从而将每个代理类的事务都抽象出来集中管理,这样只要我们命名规范,那么我们的事务处理就更加容易。
例如:
分享到:
相关推荐
Getting started with Spring ...Chapter 13 – More Spring Web MVC – internationalization, file upload and asynchronous request processing Chapter 14 – Securing applications using Spring Security
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
文档中包括13本收集的 Spring Frameworks 相关的技术数据,均为英文版,包括Spring 4 和 Spring 5,内容涉及 Spring Core-Frameworks、 Spring-Boot、 Spring Micro-Services 等。书籍列表如下 Beginning Spring ...
Discover the key Spring framework-related technology standards such as Spring core, Spring-AOP, Spring data access frameworks, and Spring testing to develop robust Java applications easily and rapidly...
09. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10. </listener> 11. 12. <servlet> 13. <servlet-name>spring</servlet-name> 14. <servlet-class>org.spring...
第13章 Spring Security框架 第14章 Spring Portlet MVC框架 第15章 Spring Web Flow 第16章 Spring远程服务和Web服务 第17章 Spring对EJB和JMS的支持 第18章 Spring对JMX、电子...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e7d503: defining beans [accountBizImpl,accountDaoImpl,baseMapperDaoImpl,org.springframework....
顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.spring...
This is a tutorial on Spring MVC, a module in the Spring Framework for rapidly developing web applications. The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in ...
第13章 Spring Security框架 第14章 Spring Portlet MVC框架 第15章 Spring Web Flow 第16章 Spring远程服务和Web服务 第17章 Spring对EJB和JMS的支持 第18章 Spring对JMX、电子...
第13章 Spring Security框架 第14章 Spring Portlet MVC框架 第15章 Spring Web Flow 第16章 Spring远程服务和Web服务 第17章 Spring对EJB和JMS的支持 第18章 Spring对JMX、电子邮件和...
Spring Framework开发参考: 1 13.9. 使用Spring的表单标签库 1 13.9.1. 配置 1 13.9.2. form标签 1 13.9.3. input标签 3 13.9.4. checkbox标签 3 13.9.5. checkboxes标签 6 13.9.6. radiobutton标签 6 13.9.7. ...
spring-framework-5.3.2-dist SHA-256:e88f15e4a3a18f70ac01ef67a7bbec4578a2ab0fc1f0d86c9c8f7ac8ece3375a (Uploaded: Identical) SHA-1:2246d8f8ac7b445b13d5d21b1b52a7cf68ae1c69 (Uploaded: Identical) MD5:a3ee...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...
Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring ...Chapter 13: Spring Boot in the Cloud Chapter 14: Extending Spring Boot Apps Appendix A: Spring Boot 1.4.x
│ Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_2_6_RELEASE.xml │ Maven__org_springframework_spring_aop_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_beans_5_2_5_...
11 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 12 13 14 <prop key="/hello.do">helloController 15 16 17 18 19 20 <!-- 21 22 --> ...
第 3 章 Webx Framework ...................................................................................... 24 3.1. Webx的初始化 ....................................................................