Spring事务管理方法步骤解析

网友投稿 229 2022-12-27


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)

第二步:配置事务

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

第三步:进行事务的测试

4、事务的注解配置方式

第一步:加载驱动

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

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:显卡怎么做接口测试工具(显卡怎么做接口测试工具图)
下一篇:api测试工具 安卓(API测试工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~