Spring 对事务管理的支持
Spring为事务管理提供了一致的编程模板,在高层次建立了统一的事务抽象。也就是说,不管选择Spring JDBC、Hibernate 、JPA 还是iBatis,Spring都让我们可以用统一的编程模型进行事务管理。
<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"/>
<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
<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>
<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"/>
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;
}
}
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........");
}
}
<?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........");
}
}
@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 对事务管理的支持的更多相关文章
- Spring对事务管理的支持的发展历程--转
原文地址:http://www.iteye.com/topic/1123049 1.问题 Connection conn = DataSourceUtils.getConnection(); //开启 ...
- Spring对事务管理的支持的发展历程(基础篇)
1.问题 Connection conn = DataSourceUtils.getConnection(); //开启事务 conn.setAutoCommit(false); try { Obje ...
- 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)
Spring事务管理概述 文章目录 Spring事务管理概述 数据库事务 什么是Spring的事务管理? Spring对事务管理的支持 Spring事务管理的核心接口 Platform Transac ...
- Spring的事务管理
事务 事务:是逻辑上一组操作,要么全都成功,要么全都失败. 事务特性(ACID) 原子性:事务不可分割 一致性:事务执行的前后,数据完整性保持一致 隔离性:一个事务执行的时候,不应该受到其他事务的打扰 ...
- spring笔记--事务管理之声明式事务
事务简介: 事务管理是企业级应用开发中必不可少的技术,主要用来确保数据的完整性和一致性, 事务:就是一系列动作,它们被当作一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. Spring中使 ...
- Spring应用——事务管理
事务基础:请参看:http://www.cnblogs.com/solverpeng/p/5720306.html 一.Spring 事务管理 1.前提:事务管理器 在使用 Spring 声明式事务管 ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- Spring高级事务管理难点剖析
1Spring事务传播行为 所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring支持7种事务传播行为 PROPAGATION_REQUIRED(加入已有事务) 如果当前没 ...
- CSDN上看到的一篇有关Spring JDBC事务管理的文章(内容比较全) (转)
JDBC事务管理 Spring提供编程式的事务管理(Programmatic transaction manage- ment)与声明式的事务管理(Declarative transaction ma ...
随机推荐
- Linux学习-实验楼(1)
1.Shell:命令解释器 2.Linux技能树 3.X11:X窗口系统,其本身只是工具包及架构协议,Xorg是x架构规范的一个实现体,是实现了x协议规范的一个提供图形界面的服务器 4.终端本质上是对 ...
- JSP(8)—EL案例和JSTL案例
1.EL案例 el.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- JDBC(9)—事务(Transaction)
数据库事务:在数据库中所谓事务是指一组逻辑操作单元,使数据从一种状态转换到另一种状态.为确保数据库中的数据的一致性,数据的操纵应当是离散的成组的逻辑单元:当它全部完成时,数据的一致性可以保持,而当这些 ...
- ubuntu17.04 调试系统工具bcc,systamtap安装
发行版 ubuntu17.04 cat lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=17.04 DISTRIB_CODENAME=zesty DISTR ...
- springmvc中select可以绑定enum中所有数据的方法
public enum States { AK("AK"), AL("AL"), AR("AR"), AZ("AZ"), ...
- 集合总结--ArrayList、LinkedList、HashMap
一.概述 ArrayList:数组集合. 查询.修改.新增(尾部新增)快,删除.新增(队列中间)慢,适用于查询.修改较多的场景. LinkedList:双向链表集合.查 ...
- android平台修改默认语言的那点事
转自:https://blog.csdn.net/qinghua0706/article/details/8243858 最近开始做智能机,用的是展讯6820平台,第一个需求就是改默认语言,一般碰到不 ...
- Docker查看运行中容器并进入容器
一.简述 Docker查看运行中容器并进入容器. 二.方法 $ sudo docker ps $ sudo docker exec -it 775c7c9ee1e1 /bin/bash 将黄色文字替换 ...
- SQL SERVER 中日期格式化,及GETDATE()、CONVERT()函数使用说明
1. date和datetime类型的区别 date是SQL Server 2008新引进的数据类型.它表示一个日期,不包含时间部分,可以表示的日期范围从公元元年1月1日到9999年12月31日.只需 ...
- Tomcat 部署多个项目出现错误
有时,我们会遇到部署同样项目可是不同版本号来回切换的问题.可是有时就是莫名奇异的会起不来. 也没太多时间去解决这些问题,所以就又一次把纯净版的Tomcat部署进去就能够了. 我想非常有可能就是Tomc ...