Spring中的事务管理

事务的回顾

1、什么是事务?

事务:逻辑上的一组操作,组成这组操作的各个语句,要么全都成功,要么全都失败。


2、事务的特性

原子性:事务不可分割
一致性:事务执行前后数据完整性保持一致
隔离性:一个事务的执行不应该受到其他事务的干扰
持久性:一旦事务结束,数据就持久化到数据库


3、如果不考虑隔离性引发安全性问题

(1)读的问题:
脏读 :一个事务读到另一个事务未提交的数据。
不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致。
虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。

(2)写的问题
丢失更新


4、解决读问题

设置事务的隔离级别
Read uncommitted :读未提交,任何读问题解决不了。
Read committed :读已提交,解决脏读,但是不可重复读和虚读有可能发生。
Repeatable read :可重复读,解决脏读和不可重复读,但是虚读有可能发生。
Serializable :串行化执行,解决所有读问题,但同时效率变低了。

Oracle默认使用读已提交,MySQL默认使用可重复读。


Spring中事务管理的API类

1、PlatformTransactionManager:平台事务管理器

平台事务管理器:是一个接口,是Spring用于管理事务的真正的对象。

实现类有:
DataSourceTransactionManager :底层使用JDBC管理事务
HibernateTransactionManager :底层使用Hibernate管理事务


2、TransactionDefinition :事务定义信息

事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读。


3、TransactionStatus:事务的状态

事务状态:用于记录在事务管理过程中,事务的状态的对象。


4、事务管理的API之间的关系

Spring进行事务管理的时候,首先平台事务管理器(PlatformTransactionManager)根据事务定义信息(TransactionDefinition)进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态(TransactionStatus)的对象中。


Spring中事务的传播行为

Spring中提供了七种事务的传播行为:分为三类。
只需记住每类的第一个。

1、保证多个操作在同一个事务中

(1)PROPAGATION_REQUIRED :默认值,如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作AB都包含进来。

(2)PROPAGATION_SUPPORTS :支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。

(3)PROPAGATION_MANDATORY :如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。


2、保证多个操作不在同一个事务中

(1)PROPAGATION_REQUIRES_NEW :如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,只包含自身操作。

(2)PROPAGATION_NOT_SUPPORTED :如果A中有事务,将A的事务挂起。不使用事务管理。

(3)PROPAGATION_NEVER :如果A中有事务,报异常。


3、嵌套式事务

PROPAGATION_NESTED :嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。


Spring的事务管理

1、搭建Spring的事务管理的环境

(1)创建Service的接口和实现类

public interface AccountService {

    public void transfer(Integer fromId, Integer toId, Double money);
}
public class AccountServiceImpl implements AccountService {

    @Resource(name = "accountDao")
    private AccountDao accountDao;

    @Override
    public void transfer(Integer fromId, Integer toId, Double money) {
        accountDao.outMoney(fromId, money);
        //int i = 1 / 0;  // 模拟中间出现异常
        accountDao.inMoney(toId, money);
    }
}

(2)创建DAO的接口和实现类

public interface AccountDao {

    void outMoney(Integer fromId, Double money);

    void inMoney(Integer toId, Double money);
}
// 继承JdbcDaoSupport可以获取Jdbc模板,不需要属性注入
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public void outMoney(Integer fromId, Double money) {
        String sql = "update t_act set balance = balance - ? where id = ?";
        this.getJdbcTemplate().update(sql, money, fromId);
    }

    @Override
    public void inMoney(Integer toId, Double money) {
        String sql = "update t_act set balance = balance + ? where id = ?";
        this.getJdbcTemplate().update(sql, money, toId);
    }
}

2、配置Service和DAO:交给Spring管理

<bean id="accountService" class="com.pipi.spring.tx.demo1.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.pipi.spring.tx.demo1.AccountDaoImpl"></bean>

3、配置连接池和JDBC的模板

<!-- 引入jdbc属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置C3P0数据连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClass}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

4、在DAO注入Jdbc的模板:

<bean id="accountDao" class="com.pipi.spring.tx.demo1.AccountDaoImpl">
    <!-- 注入dataSource连接池 -->
    <property name="dataSource" ref="dataSource" />
</bean>

5、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:com/pipi/spring/tx/demo1/tx1.xml")
public class Spring_tx_Test {

    @Resource(name = "accountService")
    private AccountService accountService;

    @Test
    public void test01() {
        accountService.transfer(1001, 1002, 500d);
    }
}

Spring的事务管理:
第一类:编程式事务(了解,需要手动编写代码)

1、第一步:配置平台事务管理器

<!-- 配置平台事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

2、第二步:Spring提供了事务管理的模板类

<!-- 配置事务的管理的模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="dataSourceTransactionManager" />
</bean>

3、第三步:在业务层注入事务管理的模板

<bean id="accountService" class="com.pipi.spring.tx.demo1.AccountServiceImpl">
    <property name="accountDao" ref="accountDao" />
    <!-- 注入事务管理的模板 -->
    <property name="transactionTemplate" ref="transactionTemplate" />
</bean>

4、编写事务管理的代码

@Override
public void transfer(Integer fromId, Integer toId, Double money) {
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            accountDao.outMoney(fromId, money);
            int i = 1 / 0;  // 模拟中间出现异常
            accountDao.inMoney(toId, money);
        }
    });
}

5、测试:

@Test
public void test01() {
    accountService.transfer(1001, 1002, 500d);
}

Spring的事务管理:二类:声明式事务管理(通过AOP配置实现)

1、XML方式的声明式事务管理

(1)第一步:引入aop的开发包
(2)第二步:恢复转账环境,即中间出现异常,钱会转丢。
(3)第三步:配置事务管理器

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

(4)第四步:配置增强

<!-- 配置事务的通知增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 事务管理的规则 -->
        <!--<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="find*" read-only="true" />-->
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

(5)第五步:AOP的配置

<!-- 配置AOP,切入点与切面,切面用advisor,一个通知与一个切入点的组合 -->
<aop:config>
    <aop:pointcut id="pointcut1" expression="execution(* com.pipi.spring.tx.demo2.AccountServiceImpl.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1" />
</aop:config>

(6)测试程序

@Test
public void test01() {
    accountService.transfer(1001, 1002, 500d);
}

2、注解方式的声明式事务管理

(1)第一步:引入aop的开发包
(2)第二步:恢复转账环境
(3)第三步:配置事务管理器

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

(4)第四步:开启注解事务

<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

(5)第五步:在业务层类上添加注解

@Transactional
public class AccountServiceImpl implements AccountService { 
	// ......
}

(6测试程序的执行:

@Test
public void test01() {
    accountService.transfer(1001, 1002, 500d);
}

声明式事务管理的XML方式,与注解方式都有优点:

XML方式的事务管理,能让匹配上的所有类中的方法都使用配置好的事务管理。

而注解方式极其方便,但需要在所有的所需类上添加注解。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值