Is the executeUpdate method in Java thread safe?

There are several threads in my application trying to insert into a MySQL database . Is the executeUpdate thread safe method to use ? How can I do this job?

+7
source share
3 answers

Although the executeUpdate method itself may be thread safe, prepared statements are not intended to be used simultaneously. This is due to the fact that each instance saves your parameters until executeUpdate prompts it to send the parameters to MySQL. Moreover, since transactions are managed through Connection objects, sharing connections at the same time without synchronization can lead to unwanted commit / rollback behavior.

To make inserts from multiple threads at the same time, each thread must use its own Connection and make its own PreparedStatement . Using multiple prepared statements simultaneously in the same database is thread safe because concurrency is managed by the RDBMS.

+6
source

Nothing is said in Javadoc that either Connection, a PreparedStatement, or ResultSet is thread safe, and therefore, none of their methods are.

+2
source

executeUpdate() not thread safe for multiple threads.

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

0
source

All Articles