A MySQL JDBC solution puzzle about jdbc URL parameter allowmultiqueries
The following is a common JDBC example:
String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306"; Connection conn = java.sql.DriverManager.getConnection (URL, user, password); Statement stmt = Conn.createstatement (); String sql = "select ' Hello '; select ' World '; stmt.execute (SQL);
Error when executing:
Com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:You has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' select ' "World" at line 1 At Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (nativeconstructoraccessorimpl.java:57)
If a single SQL is separated (or contains) multiple independent SQL by semicolons, such as:
Select ' Hello '; select ' World '
The default is to report the above error, when explicitly set Allowmultiqueries is true, it can be executed without error. As shown below:
String url = "Jdbc:mysql://localhost:3306?allowmultiqueries=true";
Official documentation explains:
Allowmultiqueriesallow the use of '; ' to delimit multiple queries during one statement (True/false), defaults to ' false ', And does not affect the Addbatch () and ExecuteBatch () methods, which instead rely on RewriteBatchStatements.Default:false Since version:3.1.1
Want to see where in the source code is different when this parameter is set to True and false. After debugging the breakpoint, it is found that there are different results in the following code:
Owning class and method: void Java.net.SocketOutputStream.socketWrite (byte[] b, int off, int len) throws IOException SocketWrite0 (FD, B, Off, Len); Byteswritten = Len;
When set to true, querying query log after executing the SOCKETWRITE0 method will see the output:
Queryselect ' Hello '; Queryselect ' World '
When set to False, after the SOCKETWRITE0 has been executed. Queries query log does not have any output.
But the strange thing is that they all have the same parameters, why does one have an output without it? As shown below:
Set to true parameter information
Parameter information when set to False:
Supplement how to view query log:
mysql> show variables like ' general_log% '; +------------------+------------------------- -------------------------------------------+| variable_name | value |+------------------+----------------------------------------------- ---------------------+| general_log | off | | general_log_file | /opt/mysql/server-5.6/data/zhuguowei-presario-cq43-notebook-pc.log |+- -----------------+--------------------------------------------------------------------+mysql> set global general_log = ' on ' ; #开启另一终端tail -f /opt/mysql/server-5.6/data/ Zhuguowei-presario-cq43-notebook-pc.log
JDBC URL setting Allowmultiqueries to True and false when the underlying processing mechanism is studied