软件编程
位置:首页>> 软件编程>> java编程>> Spring事务管理方法步骤解析

Spring事务管理方法步骤解析

作者:幸福的小耗子  发布时间:2021-12-15 20:09:04 

标签:spring,事务,管理,方法,步骤

1、Spring的事务管理主要包括3个接口

TransactionDefinition:封装事务的隔离级别,超时时间,是否为只读事务和事务的传播规则等事务属性,可通过XML配置具体信息。

PlatformTransactionManager:根据TransactionDefinition提供的事务属性配置信息,创建事务。

TransactionStatus:封装了事务的具体运行状态。比如,是否是新开启事务,是否已经提交事务,设置当前事务为rollback-only等。

2、Spring的事务管理:

1、PlatformTransactionManager:接口统一,抽取处理事务操作相关的方法;

(1):TransactionStatus getTransaction(TransactionDefinition definition): 根据事务定义信息从事务环境中返回一个已存在的事务,或者创建一个新的事务,并用TransactionStatus描述该事务的状态。

(2):void commit(TransactionStatus status): 根据事务的状态提交事务,如果事务状态已经标识为rollback-only,该方法执行回滚事务的操作。

(3):void rollback(TransactionStatus status): 将事务回滚,当commit方法抛出异常时,rollback会被隐式调用

2、在使用spring管理事务的时候,首先得告诉spring使用哪一个事务管理器;

3、常用的事务管理器:

DataSourceTransactionManager:使用JDBC,MyBatis的事务管理器;

HibernateTransactionManager:使用Hibernate的事务管理器;

3、步骤

第一步:配置Spring的事务管理器(需要用的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: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/context
   http://www.springframework.org/schema/context/spring-context.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">

<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/>
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
   <property name="driverClassName" value="${jdbc.driverClassName}"/>
   <property name="url" value="${jdbc.url}"/>
   <property name="username" value="${jdbc.username}"/>
   <property name="password" value="${jdbc.password}"/>
 </bean>
 <bean id="service" class="com.test.tx.service.impl.AccountServiceImpl">
   <property name="accountDao" ref="accountDao"/>
 </bean>
 <bean id="accountDao" class="com.test.tx.dao.impl.AccountDaoImpl">
   <property name="dataSource" ref="dataSource"/>
 </bean>

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

<!--配置事务-->
 <aop:config>
   <!--配置切入点,这里写自己想要使用Spring事务管理的类或接口,在上篇博客中有切入点配置方法-->
   <aop:pointcut id="pointcut" expression="execution( * com.test.tx.service.IAccountService.*(..))"/>
   <!--配置切面-->
   <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
 </aop:config>
 <!--事务增强器-->
 <tx:advice id="advice" transaction-manager="manager">
   <tx:attributes>
     <!--read-only可以将查询的方法设为只读事务-->
     <tx:method name="*" read-only="false"/>
   </tx:attributes>
 </tx:advice>
</beans>

第三步:进行事务的测试

4、事务的注解配置方式

第一步:加载驱动


<!--事务的注解驱动,注解解析器需要关联事务管理器-->
 <tx:annotation-driven transaction-manager="manager"/>

第二步:在实现类上添加注解@Transactional注解中相应的属性可以配置事务控制的相关细节(隔离级别/传播规则/是否只读等)

类中的方法也可以添加@Transactional注解,同样可以对方法进行细节配置,方法中的配置信息会覆盖类中的同名配置。

来源:https://www.cnblogs.com/xfdhh/p/11488444.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com