//事务注解用法
@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方法中的其它部分不希望受到影响。
- 方法B不加事务
- 方法B中不要捕获异常,任它向上抛
- 在方法A中 try catch 方法B