简体   繁体   English

多线程 - MySQL java连接器编写语句使用

[英]Multithreading - MySQL java connector prepared statement use

I have made a java class which runs in it's own thread that works through a queue of mysql queries so that it doesn't block the main application thread. 我创建了一个java类,它运行在它自己的线程中,该线程通过mysql查询队列工作,这样它就不会阻塞主应用程序线程。 I want to use prepared statements, however if I keep reusing the same prepared statement then if there's two or more of that same prepared statement in the queue (I use the same preparedstatement object for each query of that type), it will have the wrong params. 我想使用预处理语句,但是,如果我继续重复使用相同的预处理语句,那么如果队列中有两个或多个相同的预处理语句(对于该类型的每个查询,我都使用相同的preparestatement对象),它将有错误的提示PARAMS。 If I make a new preparedstatement each time will it recompile the prepared statement each time it is run or will it detect that it's already been compiled and just execute? 如果我每次创建一个新的preparestatement它会在每次运行时重新编译准备好的语句,还是会检测到它已经被编译并执行了?

I think you really wont be able to utilize Prepared Statement Pooling feature available in Java connection pooling implementations like Apache DBCP. 我认为您真的无法利用Apache DBCP等Java连接池实现中提供的Prepared Statement Pooling功能。 This is because you are storing prepared statement objects in the queue. 这是因为您将准备好的语句对象存储在队列中。 If you could store your SQL and paramenters in a custom class instead, and create / execute PreparedStatements in execution thread you can get the benefit of pooling by using something like DBCP 如果您可以将SQL和参数存储在自定义类中,并在执行线程中创建/执行PreparedStatements,您可以通过使用DBCP之类的东西获得池化的好处

See DBCP configurations docs on how to enable statement pooling (poolPreparedStatements) 请参阅有关如何启用语句池的DBCP配置文档 (poolPreparedStatements)

will it recompile the prepared statement each time it is run or will it detect that it's already been compiled and just execute? 它将在每次运行时重新编译准备好的语句,还是会检测到它已经被编译并刚刚执行?

It's up to the DBMS, not Java, but the general idea is that it can detect a reuse of an existing compiled statement and pools them under the hood, either in the driver or at the server. 这取决于DBMS,而不是Java,但是总体思路是,它可以检测现有编译语句的重用,并将其合并到驱动程序或服务器中。 See the JDBC 4.0 Specification, #11.6, "Reuse of Statements by Pooled Connections". 请参见JDBC 4.0规范#11.6“池连接的语句重用”。

From the java tutorial 来自java教程

If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead. 如果要多次执行一个Statement对象,通常可以减少执行时间,而改用PreparedStatement对象。

The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created. PreparedStatement对象的主要特性是,与Statement对象不同,它在创建时会被赋予一个SQL语句。 The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. 这样做的好处是,在大多数情况下,此SQL语句会立即发送到DBMS,并在此进行编译。 As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled. 因此,PreparedStatement对象不仅包含SQL语句,还包含已预编译的SQL语句。 This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first. 这意味着当执行PreparedStatement时,DBMS可以只运行PreparedStatement SQL语句而无需先编译它。

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM