您的位置:首页 > 运维架构

Existing transaction found for transaction marked with propagation 'never'

2016-06-28 11:39 459 查看
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.google.common.collect.Maps;//guava-17.0.jar
import com.qf.simulate.common.core.exception.QuarkException;
import com.qf.simulate.plan.service.AMonthlyIncomeCalcService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-config-test.xml"}) //加载配置文件
@TransactionConfiguration(defaultRollback = true)
public class test{

/*****
* @Resource和@Autowired都可以来完成注入依赖,但它们之间是有区 别的。首先来看一下:
a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;
b。@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;
c。@Resource注解是又J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用
d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上
*/

@Autowired(required=true)
@Qualifier("monthlyIncomeCalcServiceImpl")
private AMonthlyIncomeCalcService monthlyIncomeCalcServiceImpl;

@Test
//@Transactional
public void testCreateXdtMonlthy(){

try {
Map<String, String> condition =Maps.newConcurrentMap();//线程安全的map
System.out.println(monthlyIncomeCalcServiceImpl.createXdtMonthlyIncome(condition));
} catch (QuarkException e) {
e.printStackTrace();
}
}
}


service层

@Transactional(propagation=Propagation.NEVER,readOnly=true)
public List createXdtMonthlyIncome(Map<String, String> condition){
//.....
return list;
}


单元测试时,必须把@transition注释掉,因为调用的方法要求在无事务的环境下运行,否则报错

org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation 'never'

Propagation (事务的传播属性)
Propagation : key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: