public void upsateStatus() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null; Connection msconn1 = null;
Statement msstmt1 = null;
ResultSet msrs1 = null; Statement stmt2 = null; Statement stmt3 = null; String querysql;
try {
conn = JdbcUtil.getConnection();
stmt = conn.createStatement();
String sql = "select * from ps_dp_fax_change where status ='n'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
try {
msconn1 = JdbcUtilMsSql.getConnection();
msstmt1 = msconn1.createStatement();
querysql = "select * from EZFaxTarget where exid ='"
+ rs.getString("dp_fax_out") + "'";
msrs1 = msstmt1.executeQuery(querysql);
if (msrs1.next()) {
if (msrs1.getShort("FSCPStatus") == 11) { //fax发送成功 //更改db2  中记录的的状态
try {
stmt3 = conn.createStatement();
String updatedbSql = "update  ps_dp_fax_out set dp_succeed ='y' "
// + "where dp_fax_id  = '" + rs.getString("dp_fax_id")+"'";
+ "where dp_fax_id  = "
+ rs.getInt("dp_fax_id");
int n3 = stmt3.executeUpdate(updatedbSql);
log.info(n3 + "n3----" + updatedbSql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt3.close();
} catch (SQLException e) {
e.printStackTrace();
} } try {
//更改关联表 ps_dp_fax_change   status = 'y'
stmt2 = conn.createStatement();
String updateSql = "update ps_dp_fax_change set status = 'y' "
+ "where dp_fax_id = "
+ rs.getInt("dp_fax_id")
+ "  "
+ " and dp_fax_out='"
+ rs.getString("dp_fax_out") + "'";
int n2 = stmt2.executeUpdate(updateSql);
log.info(n2 + "n2---" + updateSql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt2.close();
//conn2.close();
} catch (SQLException e) {
e.printStackTrace();
} } }//endif fax状态改变 }//end while(rs1.next()) } catch (SQLException e) {
e.printStackTrace();
} finally {
try {
msrs1.close();
msstmt1.close();
msconn1.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }//while(rs.next() } catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }

解决方案 »

  1.   

    我记得,Statment 里面有commit(),rollback()的方法.然后你的Connection好像有个设置把事务的机制打开,不过这需要你数据库本身具有事务能力.
      

  2.   

    //更改db2  中记录的的状态
    try {
    conn.setAutoCommit(true);
             stmt3 = conn.createStatement();
             String updatedbSql = "update  ps_dp_fax_out set dp_succeed ='y' "
    // + "where dp_fax_id  = '" + rs.getString("dp_fax_id")+"'";
    + "where dp_fax_id  = "
    + rs.getInt("dp_fax_id");
            n3 = stmt3.executeUpdate(updatedbSql);
            log.info(n3 + "n3----" + updatedbSql);
    //更改关联表 ps_dp_fax_change   status = 'y'
    stmt2 = conn.createStatement();
    String updateSql = "update ps_dp_fax_change set status = 'y' "
    + "where dp_fax_id = "
    + rs.getInt("dp_fax_id")
    + "  "
    + " and dp_fax_out='"
    + rs.getString("dp_fax_out") + "'";
     n2 = stmt2.executeUpdate(updateSql);
    log.info(n2 + "n2---" + updateSql);

    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    if(n3==1&n2==1){
    conn.commit();
    }else{
    conn.rollback();
    }
    try {
    stmt3.close();
    stmt2.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }}
      

  3.   

    /**
        * Excute sql
        */
    public boolean doStatement(String[] sqlStrs)
    {
    boolean flag = false;
    if (sqlStrs.length < 1)
    return false;
    try
    {
    this.recordCount = sqlStrs.length;
    InitialContext initial = new InitialContext();
    ds = (DataSource) initial.lookup(dbjndi);
    con = ds.getConnection();
    con.setAutoCommit(false);   //加事务
    stmt = con.createStatement(); //System.out.println("sqlStrs.length:" + sqlStrs.length); int count = sqlStrs.length / 7000 + 1;
    System.out.println("count:" + count);
    int end = 0;
    for (int ii = 0; ii < count; ii++)
    {
    stmt.clearBatch();
    end = (ii + 1) * 7000;
    if (end > sqlStrs.length)
    end = sqlStrs.length;
    for (int i = ii * 7000; i < end; i++)
    {
    //modify by lcl 
    if (sqlStrs[i] != null && sqlStrs[i].trim().length() > 0)
    stmt.addBatch(sqlStrs[i]);
    System.out.println("in DbOperate.doStatement sql : " + sqlStrs[i]);
    this.progress = i;
    }
    stmt.executeBatch();
    //System.out.println("excute back:" + ii + "Strins begin:" + ii * 7000 + " end :" + end);
    } con.commit();   //提交事务
    System.out.println("excute sql finished!");
    flag = true;
    }
    catch (NamingException ee)
    {
    //System.out.println("DbOperate.java : find data source error, " + ee.toString());
    stmtStr = "find data source error";
    flag = false;
    }
    catch (Exception ex)
    {
    stmtStr = StringTool.removeNewline(ex.toString());
    ex.printStackTrace();
    if (ex.toString().indexOf("ORA") > 0)
    stmtStr = StringTool.removeNewline(ex.toString().substring(ex.toString().indexOf("ORA") + 10));
    //System.out.println("err String :" + stmtStr);
    flag = false; try
    {
    con.rollback();  //回滚
    }
    catch (Exception ee)
    {
    ee.printStackTrace();
    stmtStr = StringTool.removeNewline(ee.toString());
    }
    flag = false;
    }
    finally
    { try
    {
    this.progress = 0;
    this.recordCount = 0;
    con.setAutoCommit(true);
    }
    catch (SQLException e)
    {
    // TODO ???? catch ?
    e.printStackTrace();
    }
    if (stmt != null)
    {
    try
    {
    stmt.close();
    }
    catch (SQLException e1)
    {
    // TODO ???? catch ?
    e1.printStackTrace();
    }
    }
    if (con != null)
    {
    try
    {
    con.close();
    }
    catch (SQLException e1)
    {
    // TODO ???? catch ?
    e1.printStackTrace();
    }
    } }
    return flag;
    }