Spring JDBC-使用XML配置声明式事务
来源:互联网 发布:药品配伍禁忌 数据库 编辑:程序博客网 时间:2024/06/01 09:10
- 系列
- 概述
- 基于aoptx命名空间的配置
- 示例
- txmethod元素属性
系列
Spring对事务管理的支持概述以及 编程式的事务管理
Spring JDBC-使用XML配置声明式事务
Spring JDBC-使用注解配置声明式事务
概述
大多数开发者选择声明式事务管理的功能,这种方式对代码的侵入性最小,可以让事务管理完全从业务代码中移除,非常符合非侵入式轻量容器的理念。
Spring的声明式事务管理是通过AOP实现的,通过事务的声明性信息,Spring负责将事务管理增强逻辑动态的织入到业务方法的相应连接点中。 这些逻辑包括获取线程绑定资源、开始事务、提交/回滚事务、进行异常转换和处理等工作。
基于aop/tx命名空间的配置
Spring2.0引入了AspectJ切面定义语言,这使得事务方法切面描述变得更加简单。
Spring在基于Schema的配置中添加了一个tx命名空间,在配置文件中以明确结构化的方式定义事务属性,大大提高了配置事务属性的便利性。
示例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
我们来看下关于事务管理的配置文件:
<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入其他xml --> <import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/> <!-- 使用强大的切点表达式语言轻松定义目标方法 --> <aop:config proxy-target-class="true"> <!-- 通过aop定义事务增强切面 --> <aop:pointcut id="serviceMethod" expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))" /> <!-- 引用切面和事务增强 --> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config> <!-- 事务增强 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <!-- 事务属性定义 --> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" rollback-for="Exception"/> <tx:method name="update*"/> </tx:attributes> </tx:advice></beans>
在配置文件中引入了
<import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/>
在aop命名空间中,通过切点表达式语言,
expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))"
将com.xgj.dao.transaction.xmlTrans.service包以及所有以Service为后缀的类纳入了需要进行事务增强的范围,并配合 tx:advice的aop:advisor完成了事务切面的定义。
aop:advisor引用txAdvice增强是在tx命名空间上定义的。 首先事务增强一定需要一个事务管理器的支持,tx:advice通过 transaction-manager属性引用定义的事务管理器(默认查找名为transactionManager的事务管理器,所以如果名为transactionManager可以不指定)。
我们在ManagerService类中将addTeacher和addStudent中放在一个事务中
@Servicepublic class ManagerService { private TeacherDao teacherDao; private StudentDao studentDao; /** * * * @Title: addTeacherAndStudent * * @Description: 配置文件中<tx:method name="add*" rollback-for="Exception"/> * 我们故意经写入student表时,让其异常,看下teacher是否回滚--OK 都没有写入,说明在一个事务中 * 如果有去掉<tx:method name="add*" * rollback-for="Exception"/>,经验证Teacher可成功增加,说明不在一个事务中 * * rollback-for="Exception"这个可不加,默认是回滚的 * @param teacher * @param student * * @return: void */ public void addTeacherAndStudent(Teacher teacher, Student student) { teacherDao.addTeacher(teacher); studentDao.addStudent(student); }
因为在配置文件中
<tx:method name="add*" rollback-for="Exception"/>
所以addTeacherAndStudent中的方法在一个事务中,我们将addStudent中故意写错,让其抛出异常,经验证Teacher也没有欧增加成功,说明配置生效,在一个事务中。
tx:method元素属性
可以使用冒号来定义表格的对齐方式,如下:
get*
’、’handle*
’、’on*Event
’等等。 propagation 否 REQUIRED 事务传播行为 isolation 否 DEFAULT 事务隔离级别 timeout 否 -1 事务超时的时间(以秒为单位) ,如果为-1,则说明事务超时时间由底层的事务系统所决定 read-only 否 false 事务是否只读 rollback-for 否 所有运行期异常回滚 触发事务回滚的Excepiton,用异常名称的片段进行匹配,可以设置多个,用逗号分开,如Exception1,Exception2 norollback-for 否 所有检查型异常不回滚 不触发事务回滚的Excepiton,用异常名称的片段进行匹配,可以设置多个,用逗号分开,如Exception1,Exception2继承自runtimeexception或error的是非检查型异常(运行期异常)
继承自exception的是检查型异常,检查型异常则必须用try语句块进行处理或者把异常交给上级方法处理.
如果需要为不同的业务Bean配置不同的事务管理风格,则可以在aop:config中定义多套事务切面。
基于aop/tx配置的声明式事务管理是实际应用中最常使用的事务管理方式,它的表达能力最强且使用最为灵活。
- Spring JDBC-使用XML配置声明式事务
- Spring JDBC-使用注解配置声明式事务
- Spring事务管理——使用XML配置声明式事务
- Spring事务管理——使用XML配置声明式事务
- Spring事务管理——使用XML配置声明式事务
- Spring事务管理——使用XML配置声明式事务
- Spring声明式事务XML配置
- spring jdbc 声明式事务
- Spring使用TransactionProxyFactoryBean声明式事务配置
- Spring使用TransactionProxyFactoryBean声明式事务配置
- Spring学习历程---使用注解配置或XML配置声明式事务
- SSM创建-Spring 使用XML配置声明式事务(11)
- 12、SSM框架-Spring 使用XML配置声明式事务(12)
- spring给予XML配置的声明式事务
- Spring配置声明式事务--xml配置文件方式
- Spring声明式事务管理、事务的传播行为xml配置
- spring在xml中事务声明配置
- spring 配置声明式事务
- [cv][dsp] filter
- 欢迎使用CSDN-markdown编辑器
- centos7 无法启动网络(service network restart)错误解决办法
- Linux_CentOS7配置主机名
- AngularJS开发WebApp高亮跳转按钮效果,ui-sref和ui-sref-active的使用
- Spring JDBC-使用XML配置声明式事务
- qt用http协议访问webservice,用crypto的sha256签名
- 第一次写博客
- SublimeText3143从破解到汉化
- 奎特尔星球|英雄与魔灵
- xml
- Idea将项目上传到github
- 奎特尔星球|魔灵传说(上)
- AngularJS通过路由模块ui-sref指令跳转页面传参方式