3.1.4 Spring的事务管理

四、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);

}

猜你喜欢

转载自www.cnblogs.com/kendyho/p/10748331.html