@Transactional属性详解
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
使用说明
注解失效问题
@Transactional 应用在非 public 修饰的方法上
@Transactional 注解属性 rollbackFor 设置错误
同一个类中方法调用,导致@Transactional失效
异常被你的 catch“吃了”导致@Transactional失效
数据库引擎不支持事务
开启多线程任务时,事务管理会受到影响
@Service
public class ServiceA {
@Transactional
public void threadMethod(){
this.insert();
System.out.println("main insert is over");
for(int a=0 ;a<3;a++){
ThreadOperation threadOperation= new ThreadOperation();
Thread innerThread = new Thread(threadOperation);
innerThread.start();
}
}
public class ThreadOperation implements Runnable {
public ThreadOperation(){
}
@Override
public void run(){
insert();
System.out.println("thread insert is over");
}
}
public void insert(){
//do insert......
}
}
@Service
public class ServiceA {
@Autowired
private ServiceB serviceB;
@Transactional
public void threadMethod(){
this.insert();
System.out.println("main insert is over");
for(int a=0 ;a<3;a++){
ThreadOperation threadOperation= new ThreadOperation();
Thread innerThread = new Thread(threadOperation);
innerThread.start();
}
}
public class ThreadOperation implements Runnable {
public ThreadOperation(){
}
@Override
public void run(){
serviceB.insert();
System.out.println("thread insert is over");
}
}
public void insert(){
//do insert......
}
}
@Service
public class ServiceB {
@Transactional
public void insert(){
//do insert......
}
}
@Service
public class ServiceA {
@Autowired
private ServiceB serviceB;
@Transactional
public void threadMethod(){
this.insert();
System.out.println("main insert is over");
for(int a=0 ;a<3;a++){
ThreadOperation threadOperation= new ThreadOperation();
Thread innerThread = new Thread(threadOperation);
innerThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
try {
serviceB.delete();③
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
innerThread.start();
}
}
public class ThreadOperation implements Runnable {
public ThreadOperation(){
}
@Override
public void run(){
try {
serviceB.insert();
}catch (Exception ex){ ②
System.out.println(" Exception in run ");
throw new RuntimeException();
}
System.out.println("thread insert is over");
}
}
public void insert(){
//do insert......
}
}
@Service
public class ServiceB {
@Transactional
public void insert() throws Exception{ ①
//do insert......
}
@Transactional
public void delete() throws Exception{
//do delete......
}
}
————————————————
版权声明:本文为CSDN博主「丶炜钦」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44521690/article/details/116797363
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/weixin_44521690/article/details/116797363
粉丝福利:Java从入门到入土学习路线图
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论