@Transactional(timeout = 10)
表示设置事务的超时时间为10秒
表示超过10秒如果该事务中所有的DML语句还没有执行完毕的话,最终结果会选择回滚
默认值-1,表示没有时间限制。
如果最后一条DML语句后面还有很多业务逻辑,这些业务代码执行的时间不被计入超时时间
package com.service;
import com.dao.AccountDao;
import com.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-09 07:09
**/
@Service
public class Service02 {
@Resource(name="accountDao")
public AccountDao accountDao;
@Transactional(timeout = 10)
public void update(Account act) {
accountDao.update(act);
// 睡眠一会
try {
Thread.sleep(1000 * 20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
test:
package com;
import com.pojo.Account;
import com.service.Service01;
import com.service.Service02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-09 07:47
**/
public class IsolationTest {
@Test
public void testIsolation2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Service02 i2 = applicationContext.getBean("service02", Service02.class);
Account act = new Account("004", 1000.0);
i2.update(act);
}
}
Before testing
文章来源地址https://www.toymoban.com/news/detail-489029.html
After testing
// 睡眠一会 try { Thread.sleep(1000 * 20); } catch (InterruptedException e) { e.printStackTrace(); }这里睡眠的 20 秒在最后一条DML语句之后,不计入 timeout 时间之内
交换执行顺序,先休眠20秒,再执行DML语句:
package com.service;
import com.dao.AccountDao;
import com.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-09 07:09
**/
@Service
public class Service02 {
@Resource(name="accountDao")
public AccountDao accountDao;
@Transactional(timeout = 10)
public void update(Account act) {
// 睡眠一会
try {
Thread.sleep(1000 * 20);
} catch (InterruptedException e) {
e.printStackTrace();
}
accountDao.update(act);
}
}
Before testing
retest:
After testing :
数据未更新
再最后一条DML语句之前,休眠20秒, 会计入 timeout 时间之内,事务超时,回滚文章来源:https://www.toymoban.com/news/detail-489029.html
到了这里,关于spring 事务超时的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!