我有以下 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 语句的执行顺序:首先删除,然后插入。