java事务失效的场景,Spring事务失效场景

数据库引擎不支持

如MYSQL的MyIsam就不支持事务操作

场景1 @Transactional标记在了非接口实现的方法上

@Resource

private INoWorkingService noWorkingService;

@Test

void noWorkModel1(){

noWorkingService.noWorkModel1();

}

@Override

public void noWorkModel1() {

noWorkModel1_1();

}

@Transactional

public void noWorkModel1_1(){

User user = new User();

user.setName("noWorkModel1_1");

user.setAge(777);

userDao.save(user);

int q = 1/0;

}

由于业务开发中通常使用接口作为调用对象,如果注解标记在了接口方法调用的方法中,则事务无效

场景2 @Transactional标记在了非public的方法上

@Transactional

void noWorkModel2_2() {

User user = new User();

user.setName("noWorkModel2_2");

user.setAge(777);

userDao.save(user);

int q = 1/0;

}

场景3 发生的异常已捕获

@Transactional

@Override

public void noWorkModel3() {

User user = new User();

user.setName("noWorkModel3");

user.setAge(777);

userDao.save(user);

try {

int q = 1/0;

}catch (Exception e){

e.printStackTrace();

}

}

场景4 A调用B,B开启事务,A没有,A发生异常则B成功提交

@Override

public void noWorkModel4_1() {

noWorkModel4_2();

int q = 1/0;

}

@Override

@Transactional

public void noWorkModel4_2() {

User user = new User();

user.setName("noWorkModel4_2");

user.setAge(777);

userDao.save(user);

}

你可能感兴趣的:(java事务失效的场景)