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数据库连接池,阿里出品,肯定是精品的意思咯,这也是我们老大搭建的框架,我就站在前人的肩膀上飞翔了.先前在一个事物里,使用了多条数据库操作,都是正常的,但是前 ...
随机推荐
- 如果 date_field = TRUNC(date_field) 就说明时分秒为0(也就是不包含),否则就包含时分秒
如果 date_field = TRUNC(date_field) 就说明时分秒为0(也就是不包含),否则就包含时分秒
- 【BZOJ4547】Hdu5171 小奇的集合 矩阵乘法
[BZOJ4547]Hdu5171 小奇的集合 Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这 ...
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- input 和 button 的 border-box 模型和 IE8 错位
用 input 和 button 时出现了几个奇怪的现象,先放几个 input 和 button CSS: * { margin:; padding:; } input,button { width: ...
- 第三课补充01——set类型 sorted类型命令操作详解,redis管道及事务
1. set类型的命令操作: (1)sadd命令:向key指定的set集合添加成员 ##sadd命令:是设置set集合类型的数据,sadd <key> <mumber> [& ...
- cygwin简介,安装及卸载(体验UNIX & Linux环境)
对于爱好者或初学者来说,为了体验UNIX & Linux环境,去安装虚拟机或双系统稍显麻烦,cygwin是一个很好的选择 具/原料 安装windows的电脑一台(可以联网) 法/步骤 ...
- Linux文档编辑 vim
- 安装 oracle 11g 发行版 出现错误:由于以下错误,Enterprise Manager配置失败,启动Database Control时出错
解决方案如下进入dos(1)查看dbconsole状态:emctl status dbconsole 显示:Environment variable ORACLE_SID not defined. P ...
- css中的clear:both,display:flex;
介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...
- Vue(2)- v-model、局部组件和全局组件、父子组件传值、平行组件传值
一.表单输入绑定(v-model 指令) 可以用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定. ...