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


二、事务管理
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(

1181

被折叠的 条评论
为什么被折叠?



