学习记录:@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 索引即一系列分片的集合 一个分片即为 扩容的单元 . 一个最小的索引拥有一个分片. 一个只有一个分片的索引 ...
随机推荐
- REST接口设计
REST接口设计 为什么要有REST 在传统上,软件和网络是两个不同的领域,很少有交集:软件开发主要针对单机环境,网络则主要研究系统之间的通信.互联网的兴起,使得这两个领域开始融合,现在我们必须考虑, ...
- 强大的性能监控pidstat
前言 pidstat 可以监控单个任务.比如CPU.内存.IO.上下文切换.详细参考 man pidstat 安装 yum install sysstat 使用 1.监控所有活动进程 pidstat ...
- Java jvisualvm简要说明(转)
转自:http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程 ...
- Python3.5-20190506-廖老师-自我笔记函数
函数就是将你的代码封装起来,可以重复利用.不需要每次就写重复的代码 def 函数名(位置参数,默认参数=10,可变参数,关键字参数): 代码块 return 值 定义函数时,需要确定函数名和参数个数: ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...
- SparkStreaming反压机制
一.背景 在默认情况下,Spark Streaming 通过 receivers (或者是 Direct 方式) 以生产者生产数据的速率接收数据.当 batch processing time > ...
- yii2 后台前后台 前后台登陆、退出问题
问题描述:我使用前后台分离 配置如下: 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => ...
- QGroundControl编译出错记录
运行时出现错误: /home/myfly2/Downloads/qgroundcontrol/QGroundControl: error : cannot open shared object fil ...
- Springboot 拦截器配置(登录拦截)
Springboot 拦截器配置(登录拦截) 注意这里环境为springboot为2.1版本 1.编写拦截器实现类,实现接口 HandlerInterceptor, 重写里面需要的三个比较常用的方 ...
- bash: service: command not found 错误的解决方法
service命令是要用ROOT用户来执行的,而出错的用户是用su root切换到ROOT用户下,这个命令没有也不会把环境带过去! 用如下命令就不会出错了:su - root 注意:su 后面是一个空 ...