Spring事务控制 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class},)

发布时间:2024-12-04 08:46

Transactional Leadership(交易型领导)强调任务完成和奖励 #生活技巧# #领导力技巧# #领导风格理论#

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}, isolation = Isolation.DEFAULT, readOnly = false)

1. propagation用法讲解:

1、PROPAGATION_REQUIRED:如果存在一个事务,则支持当前事务。如果没有事务则开启。

2、PROPAGATION_SUPPORTS:如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。

3、PROPAGATION_MANDATORY:如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。

4、PROPAGATION_REQUIRES_NEW:总是开启一个新的事务。如果一个事务存在,则将这个存在的事务挂起。

5、PROPAGATION_NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务。

6、PROPAGATION_NEVER:总是非事务地执行,如果存在一个活动事务,则抛出异常。

7、 PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED属性执行

2.isolation用法讲解
1、 ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。

2、ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。

3、ISOLATION_READ_COMMITTED:保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。

4、ISOLATION_REPEATALBE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻想读。它除了保证一个事务不能读取另外一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。

5、ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不课重复读外,还避免了幻想读。

<?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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"> <import resource="classpath*:spring/spring-config-dao.xml" /> <context:component-scan base-package="com.letv.*.manager" /> <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="batchSave*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="cancelOrder" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="managers" expression="execution(* com.thb.*.manager..*.*(..)) "/> <aop:advisor advice-ref="txAdvice" pointcut-ref="managers"/> </aop:config> </beans>

12345678910111213141516171819202122232425262728293031323334353637383940414243

网址:Spring事务控制 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class},) https://www.yuejiaxmz.com/news/view/370547

相关内容

Spring Boot 事务的简单使用
基于Spring Boot的智能家居控制系统的设计与实现
基于Spring Boot和Android的旧物交易平台与实现(源码+LW+调试+讲解)
Spring定时任务注解说明
为什么越来越多的开发者选择使用Spring Boot?
使用Spring@Scheduled(cron = “0 0/10 * * * ?”) 实现定时任务
Spring中文件的上传下载
基于Spring Boot+Vue的校园二手交易平台
格力空调智能控制:Java编程实现家居自动化系统
基于Spring Boot + Vue + MySQL的公益旧物捐赠系统

随便看看