环境为
mysql 5.1.39
mysql-connector-j 5.1.11
测试代码
在mysql驱动url中如果设置useServerPrepStmts=false,那么上面两条语句执行时,向服务器发送的数据包一样(用wireshark抓包工具加断点看到)。
如果设置useServerPrepStmts=true,那么执行conn.prepareStatement("select * from test where name=?");语句时,会向服务器发送命令,告知这条语句,执行ps.execute();时则只发送参数和语句编号。此时在服务器端
mysql> show global status like 'Com_stmt%' ;
可以看到 Com_stmt_prepare数量增加。
mysql 5.1.39
mysql-connector-j 5.1.11
测试代码
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = conn
.prepareStatement("select * from test where name=?");
ps.setInt(1, 1);
ps.execute();
Statement stmt = conn.createStatement();
stmt.execute("select * from test where name=1");
conn.commit();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_test?useServerPrepStmts=false",
"db_test", "db_test");
conn.setAutoCommit(false);
return conn;
}
在mysql驱动url中如果设置useServerPrepStmts=false,那么上面两条语句执行时,向服务器发送的数据包一样(用wireshark抓包工具加断点看到)。
如果设置useServerPrepStmts=true,那么执行conn.prepareStatement("select * from test where name=?");语句时,会向服务器发送命令,告知这条语句,执行ps.execute();时则只发送参数和语句编号。此时在服务器端
mysql> show global status like 'Com_stmt%' ;
可以看到 Com_stmt_prepare数量增加。