Updated on 2025-02-27 GMT+08:00

Transaction

  • [Specification] Large object operations do not support transactions.

    Large object operations include creating and deleting DATABASE, ANALYZE, and VACUUM jobs.

  • [Rule] When accessing the database through the JDBC client, disable the autocommit parameter and explicitly execute the transaction COMMIT.
    • On the one hand, enabling the autocommit parameter will cause some parameters (such as fetchsize) to become invalid.
    • On the other hand, the service should clarify the service logic and reduce the dependence on the database.
  • [Rule] Do not combine multiple SQL statements into one statement when accessing the database through JDBC.

    When multiple statements are combined into one statement that contains object operations, if the intermediate object operation fails, a new transaction is started to execute subsequent statements.

    Example:
    • The following statement does not meet specifications:
      Connection conn = ....
      try {
          Statement stmt = null;
          try {
              stmt = conn.createStatement();
              stmt.executeUpdate("CREATE TABLE t1 (a int); DROP TABLE t1");
          } finally {
              stmt.close();
          }
          conn.commit();
      } catch(Exception e) {
         conn.rollback();
      } finally {
         conn.close();
      }
      In the preceding statement, if
      CREATE TABLE t1;

      The creation fails, and a new transaction will be started.

      DROP TABLE t1;

      The execution fails.

    • You are advised to split the statement into two statements and send them separately:
      Connection conn = ....
      try {
          Statement stmt = null;
          try {
              stmt = conn.createStatement();
              stmt.executeUpdate("CREATE TABLE t1 (a int)");
              stmt.executeUpdate("DROP TABLE t1");
          } finally {
              stmt.close();
          }
          conn.commit();
      } catch(Exception e) {
         conn.rollback();
      } finally {
         conn.close();
      }