MySql 中的setAutoCommit方法
引言
setAutoCommit方法用一句话说就是用来保持事务完整性。一个系统的更新操作可能涉及多张表,这个时候,就须要用多个Sql语句来实现,实际上我认为这个东西就是用来实现事务的。
当我们进行多条数据进行增删改的时候,一旦在一句sql中出现了错误,就会出现有部分数据已经成功。而后面的数据就没有办法运行。这个时候,就会出现脏数据。
因此我们使用setAutoCommit方法,这种方法有一个參数。參数值为Boolean,当true的时候可启用自己主动提交模式,false可禁用该模式。
凝视中有一句话是这样说的:Newlycreated Connection objects are in auto-commit mode by default, which means thatindividual SQL statements are committed automatically when the statement iscompleted. To be able to group SQL statements intotransactions
and commit them or roll them back as a unit, auto-commit must bedisabled by calling the method setAutoCommit with false as its argument. Whenauto-commit is disabled, the user must call either the commit or rollbackmethod explicitly to end a transaction.翻译过来是这种:假设连接处于自己主动提交模式下。则其全部的SQL语句将作为单个事务执行并提交。否则,其SQL语句将作为事务组,直到调用Commit方法或rollback方法为止。
默认情况下,新连接处于自己主动提交模式。
简单来说,
以下的代码:
<span style="font-size:18px;">int[] result =null;
con.setAutoCommit(false);
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String[] SqlString= null;
for(String strvalue : SqlString){
stmt.execute(strvalue);
}
con.commit();
return result;</span>
能够看到。假设代码没有出错,弹幕我们就运行Commit没有问题,可是一旦出错。我们应该运行数据的rollback,可是该代码没有进行处理。这个时候。就会出现锁,将表锁住。这个锁就没有机会释放。
因此,我们应该这样写:
<span style="font-size:18px;"> boolean result = false;
try{
con.setAutoCommit(false);
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String[] SqlString= null;
for(String strvalue : SqlString){
result = stmt.execute(strvalue);
}
con.commit();
}catch(Throwable e){
if(con!=null){
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}finally{
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return result;</span>
因此,我们一定不要小看了这个问题,这个问题一旦出现,性能就会收到非常大的影响。
假设我们将SQL语句作为单个事务进行处理的话:
<span style="font-size:18px;"> /**
* 使用PreparedStatement加批量的方法
* @return
*/
public int[] executeUpdateMore(){
int[] result=null;
try{
PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(List sqlValueString : sqlValue){
for(int i=0;i<sqlValueString.size();i++){
try {
prest.setObject(i+1,sqlValueString.get(i));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
prest.addBatch();
}
prest.executeBatch();
/* con.commit();*/
this.closeAll(con, prest, null);
} catch (SQLException ex){
Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE, null,ex);
}
return result;
} </span>
假设我们将SQL语句作为事务组来处理的话。我们就要这样写:
<span style="font-size:18px;"> /**
* 使用PreparedStatement加批量的方法,strvalue:
* "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
* @return
* @throws SQLException
*/
public boolean executeUpdateMoreNotAuto() throws SQLException{ boolean result = false;
try{
con.setAutoCommit(false);
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String[] SqlString= null;
for(String strvalue : SqlString){
result = stmt.execute(strvalue);
}
con.commit();
}catch(Throwable e){
if(con!=null){
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}finally{
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return result;
}</span>
结束语:
一定要记住处理完之后要提交或者回滚奥!
MySql 中的setAutoCommit方法的更多相关文章
- mysql中使用存储过程方法中的注意事项
public function getFxOrderList($openId,$condition='',$curentPage=1,$pagesize =10){ return $this-> ...
- 关于SQL递归查询在不同数据库中的实现方法
比如表结构数据如下: Table:Tree ID Name ParentId 1 一级 0 2 二级 1 3 三级 2 4 四级 3 SQL SERVER 2005查询方法: //上查 with ...
- MYSQL中delete删除多表数据与删除关联数据
在mysql中删除数据方法有很多种,最常用的是使用delete来删除记录,下面我来介绍delete删除单条记 录与删除多表关联数据的一些简单实例. 1.delete from t1 where 条件 ...
- 更改mysql中当前auto_increment的值的方法
最近给自己网站更改mysql中当前auto_increment的值 如果在mysql中一个表test中的ID字段设为auto_increment插入两条记录后ID=2,这时删除1条记录,再插入一条变成 ...
- mysql中的load命令使用方法
使用mysql 中的load 命令,可以将txt 文件中的内容加载到数据库表中 使用mysql 中的load 命令,讲txt 文件中的内容加载到数据库表中,例如,创建table,名称是user,一个字 ...
- MySql中启用InnoDB数据引擎的方法
1.存储引擎是什么? Mysql中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术, ...
- 把mysql 中的字符gb2312 改为gbk的方法
第一步:查找mysql的字符: mysql> show variables like '%char%';+--------------------------+----------------- ...
- MySQL中多表删除方法(转载)
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...
- 在Eclipse中使用JDBC访问MySQL数据库的配置方法
在Eclipse中使用JDBC访问MySQL数据库的配置方法 分类: DATABASE 数据结构与算法2009-10-10 16:37 5313人阅读 评论(10) 收藏 举报 jdbcmysql数据 ...
随机推荐
- java之JMX
java之JMX 有关JMX的定义和架构就不具体解释了.见百度百科: http://baike.baidu.com/link? url=6QzGGEqphTmpft3ll5mXmDNVRdvLRZhk ...
- C - The C Answer (2nd Edition) - Exercise 1-4
/* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h&g ...
- UML图和C#
这段时间学习了楚光明老师解说的C#视频,接触这个学习材料的第一感觉就是老师解说的通俗易懂,非常easy让人去接受:再有就是在学习到UML图和C#的一节时非常有收获,之前自己也学习过UML图的一 ...
- amaze ui中的icon button
amaze ui中的icon button 说明几点: 1.链接效果 连接效果的本质一般都是a标签,好像很多button的链接效果都是用的a标签,submit表单提交或者button的type为sub ...
- Raid阵列之简单介绍
1.raid分类 软raid:用软件模拟raid芯片 硬raid:集成的后来添加的 2.raid基本简介 (1)raid是由廉价磁盘冗余阵列发展成为独立磁盘冗余阵列 (2)linux是借助MD(Mui ...
- Torch 的安装与基本用法
本文安装仅限 ubuntu 系统.官方文档见:Getting started with Torch. 0. 简介 Torch 使用轻量级脚本语言 Lua 及其 C/CUDA 扩展模块实现,底层数值计算 ...
- 值得学习的CSS知识
这里零度给大家推荐几个值得学习的CSS技巧,能让你编写网页事半功倍!一.清除默认值 通常 padding 的默认值为 0,background-color 的默认值是 transparent.但是在不 ...
- ssm框架的总结
ssm对应的是spring+springmvc+mybatis, 一.spring,略. 二.spring mvc是spring提供的mvc模块, 从图中可以看出,springmvc的模块划分非常多, ...
- ssh-agent && 及 ssh-add介绍
ssh-agent命令是一种控制用来保存公钥身份验证所使用的私钥的程序.ssh-agent在X会话或登录会话之初启动,所有其他窗口或程序则以客户端程序的身份启动并加入到ssh-agent程序中.通过使 ...
- PHP 7.1安装xhprof进行性能分析
安装扩展该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7) 下载并编译xhprof扩展在web的htm ...