java - jdbc中的语句是否同步执行批处理

标签 java multithreading jdbc

在多线程环境下,每个线程都在执行这个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/

相关文章:

postgresql - 在简单的读写上激发 OOM

java - SQLException 从 JTable 和数据库中删除行

java - 如何返回数组列表迭代器? ( java )

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

java - 尝试使用 ProcessBuilder 执行 Java 应用程序时出现类未找到异常

android - 在单独的线程中使用 while 循环生成 android 按钮

Java for 线程一起启动的循环(几乎同时)

java - IOException - read end dead - 在此示例中导致它的原因以及如何修复它 - Java 中的多线程应用程序

java - Ubuntu 中的 Tomcat 和 IntelliJ

java - 如何从 session 访问 JavaBean 数组