Connection.setAutoCommit使用的注意事项
http://blog.csdn.net/xiayimiaokuaile/article/details/6422032
setAutoCommit总的来说就是保持数据的完整性,一个系统的更新操作可能要涉及多张表,需多个SQL语句进行操作
循环里连续的进行插入操作,如果你在开始时设置了:conn.setAutoCommit(false);
最后才进行conn.commit(),这样你即使插入的时候报错,修改的内容也不会提交到数据库,
而如果你没有手动的进行setAutoCommit(false);
出错时就会造成,前几条插入,后几条没有
会形成脏数据~~
setAutoCommit(false)的误用
(设定setAutoCommit(false)没有在catch中进行Connection的rollBack操作,操作的表就会被锁住,造成数据库死锁):
误用Connection.setAutoCommit导致的数据库死锁问题。
Newly created Connection objects are in auto-commit mode by default, which means that individual SQL statements are committed automatically when the statement is completed. To be able to group SQL statements into transactions and commit them or roll them back as a unit, auto-commit must be disabled by calling the method setAutoCommit with false as its argument. When auto-commit is disabled, the user must call either the commit or rollback method explicitly to end a transaction.(一定不能大意哦,如果设置成非自动提交,在最后一定要调用commit或者rollback方法)
The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet object, the statement completes when the last row of the result set has been retrieved or the ResultSet object has been closed. In advanced cases, a single statement may return multiple results as well as output parameter values. In this case, the commit may occur when all results and output parameter values have been retrieved, or the commit may occur after each result is retrieved.
throw new RuntimeException(e);
Connection.setAutoCommit使用的注意事项的更多相关文章
- 由于没有正确使用Connection.setAutoCommit(false)而导致SQL语句没有被提交
症状: 提交了Form,执行insert操作,经过Debug也确认PreparedStatement.executeUpdate()返回值>0,但是在MySQL中直接查询表,返回的仍然是Empt ...
- Java Connection.setAutoCommit
Java setAutoCommit 默认为true,即每条SQL语句在各自的一个事务中执行. 很多时候需要有多个操作在一个事务执行,如循环插入,此时可在插入开始前设置 conn.setAutoCom ...
- Java数据库操作(JDBC)
JDBC Java数据库连接(Java DataBase Connectivity,JDBC)用于在Java程序中实现数据库操作功能,它提供了执行SQL语句.访问各种数据库的方法,并为各种不同的数据库 ...
- JDBC Connection
[ http://shift-alt-ctrl.iteye.com/blog/1967020] 关于JDBC中关于Connection的两个疑问: 1.Connection实例是线程安全的吗? ...
- 探索多线程使用同一个数据库connection的后果
在项目中看到有用到数据库的连接池,心里就思考着为什么需要数据库连接池,只用一个连接会造成什么影响?(只用一个connection)? 1 猜想:jdbc的事务是基于connection的,如果多线程 ...
- [原理][源代码解析]spring中@Transactional,Propagation.SUPPORTS,以及 Hibernate Session,以及jdbc Connection关系---转载
问题: 一. 1. Spring 如何处理propagation=Propagation.SUPPORTS? 2. Spring 何时生成HibernateSession ? 3. propagati ...
- [原理][来源解析]spring于@Transactional,Propagation.SUPPORTS,以及 Hibernate Session,以及jdbc Connection关联
Spring 捆绑Hibernate. 夹: 一. 1. Spring 怎样处理propagation=Propagation.SUPPORTS? 2. Spring 何时生成HibernateSe ...
- 事务处理中如何获取同一个connection 对象
运用线程内部的map属性,将对象绑定到ThreadLocal中: 具体实现: 1.新建一个绑定Connection对象的单例类 public class ConnectionBind { privat ...
- connection holder is null新增解决方案(2018-06-02)
最近在做Java后台的项目,用到了druid数据库连接池,阿里出品,肯定是精品的意思咯,这也是我们老大搭建的框架,我就站在前人的肩膀上飞翔了.先前在一个事物里,使用了多条数据库操作,都是正常的,但是前 ...
随机推荐
- 在Intellij IDEA下用X-debug调试PHP
用Intellij IDEA使用X-debug来调试PHP,主要需要配置的部分有三个地方,分别为php.ini的配置,IDEA的配置和浏览器的配置,主要如下: php.ini(wamp修改的是phpF ...
- iOS项目开发实战——自己定义圆形进度提示控件
iOS中默认的进度条是水平方向的进度条,这往往不能满足我们的需求. 可是我们能够自己定义类似的圆形的进度提示控件,主要使用iOS中的画图机制来实现. 这里我们要实现一个通过button点击然后圆形进度 ...
- Spring工具类
文件资源访问 1.统一资源访问接口 Resource 2.实现类 FileSystemResource 通过文件系统路径访问 ClassPathResource 通过classpath路径访问 Ser ...
- Vimium、CrxMouse配置信息
每次使用别的地方的Chrome的时候,虽然Vimium插件能同步过来,但是配置信息不在,所以先记录在整理以备不时之需. 这个是Vimium的配置信息,然后我还会把搜索引擎改为http://www.ba ...
- Pycharm 2017 12月最新激活码
激活的办法:这个必须的联网才可以使用(每次打开PyCharm都需要电脑联网才可以正常使用),要是没网的话,就不能激活使用啦,大家注意哈. http://idea.iteblog.com/key.php ...
- 赢在面试之Java多线程(十一)
121,什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完 ...
- docker 中安装 FastDFS 总结
如题,参考各资料后,安装FastDFS总结.基于已有docker镜像 https://hub.docker.com/r/luhuiguo/fastdfs/ docker pull luhuiguo/f ...
- Scrapy命令和备注
Scrapy命令和备注 1.创建一个新项目(命令行) project是项目名 scrapy startproject <project_name> 2.调试项目(pycharm) 在pyc ...
- [LintCode] 合并排序数组II
class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...
- 锚点链接 阻止a标签跳转
参考 http://blog.csdn.net/awe5566/article/details/22583699 href="#downJacket" 锚点链接 必须写: 但又 ...