JDBC事务,sql语句的执行顺序 [英] JDBC transaction, execution order of sql statements
本文介绍了JDBC事务,sql语句的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下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语句的执行顺序:先删除,然后在插入-.
解决方案
它们是从上到下执行的,因此按照您执行的顺序,删除将首先执行,插入之后.
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.
解决方案
They are executed from top to bottom, so in the order you did it, the deletes will execute first and the insert after them.
这篇关于JDBC事务,sql语句的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文