mysql setautocommit_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方法为止。

默认情况下,新连接处于自己主动提交模式。

简单来说,

以下的代码:

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;

能够看到。假设代码没有出错,弹幕我们就运行Commit没有问题,可是一旦出错。我们应该运行数据的rollback,可是该代码没有进行处理。这个时候。就会出现锁,将表锁住。这个锁就没有机会释放。

因此,我们应该这样写:

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;

因此,我们一定不要小看了这个问题,这个问题一旦出现,性能就会收到非常大的影响。

假设我们将SQL语句作为单个事务进行处理的话:

/**

* 使用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

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;

} 假设我们将SQL语句作为事务组来处理的话。我们就要这样写:

/**

* 使用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;

}

结束语:

一定要记住处理完之后要提交或者回滚奥!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
####Statement和PreparedStatement - PreparedStatement预编译的SQL执行对象 1. 可以避免SQL注入 因为在编译的时候已经把SQL的逻辑固定,不会因为替换进去的内容改变逻辑 2. 如果SQL涉及变量 相比Statement的字符串拼接的方式,代码可读性提高,并且不容易出错。 3. 如果涉及批量执行多条SQL时 使用PreparedStatement执行效率较高 - 如果SQL没有变量用Statement 有变量用PreparedStatement ###批量操作 - Statement批量操作: statement.addBatch(sql1); statement.addBatch(sql2); statement.addBatch(sql3); //执行批量操作 statement.executeBatch(); - PreparedStatement批量操作: statement = connection.prepareStatement(sql); for (int i = 0; i < 100; i++) { statement.setString(1, "name"+i); statement.setString(2, "admin"+i); //添加到批量操作 statement.addBatch(); if(i ==0){ //执行批量操作 statement.executeBatch(); //清空执行过的SQL statement.clearBatch(); } } ####事务 1. 开启和关闭自动提交 connection.setAutoCommit(false/true); 2. 提交事务 connection.commit(); 3. 回滚 connection.rollback(); - 实现转账: 超人 500 蝙蝠侠 5000 蝙蝠侠给超人转2000 执行第一次成功 执行第二次成功 执行第三次失败 提示余额不足 并且回滚 create table jdbc_person(id int,name varchar(10),money int); insert into jdbc_person values(1,'超人',500),(2,'蝙蝠侠',5000); - 代码参见Demo08.java ###获取自增主键的值 create table team(id int primary key auto_increment,name varchar(10)); create table player(id int primary key auto_increment,name varchar(10),tid int); - 代码参见 Demo09.java ###元数据 - 元数据指 数据库和表的相关信息 - 代码参见 Demo10.java

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值