我正在为项目使用spring,hibernate和maven。
我下面有一节课
@Service
public class ServiceImpl implements Service ,Serializable{
//Code
}
在这个课程中,我有一个方法声明如下,
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void doSomething(Obj a, Obj b){
//code
}
我对此方法进行了JUnit测试。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:com/mt/sm/application-context.xml", "classpath:com/mt/sm/security-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class ServiceImplTest {
@Test
public void testdoSomething() {
//code
}
}
我在下面遇到此错误
Testcase: testdoSomething(com.mt.sm.services.ServiceImplTest): Caused an ERROR
Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
但是我以同样的方式宣布了一个方法来调用上面的那个
public void call (Obj a, Obj b) {
doSomething(a,b);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void doSomething(Obj a, Obj b){
//code
}
结果,我可以进行测试。 我只是想问为什么这样做没有错误。
答案 0 :(得分:1)
Spring通过AOP Proxies管理交易,因此内部调用从不是事务性的(您不能代理自己)