Spring配置事务的五种方式
Java事务的类型有三种:
JDBC事务、 可以将多个 SQL 语句结合到一个事务中。JDBC 事务的一个缺点是事务的范围局限于一个数据库连接。一个 JDBC 事务不能跨越多个数据库
JTA(Java Transaction API)事务、事务可以跨越多个数据库或多个DAO,使用也比较复杂。
容器事务。主要指的是J2EE应用服务器提供的事务管理,局限于EJB应用使用。
spring事务的配置方式编程式事务和声明式事务,相信大家都知道是有5种,但我们经常使用的应该就是基于注解和tx标签配置拦截器两种方式了
| 1 2 3 4 5 6 7 8 9 10 |    <beanid="sessionFactory"         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">           <propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>           <propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/>     </bean><!-- 定义事务管理器(声明式的事务) -->  <beanid="transactionManager"      class="org.springframework.orm.hibernate3.HibernateTransactionManager">       <propertyname="sessionFactory"ref="sessionFactory"/>   </bean> | 
ps:声明式事务管理建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。
1、基于注解,DAO上需加上@Transactional注解
| 1 | <tx:annotation-driventransaction-manager="transactionManager"/> | 
2、使用tx标签配置的拦截器
| 1 2 3 4 5 6 7 8 9 10 11 12 | <tx:adviceid="txAdvice"transaction-manager="transactionManager">     <tx:attributes>         <tx:methodname="*"propagation="REQUIRED"/>     </tx:attributes> </tx:advice> <aop:config>     <aop:pointcutid="interceptorPointCuts"        expression="execution(* com.bluesky.spring.dao.*.*(..))"/>     <aop:advisoradvice-ref="txAdvice"        pointcut-ref="interceptorPointCuts"/>         </aop:config> | 
3、使用拦截器
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <beanid="transactionInterceptor"       class="org.springframework.transaction.interceptor.TransactionInterceptor">          <propertyname="transactionManager"ref="transactionManager"/>          <!-- 配置事务属性 -->       <propertyname="transactionAttributes">              <props>                  <propkey="*">PROPAGATION_REQUIRED</prop>              </props>          </property>      </bean>    <beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">          <propertyname="beanNames">              <list>                  <value>*Dao</value>            </list>          </property>          <propertyname="interceptorNames">              <list>                  <value>transactionInterceptor</value>              </list>          </property>      </bean> | 
4、所有Bean共享一个代理基类
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <beanid="transactionBase"        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"        lazy-init="true"abstract="true">       <!-- 配置事务管理器 -->    <propertyname="transactionManager"ref="transactionManager"/>       <!-- 配置事务属性 -->    <propertyname="transactionAttributes">           <props>               <propkey="*">PROPAGATION_REQUIRED</prop>           </props>       </property>   </bean>    <!-- 配置DAO -->    <beanid="userDaoTarget"class="com.bluesky.spring.dao.UserDaoImpl">         <propertyname="sessionFactory"ref="sessionFactory"/>     </bean>     <beanid="userDao"parent="transactionBase">           <propertyname="target"ref="userDaoTarget"/>        </bean> | 
5、每个Bean都有一个代理
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | <beanid="userDao"        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">              <!-- 配置事务管理器 -->           <propertyname="transactionManager"ref="transactionManager"/>              <propertyname="target"ref="userDaoTarget"/>            <propertyname="proxyInterfaces"value="com.bluesky.spring.dao.GeneratorDao"/>         <!-- 配置事务属性 -->        <propertyname="transactionAttributes">               <props>                   <propkey="*">PROPAGATION_REQUIRED</prop>             </props>           </property>       </bean> | 
Spring配置事务的五种方式的更多相关文章
- spring事务之——spring配置事务的五种方式
		Spring配置文件中关于事务配置总是由三个部分组成,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ... 
- Spring 配置 事务的几种方式
		Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ... 
- spring配置属性的两种方式
		spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ... 
- Spring 实现事务的三种方式
		事务:保证数据的运行不会说A给B钱,A钱给了B却没收到. 实现事务的三种方式(重要代码): 1.aspectJ AOP实现事务: <bean id="dataSourceTransac ... 
- SSH2配置事务的两种方式
		<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ... 
- 【Spring】19、spring配置数据源的4种方式
		不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ... 
- Spring配置dataSource的三种方式 数据库连接池
		1.使用org.springframework.jdbc.dataSource.DriverManagerDataSource 说明:DriverManagerDataSource建立连接是只要有连接 ... 
- 浅谈spring配置定时任务的几种方式
		网上看到好多关于定时任务的讲解,以前只简单使用过注解方式,今天项目中看到基于配置的方式实现定时任务,自己做个总结,作为备忘录吧. 基于注解方式的定时任务 首先spring-mvc.xml的配置文件中添 ... 
- spring注入bean的五种方式
		1.属性注入 2.构造方法注入 3.静态工厂注入 package com.voole.factorybeans; import com.voole.beans.TestBean; public cla ... 
随机推荐
- python+RobotFramework
			今天有人问我,她想在在robot里面用到数据库的一个值的随机数,但是不知道怎么实现,我用python写了一段代码链接数据库给表中所需的字段的值取随机数,代码如下: import random,pymy ... 
- 洛谷 P2008 大朋友的数字
			DP,动态规划 树状数组 最长不下降子序列 by GeneralLiu 题目 就是说给一串由 0~9 组成的序列 求 以 i (1~n) 结尾 的 最长不下降子序列 的 和 (最长不下降子序 ... 
- Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView异常解决
			有时在修改xml文件时,全报这种错误,这个应该是缓存没得到及时更新导致的,可以通过以下方法解决: Eclipse tends to mess up your resources every now a ... 
- hdu2157:How many ways??
			n<=20个点m<=100条边有向图不带权,t个询问问Ai到Bi的经过k<=20条边方案数多少. f[i][j]--i到j的方案数,,初始化成初邻接矩阵,这样做一次就得到2条路最短路 ... 
- msp430入门编程47
			msp430中C语言的人机交互--菜单退出 msp430入门编程 msp430入门学习 
- php装饰者模式
			php装饰者模式 装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 示例: A.B.C编辑同一篇文章. class ... 
- How do I know which version of Javascript I'm using?
			Click on this link to see which version your BROWSER is using: http://jsfiddle.net/Ac6CT/ You should ... 
- Fortinet网络接入及安全方案配置步骤
			http://sec.chinabyte.com/200/12553700.shtml 1.概述: Fortinet无线接入及方案由以下两类设备组成: AC(Wifi接入控制器)及安全网关:Forti ... 
- Duplicate property mapping of contactPhone found in
			启动的时候报Duplicate property mapping of contactPhone found in com....的错误,是因为在建立实体对象的时候,有字段重复了,有的是继承了父类的字 ... 
- Deepin-安装QQ音乐(Windows程序)
			打开命令行,输入: sudo apt-get install wine 安装完成后,下载QQ音乐的安装包 然后安装 示例:wine xx.exe 实例:wine QQMusic.exe 安装完成,启动 ... 
