I have multiple threads trying to update a MySQL database? is executeUpdate method thread-safe to use?
I have multiple threads trying to update a MySQL database? is executeUpdate method thread-safe
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it is not thread-safe to use.
In fact, if some other thread uses a statement, and then another thread calls executeUpdate(), then the other thread’s
ResultSets, if any, will be closed. JavaDoc for java.sql.Statement (of which PreparedStatement is a subtype) “All execution methods in the Statement interface implicitly close a statment’s current ResultSet object if an open one exists.”Furthermore, it’s unlikely that a given implementation of
executeUpdate()would be written to be mulit-thread safe.You should either syncrhonize all use of the statement and resulting result sets, or make multiple connections so that each thread uses its own JDBC
Connectionto the database.. I would recommend the latter.