学习记录:@Transactional 事务不生效
测试时使用spring boot2.2.0,在主类中调用,@Transactional 不起作用,原代码如下:
@SpringBootApplication
@Slf4j
@Component
public class Chapter08TransactionStatementApplication implements CommandLineRunner { @Autowired
private JdbcTemplate jdbcTemplate; public static void main(String[] args) {
SpringApplication.run(Chapter08TransactionStatementApplication.class, args);
} @Override
public void run(String... args) throws Exception {
log.info("before insert:count:" + getCount());
insert();
log.info("after insert:count:" + getCount());
log.info("before insertThenRollback:count:" + getCount());
try {
insertThenRollback();
} catch (Exception e) {
log.info(e.toString());
}
log.info("after insertThenRollback:count:" + getCount());
log.info("before invokeInsertThemRollback:count:" + getCount());
try {
invokeInsertThemRollback();
} catch (Exception e) {
log.info(e.toString());
}
log.info("after invokeInsertThemRollback:count:" + getCount()); } @Transactional
public void insert() {
CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId()); } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
// jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
// callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
jdbcTemplate.execute("insert into call_task values('" + callTask.getId() + "',0,0,'" + callTask.getDataKey() + "','" + format.format(new Date()) + "','"
+ callTask.getCreateUserId() + "','" + format.format(new Date()) + "','" + callTask.getUpdateUserId() + "')");
throw new RollbackException();
} public void invokeInsertThemRollback() throws RollbackException {
insertThenRollback();
} private int getCount() {
return jdbcTemplate.queryForObject("select count(*) from call_task", Integer.class);
}
}
修改为使用serivce调用即可(访问修饰符必须为:public):
@Service
public class CallTaskServiceImpl implements CallTaskService { @Autowired
private JdbcTemplate jdbcTemplate; @Override
@Transactional
public void insert() {
CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId()); } @Override
@Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
// jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
// callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
jdbcTemplate.execute("insert into call_task values('" + callTask.getId() + "',0,0,'" + callTask.getDataKey() + "','" + format.format(new Date()) + "','"
+ callTask.getCreateUserId() + "','" + format.format(new Date()) + "','" + callTask.getUpdateUserId() + "')");
throw new RollbackException();
} @Override
public void invokeInsertThemRollback() throws RollbackException {
insertThenRollback();
} @Override
public int getCount() {
return jdbcTemplate.queryForObject("select count(*) from call_task", Integer.class);
} }
学习记录:@Transactional 事务不生效的更多相关文章
- java注解@Transactional事务类内调用不生效问题及解决办法
@Transactional 内部调用例子 在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成的代理对象来管理,这会造成自调用问题.若同一类中的其他没有@T ...
- SpringBoot 系列教程之事务不生效的几种 case
SpringBoot 系列教程之事务不生效的几种 case 前面几篇博文介绍了声明式事务@Transactional的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑.本文 ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
- 我的Spring学习记录(五)
在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...
- Spring基础系列-Spring事务不生效的问题与循环依赖问题
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...
- 在Ubuntu Server下搭建LAMP环境学习记录
更新于2015/6/16日,因图片地址失效,请在此地址查看:http://note.youdao.com/share/?id=1c249ae6dc6150cbf692adec67b23a33& ...
- 开源项目Material Calendar View 学习记录 (一)
开源项目Material Calendar View 学习记录 Github: https://github.com/prolificinteractive/material-calendarview ...
- WCF学习笔记之事务编程
WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...
- ElasticSearch 学习记录之如任何设计可扩容的索引结构
扩容设计 扩容的单元 一个分片即一个 Lucene 索引 ,一个 Elasticsearch 索引即一系列分片的集合 一个分片即为 扩容的单元 . 一个最小的索引拥有一个分片. 一个只有一个分片的索引 ...
随机推荐
- Js event对象offsetX,pageX,screenX,clientX详解
平时在测量元素位置时难以确定,下面给出具体的event对象中的各种属性,以便日后使用. 检测相对于浏览器的位置:clientX和clientY 当鼠标事件发生时,鼠标相对于浏览器左上 ...
- jquery 实现复选框选择效果
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- 理解Python中的继承规则和继承顺序
先来看一段代码: class First(object): def __init__(self): print ("first") class Second(object): de ...
- 【leedcode】950. Reveal Cards In Increasing Order
题目如下: In a deck of cards, every card has a unique integer. You can order the deck in any order you ...
- PHP chdir() 函数
实例 改变当前的目录: <?php// Get current directoryecho getcwd() . "<br>"; // Change direct ...
- PCB一些设置记录
开始时设置原点,编辑>>原点>>设置 画PCB时,导入后,根据各个模块放好位置 设计>>类>>添加电源类 设计>>规则>>Cle ...
- STM32的结构和启动模式
一.STM32F10x功能模块 32位的Cortex-M3微处理器: 可嵌套的向量中断控制器(NVIC)和60个可屏蔽中断且有16个可编程优先级: 内嵌内存: FLASH:最大512K字节 STAM: ...
- C# 串口总结
一.串口初始化 定义: using System.IO.Ports; SerialPort myPort = new SerialPort() 初始化: //port初始化 public void _ ...
- 爬虫 fake_useragent
import requests from fake_useragent import UserAgent ua = UserAgent() headers = { "UserAgent&qu ...
- 三.通过jenkins对系统的监控(2)
在这记录下,通过HTTP Request Plugin监控系统的异常.和上一篇原理差不多. 1.首先需要安装HTTP Request Plugin.插件安装好后,在系统设置下面会新增如下配置: 具体有 ...