一、声明式事务配置
1.基于TransactionProxyFactoryBean的配置
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <import resource="classpath:applicationContext-dao.xml" /> <!--可以使用不同的事务管理器配置事务 org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.orm.JpaTransactionManager org.springframework.orm.hibernate3.HibernateTransactionManager org.springframework.orm.jdo.JdoTransactionManager org.springframework.orm.jta.JtaTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="bbtForumTarget" class="com.baobaotao.service.impl.BbtForumImpl" p:forumDao-ref="forumDao" p:topicDao-ref="topicDao" p:postDao-ref="postDao"/> <bean id="bbtForum" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="txManager" p:target-ref="bbtForumTarget"> <property name="transactionAttributes"> <props> <prop key="addTopic"> PROPAGATION_REQUIRED,+PessimisticLockingFailureException </prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED,-tion</prop> </props> </property> </bean> </beans>
2.基于scheme tx/aop命名空间和切面advisor配置
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <import resource="classpath:applicationContext-dao.xml" /> <bean id="bbtForum" class="com.baobaotao.service.impl.BbtForumImpl" p:forumDao-ref="forumDao" p:topicDao-ref="topicDao" p:postDao-ref="postDao"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.baobaotao.service.*Forum.*(..))" /> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" > <tx:attributes> <tx:method name="get*" read-only="false"/> <tx:method name="add*" rollback-for="PessimisticLockingFailureException"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> </beans>
3.基于注解@Transactional 及<tx:annotation-driven >自动配置
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <import resource="classpath:applicationContext-dao.xml" /> <bean id="bbtForum" class="com.baobaotao.service.impl.BbtForumImpl" p:forumDao-ref="forumDao" p:topicDao-ref="topicDao" p:postDao-ref="postDao" /> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> <!--名称为transactionManager可通过以下简化配置 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <tx:annotation-driven/> --> </beans>
相关的java注解类
@Transactional public class BbtForumImpl implements BbtForum { private ForumDao forumDao; private TopicDao topicDao; private PostDao postDao; public void addTopic(Topic topic) throws Exception { topicDao.addTopic(topic); // if(true) throw new PessimisticLockingFailureException("fail"); postDao.addPost(topic.getPost()); } @Transactional(readOnly=true) public Forum getForum(int forumId) { return forumDao.getForum(forumId); } public void updateForum(Forum forum) { forumDao.updateForum(forum); } public int getForumNum() { return forumDao.getForumNum(); } public void setForumDao(ForumDao forumDao) { this.forumDao = forumDao; } public void setPostDao(PostDao postDao) { this.postDao = postDao; } public void setTopicDao(TopicDao topicDao) { this.topicDao = topicDao; } }
4.通过AspectJ LTW引入事务切面,实现加截期间织入事务
使用-javaagent: org.springframework.aspects-{version}.jar作为JVM参数,在类路径META-INF目录下提供如下的配置文件:
<?xml version="1.0"?> <aspectj> <aspects> <aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect" /> </aspects> <weaver options="-showWeaveInfo -XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler"> <include within="com.baobaotao.servie.impl.*" /> </weaver> </aspectj>
二、使用TransactonTemplate编程式的事务管理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- jdbc事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <!--事务模板 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <!--ISOLATION_DEFAULT 表示由使用的数据库决定 --> <property name="isolationLevelName" value="ISOLATION_DEFAULT"/> <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/> <!-- <property name="timeout" value="30"/> --> </bean> </beans>
调用TransactionTemplate 相关java代码
protected TransactionTemplate transactionTemplate;
protected PayOrder savePayReq(final PayOrder payOrder) { PayOrder order = (PayOrder) this.transactionTemplate .execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { ... } }); }
三、通过DataSourceUtils获取数据连接,以避免数据连接泄漏
Connection conn = null; try { //Connection conn = jdbcTemplate.getDataSource().getConnection(); conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); String sql = "UPDATE t_user SET last_logon_time=? WHERE user_name =?"; jdbcTemplate.update(sql, System.currentTimeMillis(), userName); Thread.sleep(1000);//②模拟程序代码的执行时间 } catch (Exception e) { e.printStackTrace(); }finally{ DataSourceUtils.releaseConnection(conn, jdbcTemplate.getDataSource()); }
其它技术相关的等价类:
SessionFactoryUtils
EntityManagerFactoryUtils
PersistenceManagerFactoryUtils
相关推荐
Spring事务管理Demo
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
spring 事务管理的理解
spring事务管理 配置文件等详解
Spring事务管理(全),需要的可下载!
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
Synchronized锁在Spring事务管理下,导致线程不安全。
详细介绍Spring事务管理
Spring事务管理中所需要的jar包是 spring-tx-3.2.0.RELEASE.jar
3、了解Spring事务管理的3个核心接口; 4、了解Spring事务管理的两种方式; 5、掌握基于XML和Annotation的声明式事务管理的使用。 二.实验内容 (1)使用Spring JDBC实现书店的购书过程,即有如下一个BookShopDao接口...
Spring事务管理4种方式 入门级 最简单demo PlatformTransactionManager TransactionTemplate
Spring声明式事务管理失效的原因,这里面是日常工作中踩过的坑,事务失效所产生的影响比较隐蔽,测试时易忽略,大多是在线上暴露,这可能是对Spring管理下的信任吧
在该JAR包的org.springframework.transaction包中,有3个接口文件PlatformTransactionManager、TransactionDefinition和TransactionStatus,如下图所示: Spring事务管理的三个核心接口 5.1.1 事务管理的核心接口 1....
此包含有所有Spring事务管理开发所需要的Jar包,包含框架、核心、表达式、日志、编译、事务切面aop、数据库连接、单元测试、框架文本等jar包。
spring3.0两种事务管理配置
spring-tx spring-transaction spring事务管理实例