Java中的executeUpdate方法是一个线程安全的吗? [重复]

时间:2022-04-23 21:00:59

This question already has an answer here:

这个问题在这里已有答案:

In my application,there are multiple threads trying to insert to a MySQL database.Is executeUpdate method thread-safe to use?How can I make this work?

在我的应用程序中,有多个线程试图插入到MySQL数据库中。是否使用线程安全的executeUpdate方法?如何使其工作?

3 个解决方案

#1


6  

While executeUpdate method in itself may be thread safe, prepared statements are not designed to be used concurrently. This is because each instance stores your parameters until executeUpdate instructs it to send the parameters to MySQL. Moreover, since transactions are managed through Connection objects, sharing connections concurrently without synchronization may give you undesired behavior on commits/rollbacks.

虽然executeUpdate方法本身可能是线程安全的,但是预准备语句不能同时使用。这是因为每个实例都存储您的参数,直到executeUpdate指示它将参数发送到MySQL。此外,由于事务是通过Connection对象管理的,因此在没有同步的情况下并发共享连接可能会在提交/回滚时给您带来不希望的行为。

In order to make inserts from multiple threads work concurrently, each thread needs to use its own Connection, and make its own PreparedStatement. Using multiple prepared statements concurrently on the same database is thread safe, because the concurrency is managed on the RDBMS side.

为了使来自多个线程的插入同时工作,每个线程需要使用自己的Connection,并创建自己的PreparedStatement。在同一个数据库上同时使用多个预准备语句是线程安全的,因为并发是在RDBMS端管理的。

#2


2  

There is nothing in the Javadoc that says either a Connection, a PreparedStatement, or a ResultSet is threadsafe, and therefore none of their methods are either.

Javadoc中没有任何内容表示Connection,PreparedStatement或ResultSet是线程安全的,因此它们的方法都不是。

#3


0  

executeUpdate() is not thread-safe to use for multiple threads.

executeUpdate()对于多个线程不是线程安全的。

you can make multiple connections so that each thread uses its own JDBC Connection to the database.

您可以建立多个连接,以便每个线程使用自己的JDBC连接到数据库。

#1


6  

While executeUpdate method in itself may be thread safe, prepared statements are not designed to be used concurrently. This is because each instance stores your parameters until executeUpdate instructs it to send the parameters to MySQL. Moreover, since transactions are managed through Connection objects, sharing connections concurrently without synchronization may give you undesired behavior on commits/rollbacks.

虽然executeUpdate方法本身可能是线程安全的,但是预准备语句不能同时使用。这是因为每个实例都存储您的参数,直到executeUpdate指示它将参数发送到MySQL。此外,由于事务是通过Connection对象管理的,因此在没有同步的情况下并发共享连接可能会在提交/回滚时给您带来不希望的行为。

In order to make inserts from multiple threads work concurrently, each thread needs to use its own Connection, and make its own PreparedStatement. Using multiple prepared statements concurrently on the same database is thread safe, because the concurrency is managed on the RDBMS side.

为了使来自多个线程的插入同时工作,每个线程需要使用自己的Connection,并创建自己的PreparedStatement。在同一个数据库上同时使用多个预准备语句是线程安全的,因为并发是在RDBMS端管理的。

#2


2  

There is nothing in the Javadoc that says either a Connection, a PreparedStatement, or a ResultSet is threadsafe, and therefore none of their methods are either.

Javadoc中没有任何内容表示Connection,PreparedStatement或ResultSet是线程安全的,因此它们的方法都不是。

#3


0  

executeUpdate() is not thread-safe to use for multiple threads.

executeUpdate()对于多个线程不是线程安全的。

you can make multiple connections so that each thread uses its own JDBC Connection to the database.

您可以建立多个连接,以便每个线程使用自己的JDBC连接到数据库。

相关文章