一个mysql jdbc待解之谜 关于jdbc  url参数 allowMultiQueries

如下的一个普通JDBC示例:

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);

执行时会报错:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select 'world'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

若一个sql中通过分号分割(或包含)了多个独立sql的话,如:

select 'hello';select 'world'

默认就会报上述错误,当若显式设置allowMultiQueries为true的话,就可以正常执行不会报错.如下所示:

String url = "jdbc:mysql://localhost:3306?allowMultiQueries=true";

官方文档解释:

allowMultiQueries
Allow 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

想要看看当该参数设置为true和false时,源码中到底哪里有不同.经过断点调试发现了在下面代码处会有不同的结果:

   //所属类和方法:void java.net.SocketOutputStream.socketWrite(byte[] b, int off, int len) throws IOException
   socketWrite0(fd, b, off, len);
   bytesWritten = len;

当设置为true时,执行完socketWrite0方法后查询query log,会看到这样的输出:

   23 Query	select 'hello';
   23 Query select 'world'

当设置为false时,执行完socketWrite0后.查询query log没有任何输出.

但奇怪的是它们的参数都一样,为什么一个有输出一个就没有呢?如下所示:

设置为true参数信息

设置为false时的参数信息:

补充: 即使当allowMultiQueries为false时,服务端也收到了查询请求,只不过没有在query log中输出而已.

如下抓包工具(Wireshark)所示:

又经过进一步调试,发现如下几处代码可能比较关键,如下所示:

/**
*
* 所属: void com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(String user, String 
* password, String database, Buffer challenge) throws SQLException
*/
// We allow the user to configure whether
// or not they want to support multiple queries
// (by default, this is disabled).
if (this.connection.getAllowMultiQueries()) { //1701行
this.clientParam |= CLIENT_MULTI_STATEMENTS;
} last_sent = new Buffer(packLength);
last_sent.writeLong(this.clientParam); //1876行

执行完上述语句后,对应的query log为:

150507 21:23:16	   19 Connect	root@localhost on

似乎是在这一交互过程中通知了服务器端客户端允许一次查询多条语句.通过抓包工具(wireshark)可以看到在创建连接过程中更多的交互信息,如下所示:

但在调试过程中又遇到了一个新问题,若在上述代码处加上了断点,就会出现如下异常:

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3161)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3615)
... 43 more

尚未找到该问题原因(如在何处设置的超时时间).但是实验发现,在创建连接过程中不能有debug调试(即不能有暂停),否则就会有fin包(不知这是Mysql的协议还是TCP的协议?).如下所示:

关于fin包的描述见wiki

但在成功建立连接后进行断点调试没有问题.

补充: 通过telnet来模拟上述现象.

# telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
J
5.6.17 <4/-7j1S- � 18fwG:-nh=66mysql_native_passwordConnection closed by foreign host.

对应的抓包信息:

因未能及时输入密码,导致服务端发出FIN包中断了连接.

或者也可以通过如下Java代码来模拟此现象:

Socket socket = new  Socket("127.0.0.1",3306);
System.in.read();

此时看到的抓包信息如下所示:

补充:

1. 如何查看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

2. 关于使用抓包工具(Wireshark)

参考文档:

http://www.maketecheasier.com/using-wireshark-ubuntu/

http://floss.zoomquiet.io/data/20120511105248/index.html

注意:

本地调试的话Interface(网卡)选择any(或lo), 过滤条件如下所示:

其他问题:

1. 为什么在mysql交互终端中执行的命令不能被Wireshark捕捉到?

需要显式指定-h参数 如

 mysql -u root -p -h 127.0.0.1

注意若为-h localhost的话,仍不能被Wireshark捕捉到,不知两者有什么区别?通过StackOverFlow中提问得到解答.

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

摘自: https://dev.mysql.com/doc/refman/5.0/en/connecting.html

2. 在mysql交互终端中是否默认设置了allowMultiQueries=true, 能有办法将其改为false吗?

确实默认设置了allowMultiQueries=true, 如下所示:

上图是通过Wireshark抓取终端mysql登录时(mysql -u root -p -h 127.0.0.1)抓取的包.

似乎没有修改的办法.

3. 如何在telent 127.0.0.1 3306后输入密码? 或者如何设置使得不用输入密码,试过使用--skip-grant-tables 启动mysql服务,但不起作用.

Jdbc Url 设置allowMultiQueries为true和false时底层处理机制研究的更多相关文章

  1. JDBC的URL设置allowMultiQueries的原因

    如下的一个普通JDBC示例: String user ="root";String password = "root";String url = "j ...

  2. easyui-combobox的option选项为true与false时的问题

    如题,我们使用easyui-combobox,当我们的选择项为true与false时,即选择是否,后台返回一个boolean类型的变量,那么这时候,通过form表单进行反显会出现这样的问题:表单里ea ...

  3. mysql的jdbc.url携带allowMultiQueries=true参数的作用及其原理

    如下配置 jdbc.url=jdbc:mysql://127.0.0.1:3306/chubb_2?autoReconnect=true&useUnicode=true&charact ...

  4. motto - question - bodyParser.urlencoded 中设置 extended 为 true 和 false 有什么区别吗?

    本文搜索关键字:motto node nodejs js javascript body-parser bodyparser urlencoded x-www-form-urlencoded exte ...

  5. C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?

    原文:C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思? 在 .NET 中创建进程时,可以传入 ProcessStartInfo ...

  6. Mysql JDBC Url参数说明useUnicode=true&characterEncoding=UTF-8

    MySQL的 JDBC URL 格式 for  Connector/J 如下例: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [ ...

  7. [android] setOnTouchEvent 设置返回值为true 和 false的区别

    今天在做自定义的可选文本的 TextView 类时,用到了 View 类的 setOnTouchListener(OnTouchListener l)事件监听,在构造 OnTouchListener ...

  8. MySQL JDBC URL参数(转)

    MySQL的 JDBC URL格式: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1][=prope ...

  9. com.mysql.cj.jdbc.Driver 新特性jdbc.url连接供参考

    com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6及以上版本中的参数详解: jdbc.url=jdbc:mysql://localhost:3306/t ...

随机推荐

  1. weat!!团队

    摘要: 团队名称:weat!! 团队成员:刘波 崔和杰 简介: 刘波:性别男,爱好:动漫,徒步旅行.在组内负责程序编写这一部分. 优点:认真负责,不懂就会去问. 崔和杰:性别男,爱好:篮球.在组内负责 ...

  2. (转第二方案)在 ASP.NET 環境下使用 Memcached 快速上手指南

    转自:http://blog.miniasp.com/post/2010/01/27/Memcached-for-ASPNET-Quick-Start-Guide.aspx 之前一直想研究 Memca ...

  3. hibernate检索策略(抓取策略)

    检索策略 类级别检索 默认检索策略:默认延迟加载, 可以使用lazy属性来进行改变. session.get(clazz,object)默认立即加载 @Test //测试左外连接查询 public v ...

  4. c#运用反射获取属性和设置属性值

    /// <summary> /// 获取类中的属性值 /// </summary> /// <param name="FieldName">&l ...

  5. RxSwift学习笔记3:生命周期/订阅

    有了 Observable,我们还要使用 subscribe() 方法来订阅它,接收它发出的 Event. let observal = Observable.of("a",&qu ...

  6. 《javascript高级程序设计》 touch事件的一个小错误

    最近一段时候都在拜读尼古拉斯大神的<javascript高级程序设计>,真的是一本好书,通俗易懂,条理比<javascript权威指南>好理解一些,当然<javascri ...

  7. 64位进程调用32位dll的解决方法

    64位进程调用32位dll的解决方法   最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/ ...

  8. 添加vscode自定义代码块

    以vue为例 一.打开vscode>文件>首选项>用户代码片段>vue.json二.编写代码块 其中一行一句:$1是占位符,就是你可以输入的地方."http get& ...

  9. 【译】AI 让科技公司变得更强大吗

    机器学习可能是当今技术中最重要的基本趋势.由于机器学习的基础是数据 - 大量的数据 - 很常见的是,人们越来越担心已经拥有大量数据的公司会变得更强大.这有一定的道理,但是以相当狭窄的方式,同时ML也看 ...

  10. scanf的拓展用法——匹配特定字符

    scanf的基本用法除了常规的输入操作外还有一些特殊的用法,使用这些用法可以很方便的在输入中读取想要的数据 1.限制输入数据的长度 这个应该算不上拓展用法,大多数读者应该都曾经使用过,这里简单提一下 ...