[英]using same statement object to execute multiple queries in multiple threads?
Is it a good idea to re-use java.sql.Statement object to execute multiple queries in multiple threads simultaneously ? 重用java.sql.Statement对象在多个线程中同时执行多个查询是一个好主意吗?
I've read somewhere that it's recommended to reuse same statement object, however it was sequential execution. 我在某处读过,建议重用同一条语句对象,但这是顺序执行。
Statement statement = con.createStatement();
Now if I use this single statement instance to run executeUpdate()
for multiple queries in multiple threads simultaneously ? 现在,如果我使用单个语句实例在多个线程中同时为多个查询运行executeUpdate()
?
Note that it's not PreparedStatement and I am not getting any ResultSet. 请注意,它不是PreparedStatement,并且没有任何ResultSet。
I think answer depends on two scenarios: 我认为答案取决于两种情况:
Is my understanding correct? 我的理解正确吗?
Thanks. 谢谢。
No, it's definitely not a good idea. 不,这绝对不是一个好主意。 For one, ResultSets
are associated with a Statement
. 首先, ResultSets
与Statement
关联。 You don't want to be iterating through a ResultSet
when another thread suddenly makes a new query (the ResultSet
would be closed, at least if the driver is a well behaved one). 您不希望在另一个线程突然发出新查询时遍历ResultSet
(至少在驱动程序运行良好的情况下, ResultSet
才会关闭)。
There is also nowhere an indication that Statements
would be thread-safe. 也没有地方表明Statements
是线程安全的。 If it's not clearly indicated, it's not thread-safe (at least in most cases). 如果没有明确指出,则它不是线程安全的(至少在大多数情况下)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.