在多线程环境下,每个线程都在执行这个call()>>
PreparedStatement st1 = null
PreparedStatement st2 = null;
.....
.....
st1.executeBatch();
st2.executeBatch();
connection.commit();
是否保证对于每个线程,st1 中的 sql 批处理将在 st2 中的 sql 批处理之前执行?换句话说,在这种情况下同步行为是否得到保证?
谢谢
最佳答案
根据documentation ,连接对象可以启用(默认模式)或禁用自动提交模式。当自动提交为 false 时:
If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback. By default, new connections are in auto-commit mode.
因此,如果自动提交为 false,则第一批将在第二批之前提交。如果自动提交为 true,则第一批和第二批将作为一个事务提交。请注意,仅当自动提交为 false 时才应使用 Connection.close()
。
在这两种情况下,执行顺序(无论提交模式如何)都遵循代码中的顺序。
关于java - jdbc中的语句是否同步执行批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350859/