Spring事务管理[通俗易懂]

Spring事务管理[通俗易懂]1、Spring的事务管理主要包括3个接口TransactionDefinition:封装事务的隔离级别,超时时间,是否为只读事务和事务的传播规则等事务属性,可通过XML配置具体信息。Platfo

大家好,又见面了,我是你们的朋友全栈君。

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注解,同样可以对方法进行细节配置,方法中的配置信息会覆盖类中的同名配置。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/154382.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号