首页 > 编程知识 正文

java方法线程安全,线程安全三种方法

时间:2023-05-05 10:40:06 阅读:211523 作者:2229

I have multiple threads trying to update a MySQL database? is executeUpdate method thread-safe to use?

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 Connection to the database.. I would recommend the latter.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。