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数据库连接池,阿里出品,肯定是精品的意思咯,这也是我们老大搭建的框架,我就站在前人的肩膀上飞翔了.先前在一个事物里,使用了多条数据库操作,都是正常的,但是前 ...
随机推荐
- 【C++自我精讲】基础系列五 隐式转换和显示转换
[C++自我精讲]基础系列五 隐式转换和显示转换 0 前言 1)C++的类型转换分为两种,一种为隐式转换,另一种为显式转换. 2)C++中应该尽量不要使用转换,尽量使用显式转换来代替隐式转换. 1 隐 ...
- js节流防抖应用场景,以及在vue中节流防抖的具体实现
故事背景: 项目有个需求是输入框在输入的时候进行搜索,展示下拉数据,但是没必要输入一个字都进行搜索,所以想到了在输入结束200毫秒后再进行搜索,从而引出来了 js的节流(throttle),防抖(de ...
- hdu 4284(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4284 思路:类似于poj3311:http://poj.org/problem?id=3311,首先f ...
- VR应用开发遍地走的日子还有多远
从上世纪60年代美国计算机科学家Ivan Sutherland发明的第一款真正意义上的虚拟现实头盔,到Facebook以20亿美元收购"虚拟现实之眼"Oculus Rift,大批厂 ...
- iOS开发之 -- 判断tableview/scrollview的滑动方法,及导航栏渐变的实现代码
开发的过程中,肯定会用到在视图想上滑动的时候,在导航处做一些操作,比如向上滑动的时候,做个动画,出现一个搜索框,或者其他的操作,那么我们怎么来判断它的滑动方向呢? 首先我们应该知道tableview继 ...
- python bottle学习(二)加载配置文件
from bottle import (run, route, get, post, default_app, Bottle) """ 首先获得一个app对象,有两种方法 ...
- 【BZOJ3039】玉蟾宫 单调栈
[BZOJ3039]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子 ...
- vue父子组件传值
1.父组件向子组件传值 例如app.vue是父组件,v-header.vue是子组件,实现app向v-header传值父组件需要自定义自己的title值, 子组件v-header内容 <temp ...
- nodemailer发送邮件各个服务器接口
来自:https://github.com/nodemailer/nodemailer-wellknown/blob/master/services.json#L125 { "1und1 ...
- 使用CocoaPods配置管理开源项目
今天从GitHub下载了MMProcessHUB,想先看看demo,但是不巧的是作者是用CocoaPods配置的,需要安装CocoaPods,CocoaPods是一个第三方的类库管理工具.找了一篇很详 ...