前言
javaWeb开发人员都知道@org.springframework.transaction.annotation.Transactional注解是居于Spring AOP实现的,这样就出现一个问题:在同一个类中,直接调用类中另一个有@Transactional注解的方法,被调用方法的事务不会启动,代码如下:
import com.xqx.quality.patrol.domain.ATest;
import com.xqx.quality.patrol.mapper.ATestMapper;
import com.xqx.quality.patrol.service.IATestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j
@Service
public class ATestServiceImpl implements IATestService {
@Autowired
private ATestMapper aTestMapper;
@Override
public void addTest() {
this.addTest2("no transaction");
}
@Override
@Transactional
public void addTest2(String p1) {
ATest aTest = new ATest();
aTest.setP1(p1);
aTestMapper.insertATest(aTest);
throw new RuntimeException("test exception");
}
}
测试类如下
import com.xqx.common.utils.JsonUtil;
import com.xqx.quality.patrol.domain.ATest;
import com.xqx.quality.patrol.service.IATestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dev")
public class TransactionalTest {
@Autowired
private IATestService iATestService;
@Test
public void testTransactional() {
iATestService.addTest();
}
}
最终数据库结果如下:
结论:在同一个类中调用有@Transactional直接的方法,事务不会生效
如何使事务生效
既然@Transactional是居于SpringAOP实现,考虑通过在调用出不再直接调用方法,而是通过Spring上下文重新获取到该类的实例,通过实例在调用对应方法来实现。
改造ATestServiceImpl类如下:
import com.xqx.quality.patrol.domain.ATest;
import com.xqx.quality.patrol.mapper.ATestMapper;
import com.xqx.quality.patrol.service.IATestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j
@Service
public class ATestServiceImpl implements IATestService {
@Autowired
private ATestMapper aTestMapper;
@Autowired
private ApplicationContext applicationContext;
@Override
public void addTest() {
// 时候用try{}catch(){}避免影像后续代码执行
try {
applicationContext.getBean(IATestService.class)// 通过spring上下文获取类实例
.addTest2("has transaction");
} catch (Exception e) {
log.error("addTest error", e);
}
try {
this.addTest2("no transaction");
} catch (Exception e) {
log.error("addTest error", e);
}
}
@Override
@Transactional
public void addTest2(String p1) {
ATest aTest = new ATest();
aTest.setP1(p1);
aTestMapper.insertATest(aTest);
throw new RuntimeException("test exception");
}
}
测试类执行后,数据库结果如下:
可以明显看到,"has transactional"这条数据没有插入数据库,并且id被占用了。显而易见,spring事务回滚了。
至此,实现了在同一个类中,方法之间的调用同样适用事务 的要求
当然本人只是提供一种解决思路,这肯定不是唯一的办法。
衍生
不知道有没有细心的读者注意到,我在addTest2方法中,抛出的是RuntimeException,而不是Exception,为什么?
如果有仔细了解过Transactional注解的,这应该觉得很简单:Transactional注解默认捕获运行时异常作为事务回滚的依据,但对编译时异常,则需要在直接上进行申明(如:@Transactional(rollbackFor = Exception.class))才会起效。