博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java事务处理类(源码)
阅读量:7126 次
发布时间:2019-06-28

本文共 2710 字,大约阅读时间需要 9 分钟。

Mysql5很好的支持了事物处理功能。不过支持这个功能的只有两种表类型。

 

分别是BDB,InnoDB。

 

先建立一个表,名为Kiss,数据为id (int),name(varchar),pop(varchar)。

 

下面是源码:

 

import
 java.sql.
*
;
ExpandedBlockStart.gif
public
 
class
 TestCommit
{
ExpandedSubBlockStart.gif    
public static void main(String args[]){
    Connection conn
=null;
ExpandedSubBlockStart.gif    
try{
       Class.forName(
"com.mysql.jdbc.Driver");
       String url
="jdbc:mysql://localhost:3306/kiss";
       conn
=DriverManager.getConnection(url,"username","password");
       
boolean autoCommit=conn.getAutoCommit();
       
//关闭自动提交功能
       conn.setAutoCommit(false);
       Statement stmt
=conn.createStatement();
       stmt.executeUpdate(
"insert into sun values(15,'Hello','Beijing')");
       stmt.executeUpdate(
"insert into sun values(16,'Hi','shanghai')");
       ResultSet rs
=stmt.executeQuery("select * from sun");
ExpandedSubBlockStart.gif       
while(rs.next()){
            System.out.print(
"DeptNo:"+rs.getInt(1));
            System.out.print(
"\tDeptName:"+rs.getString(2));
            System.out.println(
"\tLOC:"+rs.getString(3));
}
   
//提交事务
    conn.commit();
//恢复原来的提交模式
    conn.setAutoCommit(autoCommit);
    stmt.close();
ExpandedSubBlockStart.gif}
catch(Exception e){
    System.out.println(
"操作失败!!!任务撤销!!!");
ExpandedSubBlockStart.gif    
try{
        
//回滚、取消前述操作
        conn.rollback();
ExpandedSubBlockStart.gif    }
catch(Exception e1){
      e1.printStackTrace();
    }
ExpandedSubBlockStart.gif}
finally{
ExpandedSubBlockStart.gif   
try{
ExpandedSubBlockStart.gif         
if(conn!=null){
            conn.close();
         }
ExpandedSubBlockStart.gif   }
catch(Exception e1){
     e1.printStackTrace();
    }
   }
    }
}

 

 

执行第一次执行这个类,id如果不冲突,就可以顺利插入数据,第二次插入,id冲突,则实现回滚。

 

下面是部分回滚:

 

 

 

import
 java.sql.
*
;
ExpandedBlockStart.gif
public
 
class
 TestSavepoint
{
ExpandedSubBlockStart.gif    
public static void main(String args[]){
    Connection conn
=null;
ExpandedSubBlockStart.gif    
try{
       Class.forName(
"com.mysql.jdbc.Driver");
       String url
="jdbc:mysql://localhost:3306/kiss";
       conn
=DriverManager.getConnection(url,"username","password");
       
boolean autoCommit=conn.getAutoCommit();
       
//关闭自动提交功能
       conn.setAutoCommit(false);
       Statement stmt
=conn.createStatement();
       stmt.executeUpdate(
"insert into sun values(21,'Hello','Beijing')");
       stmt.executeUpdate(
"insert into sun values(22,'Hi','shanghai')");
       Savepoint sp1
=conn.setSavepoint("p1");
       stmt.executeUpdate(
"insert into sun values(25,'shiyang','xingtai')");
       Savepoint sp2
=conn.setSavepoint("p2");
       stmt.executeUpdate(
"insert into sun values(60,'shiyang','baoding')");
       ResultSet rs
=stmt.executeQuery("select avg(id) from sun");
       rs.next();
       
double avg_id=rs.getDouble(1);
ExpandedSubBlockStart.gif       
if(avg_id>100){
         conn.rollback(sp1);
ExpandedSubBlockStart.gif       }
else if(avg_id>30){
         conn.rollback(sp2);
       }
       conn.commit();
       rs
=stmt.executeQuery("select * from sun");
ExpandedSubBlockStart.gif       
while(rs.next()){
            System.out.print(rs.getInt(
1));
            System.out.print(
"\t"+rs.getString(2).trim());
            System.out.println(
"\t"+rs.getString(3));
}
   
rs.close();
stmt.close();
ExpandedSubBlockStart.gif    }
catch(Exception e){
      System.out.println(
"Failure.rollback!!!");
ExpandedSubBlockStart.gif   
try{
       conn.rollback();
ExpandedSubBlockStart.gif   }
catch(Exception e1){
     e1.printStackTrace();
    }
    e.printStackTrace();
ExpandedSubBlockStart.gif   }
finally{
ExpandedSubBlockStart.gif     
try{
ExpandedSubBlockStart.gif       
if(conn!=null){
         conn.close();
       }
ExpandedSubBlockStart.gif     }
catch(Exception e1){
       e1.printStackTrace();
     }
   }
    }
}

 

代码测试通过,完美运行。

本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/08/16/1269065.html,如需转载请自行联系原作者

你可能感兴趣的文章
Zabbix 监控问题总结 (3.0)
查看>>
Elasticsearch 集群优化篇
查看>>
Chris Dixon谈体验经济的崛起,及其对创业公司的重要性
查看>>
ecshop模板商品详情页显示供货商
查看>>
搜索引擎优化的10条军规矩
查看>>
YII之yiic创建YII应用
查看>>
MySQL引擎
查看>>
入门级Ibatis案例
查看>>
LeetCode:Rectangle Area - 矩形交叉部分的面积
查看>>
研究开源OpenWnn Android输入法源代码
查看>>
下拉列表
查看>>
Visual Studio 11 Beta 带来新的Metro 应用开发体验
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Android 打开高德地图和百度地图
查看>>
如何查找Oracle数据库service_name、及可访问数据库的用户名密码
查看>>
phpredis:php一个key-value扩展 简介
查看>>
Appium TestNg Maven Android Eclipse java简单启动实例
查看>>
ListView Adapter常见问题
查看>>
中继链路
查看>>