Spring事务管理
来源:互联网 发布:nginx 静态页面 编辑:程序博客网 时间:2024/06/07 13:02
1.Spring有哪些事务?
(1)spring提供了编程式事务支持 TransactionTemplate;
(2)也提供了声明式事务支持 底层采用AOP实现的事务通知bean;
(3)也有注解形式的 @Transactional
2.Spring中怎么使用事务?
(1)编程式事务 在DAO中通过TransactionTemplate模板类封装事务的管理过程
(2)声明式事务(AOP配置事务通知bean):底层采用AOP思想实现,将事务的管理过程(包括事务对象的创建,提交和回滚作)封装在一个事务通知bean中,然后通过配置AOP切入点的方式来描述需要将事务通知bean所提供的事务管理服务引用给哪些核心对象/方法。
配置方式依赖于AOP的配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > <property name="underclassmen" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="" transaction-manager="transactionManager"(指向一个事务管理器)><tx:attributes> 为不同的方式设置不同的事务属性<tx:method name="表达式匹配方法" 四个属性...rollback-for="监听到什么异常回滚事务"/><tx:method name="add*"isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/><tx:method name="*" rollback-for="java.lang.Exception"/></tx:attributes> </tx:advice><aop:config><aop:pointcut id="perform" expression="execution(* com.hofon.serviceImpl.XXXServiceImpl.*(..))"(表达式指向需要引用事务的方法列表)/><aop:advisor advice-ref="transaction" pointcut-ref="perform"/>()</aop:config>
上述execution(* com.hofon.serviceImpl.XXXServiceImpl.*(..))表达式表示切入点为该类中的任何方法。所以当XXXServiceImpl类中方法调用时就会进行事务管理
,并且当抛出RuntimeException或者Exception时(看具体方法名),自动进行回滚操作。
,并且当抛出RuntimeException或者Exception时(看具体方法名),自动进行回滚操作。
或者(Hibernate)
<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name="configLocation" value= "classpath:hibernate.cfg.xml" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionMgr" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" > <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务传播特性 --> <tx:advice id="txAdvice" transaction-manager= "transactionMgr" > <tx:attributes> <tx:method name="add*" propagation= "REQUIRED" /> <tx:method name="del*" propagation= "REQUIRED" /> <tx:method name="update*" propagation= "REQUIRED" /> <tx:method name="*" read-only= "true" /> </tx:attributes> </tx:advice> <!-- 那些类使用事务 --> <aop:config> <aop:pointcut id="point-cut" expression= "execution(* com.hofon.manager.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref= "point-cut" /> </aop:config> </beans>
声明式事务的特性 : 不需要在核心代码中编写事务的管理过程,以此将核心代码与事务管理服务动态分离。
声明式事务回滚的方式 : 默认产生运行时异常时回滚,可设置(如Exception)
(3)使用@Transactional注解来实现声明式事务
①引入<tx:>命名空间 ,在spring的配置文件中修改, beans根元素里多了三行,如下
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
②配置事务管理器transactional和开始事务的注解支持
<!-- 配置spring的PlatformTransactionManager,名字为默认值 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启事务控制的注解支持 --> <!--用@Transactional 注解替代tx/aop命名空间 来实现 Spring声明式事务管理--> <!--在service类上使用注解@Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/></span></span>
③在接口或类的声明处 ,写一个@Transactional. 要是只在接口上写, 接口的实现类就会继承下来.在接口的实现类的具体方法上,还可以覆盖类声明处的设置.
当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该注解来覆盖类级别的定义。
//在service类上的注解@Transactional@Service@Scope("prototype")public class NewsServiceImpl implements NewsService { @Autowired private NewsDao nd; @Override @Transactional(readOnly=true) //为方法增加事务处理特性 public List showAllNews() { List<News> allNewList = nd.showAllNews(); return allNewList; }}
1、检查你方法是不是public的
2、你的异常类型是不是unchecked异常 ,默认运行时异常RuntimeException才回滚
要想所有异常都回滚,注解上面写明异常类型即可
2、你的异常类型是不是unchecked异常 ,默认运行时异常RuntimeException才回滚
要想所有异常都回滚,注解上面写明异常类型即可
@Transactional(rollbackFor=Exception.class)
类似的还有norollbackFor,自定义不回滚的异常
3、数据库引擎要支持事务,如果是MySQL,注意表要使用支持事务的引擎,比如innodb,如果是myisam,事务是不起作用的
4、是否开启了对注解的解析
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
5、spring是否扫描到你这个包,如下是扫描到com.hofon下面的包,必须是spring里配置的bean才起作用?
<context:component-scan base-package="com.hofon" ><context:component-scan>
类似的还有norollbackFor,自定义不回滚的异常
3、数据库引擎要支持事务,如果是MySQL,注意表要使用支持事务的引擎,比如innodb,如果是myisam,事务是不起作用的
4、是否开启了对注解的解析
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
5、spring是否扫描到你这个包,如下是扫描到com.hofon下面的包,必须是spring里配置的bean才起作用?
<context:component-scan base-package="com.hofon" ><context:component-scan>
阅读全文
1 0
- 事务管理之Spring事务管理
- Spring事务管理
- spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring 事务管理
- Spring事务管理
- spring 事务管理
- spring 事务管理
- spring 事务管理
- spring事务管理
- Spring 事务管理
- Spring事务管理
- spring事务管理
- spring 事务管理
- 用户账户的创建和管理
- Linux-内核通信之netlink机制-详解
- Linux网络编程(三)多路IO转接服务器
- fatal: cannot resume: .git/rebase-apply/final-commit does not exist.
- ArrayList和Vector的扩容机制
- Spring事务管理
- mysql 不是主键自增长
- 怎么把CAD文件转为pdf文件
- git使用记录
- VS x86 x64 anycpu 编译运行对照表
- list嵌套需要注意的问题
- Rigidbody.AddForce
- 跨域解决方案 & 使用angularJS和jQuery进行Ajax请求的差异
- 组群管理
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
中指门
振南明 一袖乾坤
002021中捷资源股吧
中捷地产
沈阳中捷机床有限公司
中捷缝纫机价格表
中捷通信有限公司
中捷缝纫机说明书
在系统中注册捷信
中捷电脑横机
中捷集团
中捷缝纫机股份有限公司
中捷代购
中控台记录仪
昂科雷中控台按键图解
帕萨特中控台
昂科威中控台图解
汽车中控台拆解图
昂科威中控台按键说明
八代雅阁中控台说明书
宝骏560中控台拆装图解
2018款朗逸中控台拆解教程
凯越中控台拆卸图解
凯美瑞中控台改装
各种汽车中控台有表图片
雅阁八代中控台各键作用
中控台行车记录仪
汽车中控台改装
中控台改装
汽车中控台改装液晶屏
改装汽车中控台
凌派2019豪华版中控台按键图解
中控招聘
北京消防中控招聘
中控员招聘
急招消防中控值班员
2018年消防中控员证报名要求
北京消防中控员招聘
中控消防员培训
消防中控员就业前景
消防中控员招聘