JDBC transaction, execution order of sql statements

依然范特西╮ 提交于 2019-12-11 14:01:59

问题


I have following JDBC code:

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;
        }

And I would like 2 prepared statements to be part of one JDBC transaction. I am wondering whether order at which they are created will be the execution order of SQL statements: delete first and inserts - after.


回答1:


They are executed from top to bottom, so in the order you did it, the deletes will execute first and the insert after them.



来源:https://stackoverflow.com/questions/18859905/jdbc-transaction-execution-order-of-sql-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!