java spring事务回滚_java spring配置事务回滚的案例方法

以前都不玩事务,或让hibernate做数据库事务回滚,不安全也不方便。为了规范自己的编码习惯,就做了spring管理事务,我的思路是action调用services,aop所有的services,只要services的任何地方出错,就回滚这个service方法。理解起来简单,配置起来也简单:

1.spring applicationContext.xml<?xml  version="1.0" encoding="UTF-8"?>

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"

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

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

default-autowire="byName">

class="org.apache.commons.dbcp.BasicDataSource">

value="com.mysql.jdbc.Driver">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

org.hibernate.dialect.MySQLDialect

validate

dao/user/User.hbm.xml

2.配置说明

只需要看事务回滚的部分就可以了,复制到自己的代码中也只要复制配置事物回滚这部分就可以了。

最重要的就是

就是监听services包下的所有方法,只要这个方法出异常就回滚。

另外

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。


版权声明:本文为weixin_35335035原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。