测试时使用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 事务不生效的更多相关文章

  1. java注解@Transactional事务类内调用不生效问题及解决办法

    @Transactional 内部调用例子 在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成的代理对象来管理,这会造成自调用问题.若同一类中的其他没有@T ...

  2. SpringBoot 系列教程之事务不生效的几种 case

    SpringBoot 系列教程之事务不生效的几种 case 前面几篇博文介绍了声明式事务@Transactional的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑.本文 ...

  3. 我的Spring学习记录(四)

    虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...

  4. 我的Spring学习记录(五)

    在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...

  5. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  6. 在Ubuntu Server下搭建LAMP环境学习记录

    更新于2015/6/16日,因图片地址失效,请在此地址查看:http://note.youdao.com/share/?id=1c249ae6dc6150cbf692adec67b23a33& ...

  7. 开源项目Material Calendar View 学习记录 (一)

    开源项目Material Calendar View 学习记录 Github: https://github.com/prolificinteractive/material-calendarview ...

  8. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  9. ElasticSearch 学习记录之如任何设计可扩容的索引结构

    扩容设计 扩容的单元 一个分片即一个 Lucene 索引 ,一个 Elasticsearch 索引即一系列分片的集合 一个分片即为 扩容的单元 . 一个最小的索引拥有一个分片. 一个只有一个分片的索引 ...

随机推荐

  1. shutdown - 关闭系统

    总览 SYNOPSIS /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message] 描述 DESCRIPTION shutdown 以一种安全 ...

  2. macos系统安装nginx

    MacOS系统安装软件: macos系统下没有yum和apt-get命令,要安装软件需要使用homebrew. 1.安装homebrew: 安装:/usr/bin/ruby -e "$(cu ...

  3. 备份Oracle 数据库。

    #!/bin/bash# 2018-07-07 oracle database back#filename=`date +%Y%m%d`filename=`date +%Y_%m_%d_%H%M`di ...

  4. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  5. Axure 部件的交互样式

    选中的 时  需要设置事件

  6. 【Zookeekper】分布锁Curator

    有序节点:假如当前有一个父节点为/lock,我们可以在这个父节点下面创建子节点:zookeeper提供了一个可选的有序特性,例如我们可以创建子节点“/lock/node-”并且指明有序,那么zooke ...

  7. 【Flutter学习】之绘画实例(二)

    一,画路径 - drawPath(Path path, Paint paint)  Path 主要有方法如下: 直接描述路径的方法还可以细分为两组:添加子图形和画线(直线或曲线) addXXX() - ...

  8. c#发送邮件功能

    protected void Page_Load(object sender, EventArgs e)    {        //先到qq邮箱设置中启用smtp服务        Random r ...

  9. redis常用命令-2

    redis常用命令 type your_key #查看Key类型 del your_key #删除key keys * #所有key info #信息 /usr/local/bin/redis-cli ...

  10. BZOJ 4545

    bzoj 4545 给定一个踹树,支持几种操作. 本质不同子串询问 加入子树 询问字符串\(S\) 在树上的出现次数. 好码好码 重点就是维护\(parent\) 树,考虑用\(LCT\)维护此树. ...