Spring-事务管理

一、事务回顾

事务:事务指数据库中多个操作合并在一起形成的操作序列。

事务的作用:
在这里插入图片描述
在这里插入图片描述

二、事务管理

1、Spring事务核心对象

在这里插入图片描述

3个API:

1)、PlatformTransactionManager:平台事务管理器

为接口,最终要操作的是他的实现类,它的实现类非常多:
在这里插入图片描述
在这里插入图片描述
此接口定义了事务的基本操作

获取事务:
TransactionStatus getTransaction(TransactionDefinition definition)
// 参数为事务定义对象,返回的是事务的状态
提交事务:
void commit(TransactionStatus status)
回滚事务:
void rollback(TransactionStatus status)
2)、TransactionDefinition:事务定义对象

TransactionDefinition,此接口定义了事务的基本信息

获取事务定义名称: String getName()

获取事务的读取属性: boolean isReadOnly()

获取事务隔离级别: int getIsolationLevel()

​ 值有:int ISOLATION_DEFAULT=-1, int ISOLATION_READ_UNCOMMITTED=1…

获取事务超时时间: int getTimeout() 默认:int TIMEOUT_DEFAULT=-1 (永不超时)

获取事务传播行为特征: int getPropagationBehavior()

3)、TransactionStatus:事务状态

此接口定义了事务在执行过程中某个时间点上的状态信息及对应的操作状态

1、获取事务是否处于新开启事务状态

boolean isNewTransaction()

2、获取事务是否处于已完成状态

boolean isCompleted()

3、获取事务是否处于回滚状态

boolean isRollbackOnly()

4、刷新事务状态

void flush()

5、获取事务是否具有回滚存储点

boolean hasSavepoint()

6、设置事务处于回滚状态

void setRollbackOnly()

2、编程式事务

ApplictionContext.xml 业务层需要注入dataSource

<?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:p="http://www.springframework.org/schema/p"
       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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--加载配置文件,1导入约束content,2通过context标签设置,3,定义成bean对象-->
    <context:property-placeholder location="classpath:*.properties"/>
    <!--    加载druid数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--accoutDao的bean不许定义,使用代理自动生成,需要在AccountServiceImpl定义accountDao,dataSource的Set方法-->
    <bean id="accountService" class="com.lvmanba.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--    spring整合myhabtis后控制的创建连接的对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.lvmanba.domain" />
    </bean>
    <!--    加载myhabtis映射配置的扫描,将其作为spring的bean进行管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lvmanba.dao"/>
    </bean>
<!--    设置AOP-->
    <bean id="txAdvice" class="com.lvmanba.aop.TxAdvice">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <aop:config>
        <!-- 切入点-->
        <aop:pointcut id="pt" expression="execution(* *..transfer(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值