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--以非事务方式执行,如果当前存在事务,则抛出异常。
相关文章推荐
- 未来演进的方向--基于Docker部署微服务
- CentOS下调整home和根分区大小
- Websphere MQ 7 For Linux版安装
- elasticsearch linux 安装
- 使用 PowerShell 自动登录 Azure
- linux nohup命令
- Source order for linux
- linux小知识之目录卸载
- 在centos6.5-64bit上安装wxHexEditor,以查看编译二进制文件
- 10032---百万级访问网站前期的技术准备
- CentOS 安装配置subversion
- nginx详解
- Linux/ubuntu下安装Chrome
- Centos 7 ssh秘钥认证(无需密码ssh)
- Hadoop集群安装配置教程_Hadoop2.6.0_Ubuntu/CentOS
- 使用 PowerShell 自动登录 Azure
- 虚拟机中实现Linux与Windows之间的文件传输
- 10.Shell操作符
- Java Web实战01--Tomcat环境、JSP、Servlet初试
- 使用 PowerShell 自动登录Azure