首页 技术 正文
技术 2022年11月18日
0 收藏 723 点赞 3,354 浏览 3078 个字

四、Spring的事务管理

事务原本是数据库中的概念, 在Dao层。 但一般情况下, 需要将事务提升到

业务层, 即Service层。 这样做是为了 能够使用事务的特性来管理具体的业务。

1. Spring事务管理API介绍

(1) 事务管理器是PlatformTransactionManager接口 对象。 其主要

用于完成事务的提交、 回滚, 及获取事务的状态信息。

PlatformTransactionManager接口 常用的两个实现类

DataSourceTransactionManager: 使用JDBC或MyBatis 进行持久化数据时使用。

HibernateTransactionManager: 使用Hibernate进行持久化数据时使用。

(2) Spring的回滚方式

Spring事务的默认回滚方式是: 发生运行时异常时回滚, 发生受查异常时提交。

(3) 事务定义接口 事务定义接口 TransactionDefinition中定义了 事务描述

相关的三类常量: 事务隔离级别、 事务传播行为、 事务默认超时时限, 及对它们的

操作。

所谓事务传播行为是指, 处于不同事务中的方法在相互调用时, 执行期间事

务的维护情况。 如, A事务中的方法doSome() 调用B事务中的方法doOther() , 在调

用执行期间事务的维护情况, 就称为事务传播行为。

REQUIRED: 指定的方法必须在事务内 执行。 若当前存在事务, 就加入到当前

事务中; 若当前没有事务, 则创建一个新事务。 这种传播行为是最常见的选择

2. 环境搭建(导入jar包、 添加约束)

除之前的包,还要导入spring-jdbc-4.1.6.RELEASE,spring-tx-4.1.6.RELEASE,mysql-connector-java-5.1.30,com.springsource.com.mchange.v2.c3p0-0.9.1.2

3. 使用AspectJ的AOP配置管理事务( 重点)

分为三步:

注册事务管理器

配置事务注解驱动

以下是xml文件代码:

<?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: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/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

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

        http://www.springframework.org/schema/context/spring-context.xsd>

<!– 加载jdbc属性文件 –>

<context:property-placeholder location=“jdbc.properties”/>

<!– 注册c3p0数据源 –>

<bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>

<property name=“driverClass” value=“${jdbc.driver}”/>

<property name=“jdbcUrl” value=“${jdbc.url}”/>

<property name=“user” value=“${jdbc.username}”/>

<property name=“password” value=“${jdbc.password}”/>

</bean>

<!– 注册accountDao –>

<bean id=“accountDaoImpl” class=“com.mypack.dao.impl.AccountDaoImpl”>

<property name=“dataSource” ref=“dataSource”></property>

</bean>

<!– 注册fundDao –>

<bean id=“fundDaoImpl” class=“com.mypack.dao.impl.FundDaoImpl”>

<property name=“dataSource” ref=“dataSource”></property>

</bean>

<!– 注册service –>

<bean id=“fundServiceImpl” class=“com.mypack.service.impl.FundServiceImpl”>

<property name=“accountDaoImpl” ref=“accountDaoImpl”></property>

<property name=“fundDaoImpl” ref=“fundDaoImpl”></property>

</bean>

<!– 注册事务管理器 –>

<bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<property name=“dataSource” ref=“dataSource”></property>

</bean>

    <!– 配置事务注解驱动 –>

    <tx:annotation-driven transaction-manager=“transactionManager”/>

</beans>

4. 使用事务注解管理事务

处理函数增加注释:

@Transactional(rollbackFor=FundException.class)

public void buyFund(String aname, double money, String fname, int amount) throws FundException {

accountDaoImpl.updateAccount(aname, money);

if (1==1) {

throw new FundException(“购买基金出现异常!”);

}

fundDaoImpl.updateFund(fname, amount);

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898