Spring 事务传播行为 Transaction Propagation

//事务注解用法
@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor=Exception.class,timeout=10)
public void save(Goods goods) throws Exception {...}

除了 propagation 其它几个属性都是字面意思,下面就看下 propagation

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.annotation;

import org.springframework.transaction.TransactionDefinition;

/**
 * Enumeration that represents transaction propagation behaviors for use
 * with the {@link Transactional} annotation, corresponding to the
 * {@link TransactionDefinition} interface.
 *
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.2
 */
public enum Propagation {

	/**
	 * 你有事务我就用你的,你没有我就自己创建一个,反正老子就是要事务。
	 */
	REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),

	/**
	 * 你有事务我就用你的,你没有就算了,我也懒得麻烦。
	 */
	SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),

	/**
	 * 你没事务给我用还敢调用老子,异常走你~~~~
	 */
	MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),

	/**
	 * 老子不管你那套,你有事务是你的事,一边待着。老子自己得创建自己的事务。
	 */
	REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),

	/**
	 * 老子不要事务,你调用我就了不起啊,让你的事务一边待着。我就按非事务方式跑
	 */
	NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

	/**
	 * 老子说了不要事务你还来,异常走你~~~~~~~~~~~
	 */
	NEVER(TransactionDefinition.PROPAGATION_NEVER),

	/**
	 * 先说好,你有没有事务,老子就创建新的。你要是有我就在你事务下创建嵌套事件。我提交、回滚与你无关。
	 */
	NESTED(TransactionDefinition.PROPAGATION_NESTED);

	private final int value;

	Propagation(int value) { this.value = value; }

	public int value() { return this.value; }

}

案列

事务A中调用 方法B(B无事务)

当方法B异常时,A方法中的其它部分不希望受到影响。

  1. 方法B不加事务
  2. 方法B中不要捕获异常,任它向上抛
  3. 在方法A中 try catch 方法B

参考资料

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/TransactionDefinition.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值