JDBC事务,SQL语句的执行顺序(JDBC transaction, execution orde

2019-10-18 15:41发布

我有以下JDBC代码:

Connection conn = connectUserDataSource();
        // Setting auto commit to false to execute all queries as part of transaction
        conn.setAutoCommit(false);
        PreparedStatement deletePreparedStatement = null;
        PreparedStatement insertPreparedStatement = null;
        try
        {
            deletePreparedStatement = conn.prepareStatement(sqlDelete);
            deletePreparedStatement.setInt(1, someId);
            deletePreparedStatement.executeUpdate();

            insertPreparedStatement = conn.prepareStatement(sqlInsert);
            for(SomeObject obj : objects)
            {
                insertPreparedStatement.setInt(1, obj.getId());
            }

            insertPreparedStatement.executeBatch();
            // committing transaction
            conn.commit();
            transactionComplete = true;
        }

我想2条准备的语句是一个JDBC事务的一部分。 我想知道,是否在其创建将SQL语句的执行顺序顺序:先删除和插入 - 后。

Answer 1:

他们从上到下执行的,所以在你做这件事的顺序,删除将执行第一和照顾他们的插入。



文章来源: JDBC transaction, execution order of sql statements