用JDBC 批处理插入数据出错后不能回滚,数据库是mysql 插入的数据是60000条数据。mysql里还是保存了部分的数据。请知道原因的朋友麻烦解决哈谢谢
try{
fr = new FileReader(file);
        br = new BufferedReader(fr,6*1024);   
      con  = this.getSession().connection();    
      con.setAutoCommit(false);
        ps = con.prepareStatement("insert into test  value(?,?,?,?,?,?,?,?)");
//         System.out.println(con.getAutoCommit());
        while ((line=br.readLine())!=null) {
         fields = line.split(",");
         ps.setString(1, fields[0]);
         ps.setInt(2, fields[1]);
         ps.setInt(3, 0);
         ps.setInt(4, 0);
         ps.setInt(5, 0);
         ps.setInt(6, 0);
         ps.setInt(7, 0);
         ps.setInt(8, 0);
         ps.addBatch();          i++;
         if(i%5000==0){
         ps.executeBatch();          }
         } 
        ps.executeBatch();
        con.commit();
}catch(Exception e){
try {
con.rollback();


} catch (Exception e1) { 
}}

解决方案 »

  1.   

    conn 对象是从hb的seesion里面获取的
      

  2.   

    是啊 郁闷 以前在oracle下就没这个问题
      

  3.   

    楼主这是冬眠吧,哪里是什么jdbc??
      

  4.   

    楼主看一下javadoc的对于Statement接口的executeBatch方法的解释吧我摘两句
    If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch.关键就在这个“may or may not ”
    也就是说jdbc驱动可以继续也可以不继续做接下来的命令。可能oracle的驱动是"may"
    而楼主现在的数据库的jdbc驱动是“may not”楼主参考参考吧btw:javadoc说“throws a BatchUpdateException”,但是根据楼主的描述,也没抛出这个exception到楼主程序的catch里面,
    难道说被executeBatch方法给“吃掉了”??明白人来给说道说道吧good luck
      

  5.   

    极有可能是你的MySQL处理引擎不支持事务,建表的时候加上engine=InnoDB试一下。
    既然换成Oracle没有问题,肯定就是数据库设置的问题了。
      

  6.   

    MySQL建表的时候不支持事务吧
    我看到标题第一时间想到的就是这个
      

  7.   

    估计是数据库的隔离级别和innoDb的问题把楼主给你个解决方法:在开始的时候采用: 检查点机制,在开始的时候创建一个检查点,在出现异常的时候采用roolBack(Point)回滚到开始的地方,首先保证你的engine是innoDb的,否则可能不支持某些事物
      

  8.   


    try{ 
    fr = new FileReader(file); 
            br = new BufferedReader(fr,6*1024);  
        con  = this.getSession().connection();    
        con.setAutoCommit(false);
        Transaction trans = db.createTransaction(conn);
        trans.begin(); 
        
        //构选 insert语句...
        ...
               i++; 
            if(i%5000==0){ 
              trans.executeSQL();
            } 
            } 
            trans.executeSQL();        trans.commit();
        }
        catch(Exception e){ 
          try { 
            trans.rollback(); 
          } 
          catch (Exception e1) { 
          } 
        }
        finally{
    trans.close();
            con.close;
        }
      

  9.   

    不会的啊,我就用的mysql,java.sql.Connection接口本身就有提交,回滚这几个动作,使用时要开启事务(即设置自动提交为false),然后尽管使用Statement或PreparedStatement执行各类sql语句,只要不出异常即可选择提交,否则则执行回滚。