1.配置普通的 controller,service ,dao 的bean.

<!-- 配置 dao ,service -->
<bean id="bookShopDao" class="com.liujl.spring.tx.xml.BookShopDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="bookShopService" class="com.liujl.spring.tx.xml.serivice.impl.BookShopServiceImpl">
<property name="bookShopDao" ref="bookShopDao"></property>
</bean> <bean id="cashier" class="com.liujl.spring.tx.xml.serivice.impl.CashierImpl">
<property name="bookShopService" ref="bookShopService"></property>
</bean>

2.配置事务管理器bean

<!-- 配置事务管理器 hibernate、jpa都是类似的这样配 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

3.引入 tx 命名空间

xmlns:tx="http://www.springframework.org/schema/tx"

4.配置事务各个属性(方法名可以使用通配符*)

<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase" propagation="REQUIRED"/>
<tx:method name="checkout" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>

5.引入aop命名空间

xmlns:aop="http://www.springframework.org/schema/aop"

6.配置事务的切点,并把事务切点和事务属性关联起来

<!-- 配置事务的切点,并把事务切点和事务属性不关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.liujl.spring.tx.xml.serivice.*.*(..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
 事务的属性:  
    1.使用 propagation 声明事务的传播属性,默认即 REQUIRED 即被包含在上面的事务中,放弃自己处理事务
REQUIRES_NEW 以本方法为执行单位,开启一个新事务(外部事务在方法执行前后被挂起) 2.使用 isolation 指定事务的隔离级别,最常用的取值为 READ_COMMITTED 读与提交
    3.默认情况下Spring 的声明式事务对所有的运行时一样长进行回滚
也可以对应的属性指定配置,通常情况下取默认值即可。使用 noRollbackFor 指定不回滚的异常,其他的类似
    4.使用 readOnly 指定事务是否为只读,表示这个事务只读取数据但不更新数据,
这样可以帮助数据库引擎优化事务。若真的是一个只读取数据库值得方法,应设置readOnly=true
    5.使用 timeout 指定强制回滚之前事务可以占用的时间

最新文章

  1. OWIN系列之自己动手编写中间件
  2. WPF透明设置(Opacity)
  3. Bash 4.4 中新增的 ${parameter@operator} 语法
  4. Android开发之Fragment
  5. 在Visual C++下搭建OpenGL的开发环境
  6. Poj 2993 Emag eht htiw Em Pleh
  7. win7-64bit 下oracle11g plsql 的正确安装
  8. 实现js浮点数加、减、乘、除的精确计算(网上很多文章里的方法是不能解决所有js浮点数计算误差的)
  9. SQL Server Service Borker 1
  10. 用git上传项目到github
  11. hdu 4861 Couple doubi(数论)
  12. angular drag and drop (marceljuenemann) 笔记
  13. 1.WF 4.5在项目中直接使用的问题
  14. bookStore第三篇【用户模块、购买模块、订单模块】
  15. springBoot系列教程03:redis的集成及使用
  16. 【iOS】swift 让程序挂起后,能在后台继续运行任务
  17. There are multiple modules with names that only differ in casing. 黄色warning
  18. JavaScript eval
  19. Windows Server 2008 R2 服务器系统安装图文教程
  20. am335x Lan8710a 双网口配置

热门文章

  1. URL编码分析与乱码解决方案
  2. [android] 手机卫士自定义吐司
  3. Xcode 8 媒体权限
  4. 7行代码看EntityFramework是如何运行
  5. WCF使用net.tcp绑定的配置实例
  6. 如何解决“There is no locally stored library”的问题
  7. c#如何禁用win7的任务管理器
  8. BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)
  9. vue项目中引入bootstrap
  10. GIT 这么好用,为什么还是有人不知道怎么用它提交代码?