MySQL数据库的回滚失败(JAVA)

这几天在学习MySQL数据的知识,有一个小测试,用来测试数据库的提交和回滚。

刚开始的时候真的没把这个当回事,按照正常的步骤来讲的话,如下所示,加载驱动,获取数据库的连接,并且把数据库的自动提交给关闭掉。

1 //加载驱动
2 Class.forName(driver);
3 //获取数据库的连接
4 connection = DriverManager.getConnection(url, user, password);
5 //关闭数据库事务的自动提交,默认值为true
6 connection.setAutoCommit(false);

在做测试的时候,自己使用的是建表语句,总共三句话,最后一句是错误的,按照正常情况来讲,执行错误语句以后,程序会跳转到catch代码块中,执行回滚语句。但是在刷新数据库以后,查看数据库中的表格,会发现数据库多了两张表,前两条语句执行成功,并没有回滚。

 1 String[] sqls={
 2                 "create table A (Sno varchar(100) ,Sname varchar(20))",
 3                 "create table B (Cno varchar(100) ,Cname varchar(20))",
 4                 "create table C (Sno varchar(100),Cno varchar(100),Grade  )"
 5         };
 6                
 7         Connection connection = JDBCUtil.getConnection();//获取数据库的连接
 8         try {
 9             
10             Statement stmt=connection.createStatement();
11             
12             for(String sql:sqls){
13             stmt.execute(sql);
14             }
15                  
16             connection.commit();  //提交事务
17         } catch (SQLException e) {
18             e.printStackTrace();
19                 try {
20                                       
21                     connection.rollback();//事务的回滚
22                 } catch (SQLException e1) {
23                     // TODO Auto-generated catch block
24                     e1.printStackTrace();
25                 }
26         
27         
28         }    

后来在网上查了一些资料,得知在MySQL中,对表的创建、删除和更改表结构等操作是回滚无效的,DDL操作直接触发隐式提交,任何事务回滚的前提是没有commit,隐式提交直接执行commit了,后续的rollback对前面的语句无效。如果想对建表语句回滚的话,可以在catch语句块里面,模拟回滚操作,判断表格否存在,存在就删除即可。

转载于:https://www.cnblogs.com/tudou1179006580/p/9159005.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389476.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

条件概率分布_条件概率

条件概率分布If you’re currently in the job market or looking to switch careers, you’ve probably noticed an increase in popularity of Data Science jobs. In 2019, LinkedIn ranked “data scientist” the №1 most promising job in the U.S. based on job openin…

MP实战系列(十七)之乐观锁插件

声明,目前只是仅仅针对3.0以下版本,2.0以上版本。 意图: 当要更新一条记录的时候,希望这条记录没有被别人更新 乐观锁实现方式: 取出记录时,获取当前version 更新时,带上这个version 执行更新时…

二叉树删除节点,(查找二叉树最大值节点)

从根节点往下分别查找左子树和右子树的最大节点,再比较左子树,右子树,根节点的大小得到结果,在得到左子树和右子树最大节点的过程相似,因此可以采用递归的 //树节点结构 public class TreeNode { TreeNode left;…

Tensorflow框架:InceptionV3网络概念及实现

卷积神经网络迁移学习-Inception • 有论文依据表明可以保留训练好的inception模型中所有卷积层的参数,只替换最后一层全连接层。在最后 这一层全连接层之前的网络称为瓶颈层。 • 原理:在训练好的inception模型中,因为将瓶颈层的输出再通过…

View详解(4)

在上文中我们简单介绍了Canvas#drawCircle()的使用方式,以及Paint#setStyle(),Paint#setStrokeWidth(),Paint#setColor()等相关函数,不知道小伙伴们了解了多少?那么是不是所有的图形都能通过圆来描述呢?当然不行,那么熟…

成为一名真正的数据科学家有多困难

Data Science and Machine Learning are hard sports to play. It’s difficult enough to motivate yourself to sit down and learn some maths, let alone to becoming an expert on the matter.数据科学和机器学习是一项艰巨的运动。 激励自己坐下来学习一些数学知识是非常…

Ubuntu 装机软件

Ubuntu16.04 软件商店闪退打不开 sudo apt-get updatesudo apt-get dist-upgrade# 应该执行一下更新就好,不需要重新安装软件中心 sudo apt-get install –reinstall software-center Ubuntu16.04 深度美化 https://www.jianshu.com/p/4bd2d9b1af41 Ubuntu18.04 美化…

数据分析中的统计概率_了解统计和概率:成为专家数据科学家

数据分析中的统计概率Data Science is a hot topic nowadays. Organizations consider data scientists to be the Crme de la crme. Everyone in the industry is talking about the potential of data science and what data scientists can bring in their BigTech and FinT…

Keras框架:Mobilenet网络代码实现

Mobilenet概念: MobileNet模型是Google针对手机等嵌入式设备提出的一种轻量级的深层神经网络,其使用的核心思想便是depthwise separable convolution。 Mobilenet思想: 通俗地理解就是3x3的卷积核厚度只有一层,然后在输入张量上…

clipboard 在 vue 中的使用

简介 页面中用 clipboard 可以进行复制粘贴&#xff0c;clipboard能将内容直接写入剪切板 安装 npm install --save clipboard 使用方法一 <template><span>{{ code }}</span><iclass"el-icon-document"title"点击复制"click"co…

数据驱动开发_开发数据驱动的股票市场投资方法

数据驱动开发Data driven means that your decision are driven by data and not by emotions. This approach can be very useful in stock market investment. Here is a summary of a data driven approach which I have been taking recently数据驱动意味着您的决定是由数据…

前端之sublime text配置

接下来我们来了解如何调整sublime text的配置&#xff0c;可能很多同学下载sublime text的时候就是把它当成记事本来使用&#xff0c;也就是没有做任何自定义的配置&#xff0c;做一些自定义的配置可以让sublime text更适合我们的开发习惯。 那么在利用刚才的命令面板我们怎么打…

python 时间序列预测_使用Python进行动手时间序列预测

python 时间序列预测Time series analysis is the endeavor of extracting meaningful summary and statistical information from data points that are in chronological order. They are widely used in applied science and engineering which involves temporal measureme…

keras框架:目标检测Faster-RCNN思想及代码

Faster-RCNN&#xff08;RPN CNN ROI&#xff09;概念 Faster RCNN可以分为4个主要内容&#xff1a; Conv layers&#xff1a;作为一种CNN网络目标检测方法&#xff0c;Faster RCNN首先使用一组基础的convrelupooling层提取 image的feature maps。该feature maps被共享用于…

算法偏见是什么_算法可能会使任何人(包括您)有偏见

算法偏见是什么在上一篇文章中&#xff0c;我们展示了当数据将情绪从动作中剥离时会发生什么 (In the last article, we showed what happens when data strip emotions out of an action) In Part 1 of this series, we argued that data can turn anyone into a psychopath, …

大数据笔记-0907

2019独角兽企业重金招聘Python工程师标准>>> 复习: 1.clear清屏 2.vi vi xxx.log i-->edit esc-->command shift:-->end 输入 wq 3.cat xxx.log 查看 --------------------------- 1.pwd 查看当前光标所在的path 2.家目录 /boot swap / 根目录 起始位置 家…

Tensorflow框架:目标检测Yolo思想

Yolo-You Only Look Once YOLO算法采用一个单独的CNN模型实现end-to-end的目标检测&#xff1a; Resize成448448&#xff0c;图片分割得到77网格(cell)CNN提取特征和预测&#xff1a;卷积部分负责提取特征。全链接部分负责预测&#xff1a;过滤bbox&#xff08;通过nms&#…

线性回归非线性回归_了解线性回归

线性回归非线性回归Let’s say you’re looking to buy a new PC from an online store (and you’re most interested in how much RAM it has) and you see on their first page some PCs with 4GB at $100, then some with 16 GB at $1000. Your budget is $500. So, you es…

朴素贝叶斯和贝叶斯估计_贝叶斯估计收入增长的方法

朴素贝叶斯和贝叶斯估计Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works wi…

numpy统计分布显示

import numpy as np from sklearn.datasets import load_iris dataload_iris()petal_lengthnumpy.array(list(len[2]for len in data[data]))#取出花瓣长度数据 print(np.max(petal_length))#花瓣长度最大值 print(np.mean(petal_length))#花瓣长度平均值 print(np.std(petal_l…