1、Spring对事务管理的支持

Spring为事务管理提供了一致的编程模板,在高层次建立了统一的事务抽象。也就是说,不管选择Spring JDBC、Hibernate 、JPA 还是iBatis,Spring都让我们可以用统一的编程模型进行事务管理。

    在Spring事务管理SPI(Service Provider Interface)的抽象层主要包括3个接口,分别是PlatformTransactionManager、TransactionDefinition和TransactionStatus。TransactionDefinition用于描述事务的隔离级别、超时时间、是否为只读事务和事务传播规则等控制事务具体行为的事务属性,这些事务属性可以通过XML配置或注解描述提供,也可以通过手工编程的方式设置。PlatformTransactionManager根据TransactionDefinition提供的事务属性配置信息,创建事务,并用TransactionStatus描述这个激活事务的状态。
1)Spring JDBC 和iBatis
基于数据源的数据管理器

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
2)JPA
JAP事务管理器配置

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"/>
...
<bean id="transactionManger" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFacotry-ref="entityManagerFactory"/>

3)Hibernate

Hibernate3.0事务管理器配置

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingResources="classpath:bbtForum.hbm.xml"> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
4)JTA
JTA事务管理器

<jee:jndi-lookup id="accountDs" jndi-name="java:comp/env/jdbc/account"/>
<jee:jndi-lookup id="orderDs" jndi-name="java:comp/env/jdbc/account"/>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager"/>
2、编程式事务管理
    Spring事务管理提供了模板类org.springframework.transaction.support.TransactionTemplate,以满足一些特殊场合的需要。

package com.baobaotao.service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.baobaotao.dao.ForumDao;
import com.baobaotao.domain.Forum;
public class ForumService1 {
private ForumDao forumDao;
//通过IoC注入
private TransactionTemplate template;
public void addForum(final Forum forum) {
template.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
//需要在事务环境中执行的代码
forumDao.addForum(forum);
}
});
}
public void setForumDao(ForumDao forumDao) {
this.forumDao = forumDao;
}
public void setTemplate(TransactionTemplate template) {
this.template = template;
}
}
3、使用XML配置声明式事务
    Spring的声明式事务管理是通过Spring AOP实现的,通过事务的声明性信息,Spring负责将事务管理增强逻辑动态织入到业务方法相应连接点中。这些逻辑包括获取线程绑定资源、开始事务、提交/回滚事务、进行异常转换和处理等工作。
BbtForum接口:

package com.yyq.service;
import com.yyq.domain.Forum;
import com.yyq.domain.Topic;
public interface BbtForum {
void addTopic(Topic topic) throws Exception;
void updateForum(Forum forum);
Forum getForum(int forumId);
int getForumNum();
}

BbtForumImpl实现类:

package com.yyq.service.impl;
import com.yyq.dao.ForumDao;
import com.yyq.dao.PostDao;
import com.yyq.dao.TopicDao;
import com.yyq.domain.Forum;
import com.yyq.domain.Topic;
import com.yyq.service.BbtForum; 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());
}
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;
}
}

Dao和Datasource配置文件applicationContext-dao.xml:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"/> <bean id="abstracDao" abstract="true" p:jdbcTemplate-ref="jdbcTemplate"/>
<bean id="forumDao" parent="abstracDao" class="com.yyq.dao.jdbc.ForumDaoImpl" />
<bean id="postDao" parent="abstracDao" class="com.yyq.dao.jdbc.PostDaoImpl"/>
<bean id="topicDao" parent="abstracDao" class="com.yyq.dao.jdbc.TopicDaoImpl"/> </beans>

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">
<!--导入Dao和DataSource的配置文件-->
<import resource="applicationContext-dao.xml"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--需要实施事务增强的目标业务Bean-->
<bean id="bbtForumTarget" class="com.yyq.service.impl.BbtForumImpl"
p:forumDao-ref="forumDao"
p:topicDao-ref="topicDao"
p:postDao-ref="postDao"/>
<!--使用事务代理工程类为目标业务Bean提供事务增强-->
<bean id="bbtForum" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
p:transactionManager-ref="txManager"
p:target-ref="bbtForumTarget">
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>

测试方法:

import com.yyq.domain.Post;
import com.yyq.domain.Topic;
import com.yyq.service.BbtForum;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBbtForumPfb {
public static void main(String[] args) throws Throwable{
String configPath = "classpath:applicationContext-pfb.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
BbtForum bbtForum = ctx.getBean("bbtForum",BbtForum.class);
System.out.println("begin........");
Topic topic = new Topic();
topic.setTopicTitle("Title -pfb");
Post post = new Post();
post.setPostText("post content -pfb");
topic.setPost(post);
bbtForum.addTopic(topic);
System.out.println("end........");
}
}
2)基于tx/aop命名空间的配置:

<?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">
<!--导入Dao和DataSource的配置文件-->
<import resource="classpath:applicationContext-dao.xml"/>
<!--声明事务管理器-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!--不再需要为了事务AOP增强的实施而改名换姓-->
<bean id="bbtForum"
class="com.yyq.service.impl.BbtForumImpl"
p:forumDao-ref="forumDao"
p:topicDao-ref="topicDao"
p:postDao-ref="postDao"/>
<!--使用强大的切点表达式语言轻松定义目标方法-->
<aop:config>
<!--通过aop定义事务增强切面-->
<aop:pointcut id="serviceMethod" expression="execution(* com.yyq.service.*Forum.*(..))"/>
<!--引用事务增强-->
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>
<!--事务增强-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<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>

测试方法:

import com.yyq.domain.Post;
import com.yyq.domain.Topic;
import com.yyq.service.BbtForum;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBbtForumTx {
public static void main(String[] args) throws Throwable{
String configPath = "classpath:applicationContext-tx.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
BbtForum bbtForum = ctx.getBean(BbtForum.class); System.out.println("begin........");
Topic topic = new Topic();
topic.setTopicTitle("Title Tx-");
Post post = new Post();
post.setPostText("post content -Tx");
topic.setPost(post);
bbtForum.addTopic(topic);
System.out.println("end........");
}
}
 
4、使用注解配置声明式事务
    使用@Transaction注解声明事务,可以应用于接口定义和接口方法、类定义和类的public方法上。
使用@Transaction注解的业务类:

@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());
}
public Forum getForum(int forumId) {
return forumDao.getForum(forumId);
}
public void updateForum(Forum forum) {
forumDao.updateForum(forum);
}
....

applicationContext-anno.xml:使事务注解生效

<?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实现类标注了@Transaction,所以将会被注解驱动自动织入事务-->
<bean id="bbtForum"
class="com.yyq.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"/>
<!--对标注@Transaction注解的Bean进行加工处理,以织入事务管理切面-->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
</beans>
 
 

Spring 对事务管理的支持的更多相关文章

  1. Spring对事务管理的支持的发展历程--转

    原文地址:http://www.iteye.com/topic/1123049 1.问题 Connection conn = DataSourceUtils.getConnection(); //开启 ...

  2. Spring对事务管理的支持的发展历程(基础篇)

    1.问题 Connection conn = DataSourceUtils.getConnection(); //开启事务 conn.setAutoCommit(false); try { Obje ...

  3. 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)

    Spring事务管理概述 文章目录 Spring事务管理概述 数据库事务 什么是Spring的事务管理? Spring对事务管理的支持 Spring事务管理的核心接口 Platform Transac ...

  4. Spring的事务管理

    事务 事务:是逻辑上一组操作,要么全都成功,要么全都失败. 事务特性(ACID) 原子性:事务不可分割 一致性:事务执行的前后,数据完整性保持一致 隔离性:一个事务执行的时候,不应该受到其他事务的打扰 ...

  5. spring笔记--事务管理之声明式事务

    事务简介: 事务管理是企业级应用开发中必不可少的技术,主要用来确保数据的完整性和一致性, 事务:就是一系列动作,它们被当作一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. Spring中使 ...

  6. Spring应用——事务管理

    事务基础:请参看:http://www.cnblogs.com/solverpeng/p/5720306.html 一.Spring 事务管理 1.前提:事务管理器 在使用 Spring 声明式事务管 ...

  7. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  8. Spring高级事务管理难点剖析

    1Spring事务传播行为 所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring支持7种事务传播行为 PROPAGATION_REQUIRED(加入已有事务) 如果当前没 ...

  9. CSDN上看到的一篇有关Spring JDBC事务管理的文章(内容比较全) (转)

    JDBC事务管理 Spring提供编程式的事务管理(Programmatic transaction manage- ment)与声明式的事务管理(Declarative transaction ma ...

随机推荐

  1. .NET项目引用黄色小三角以及找不到依赖的解决方法

    上图,问题描述: 经过查证,这个是引用的项目框架版本和本项目框架版本不一致的症状. 解决办法: 点击项目右键属性,调整版本使两个项目的版本一致 问题解决.good! ----------------- ...

  2. 28、初识socket(subprocess模块)

    经过近一个半月的学习我们已经度过了python基础的阶段,今天我们开始学习python网络编程,没有难以理解的逻辑,更注重的是记忆. 本篇导航: 客户端/服务器架构 scoket与网络协议 套接字 基 ...

  3. pygame 笔记-3 角色动画及背景的使用

    上二节,已经知道如何控制基本的运动了,但是只有一个很单调的方块,不太美观,本节学习如何加载背景图,以及角色的动画. 素材准备:(原自github) 角色动画的原理:动画都是一帧帧渲染的,比如向左走的动 ...

  4. 第一章 java nio三大组件与使用姿势

    本案例来源于<netty权威指南> 一.三大组件 Selector:多路复用器.轮询注册在其上的Channel,当发现某个或者多个Channel处于“就绪状态”后(accept接收连接事件 ...

  5. golang使用chan注意事项

    背景 最近老代码中遇到的一个问题,表现为: goroutine数量在高峰期上涨,上涨后平峰期将不下来.也就是goroutine泄露 使用pprof看,进程堵塞在chan chan的使用经验 在使用ch ...

  6. 使用Sphinx编写文档

    操作系统 : Windows7_x64 Python 版本 : 2.7.10 Sphinx 版本 : 官方网址:http://sphinx-doc.org github地址: https://gith ...

  7. 【转载】linux系统时间自动同步:ntp

    NTP基本介绍 NTP(Network TimeProtocol,网络时间协议),使用来使本地机器与服务端机器时间保持同步的一种协议.如果我们只有一台机器那么只需要安装NTP客户端ntpdate这个包 ...

  8. [转]正则表达式的先行断言(lookahead)和后行断言(lookbehind)

    正则表达式的先行断言和后行断言一共有4种形式: (?=pattern) 零宽正向先行断言(zero-width positive lookahead assertion) (?!pattern) 零宽 ...

  9. APK优化工具zipalign的详细介绍和使用

    什么是Zipalign?      Zipalign是一个android平台上整理APK文件的工具,它首次被引入是在Android 1.6版本的SDK软件开发工具包中.它能够对打包的Android应用 ...

  10. vs code 快捷键中英文对照

    常用 General 按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick O ...