【问题标题】:I am trying to execute jdbc transaction.But the database is not updated once I run the code我正在尝试执行 jdbc 事务。但是一旦我运行代码,数据库就不会更新
【发布时间】:2016-02-16 13:26:57
【问题描述】:

我在 Eclipse 中使用了以下代码。运行代码后数据库不会更新。它仍然显示相同的值。我什至在数据库中运行了运行良好的 sql 查询,但是一旦我运行我的 java 代码什么都没有更新..

    import java.io.*;
    import java.sql.*;
    import java.util.Properties;
    public class ExpTran {

     public static void main(String[] args) {
    Connection con = null;
    try {
        FileInputStream io = new FileInputStream("config/db.properties");
        Properties p = new Properties();
        p.load(io);
        Class.forName(p.getProperty("driver"));
        con = DriverManager.getConnection(p.getProperty("url"),p);
        Statement stmt = con.createStatement();
        con.setAutoCommit(false);
        int i = stmt.executeUpdate("update tutorials.account set amount=amount-500 where acc_no='A002'");
        i += stmt.executeUpdate("update tutorials.account set amount=amount+500 where acc_no='A003'");
        if(i!=2)
        {
            System.out.println("Exception");
            throw new SQLException();
        }
        System.out.println(i);  
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        if(con!=null){
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        e.printStackTrace();
    }
    finally {
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

}

【问题讨论】:

  • 你在哪里提交交易?

标签: java mysql jdbc transactions


【解决方案1】:

尝试替换这个:

   con.setAutoCommit(false);

这里有这个:

  System.out.println(i);
  con.commit();

或者设置为true:

  con.setAutoCommit(true);

【讨论】:

    【解决方案2】:

    随便

    conn.commit();
    

    调用 commit() 会将所有更改写入数据库,这是必需的,因为自动提交已关闭。如果发生异常,则需要捕获它并发出命令

    conn.rollback();
    

    将数据库恢复到其原始状态。

    否则,请执行

    conn.setAutoCommit(true);
    

    然后每个单独的语句将自动转换到数据库。问题是每条语句都将在自己的事务中执行。如果在执行第一条语句后发生异常,则不会执行第二条语句,这可能会使数据库处于不一致状态。

    【讨论】:

      【解决方案3】:

      如果您定义con.setAutoCommit(false);,则必须调用con.commit(); 显式提交您的更改

      同时避免对多个操作使用单个 try/catch/finally 块。

      public static void main(String[] args) {
          Properties properties = new Properties();
      
          try (FileInputStream propertiesInputStream = new FileInputStream("config/db.properties")) {
              properties.load(propertiesInputStream);
          } catch (IOException e) {
              throw new RuntimeException("Error loading properties file.", e);
          }
      
          String driverClassName = properties.getProperty("driver");
          try {
              Class.forName(driverClassName);
          } catch (ClassNotFoundException e) {
              throw new RuntimeException("Error loading JDBC driver class.", e);
          }
      
          String databaseUrl = properties.getProperty("url");
      
          try (Connection connection = DriverManager.getConnection(databaseUrl, properties)) {
              connection.setAutoCommit(false);
      
              Statement stmt = connection.createStatement();
              executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount-500 where acc_no='A002'");
              executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount+500 where acc_no='A003'");
      
              connection.commit();
          } catch (SQLException e) {
              throw new RuntimeException(e);
          }
      }
      
      private static void executeUpdateAndCheck(Statement stmt, String command) throws SQLException {
          int result = stmt.executeUpdate(command);
      
          if (result == 0) {
              stmt.getConnection().rollback();
              throw new IllegalStateException();
          }
      }
      

      【讨论】:

        【解决方案4】:

        插入或更新未反映在您设置的数据库中:con.setAutoCommit(false);

        将此行替换为:

        con.commit();

        con.setAutoCommit(true);

        【讨论】:

        • A con.commit() 应该放在 更新语句之后,不要设置自动提交(默认为 true)。跨度>
        【解决方案5】:

        您定义了con.setAutoCommit(false);,因此您需要在完成操作后提交连接。

        为此使用Connection.commit()

        if(i!=2) {
           System.out.println("Exception");
           throw new SQLException();
        } else {
           con.commit();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多