From what I understand, MySQL 5.1 supports server-side prepared statements. Therefore the following code should prepare the statement once, and execute it 10 times:
Connection conn = ds.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) FROM users WHERE user_id=?");
for (int i=0; i<10; i++)
{
stmt.setString(1, "FOO"+i);
ResultSet res = stmt.executeQuery();
res.close();
}
stmt.close();
conn.close();
What I see instead in the mysqld log is the query being executed directly:
SELECT @@session.tx_isolation
SELECT USER()
SELECT COUNT(*) FROM users WHERE user_id='FOO0'
SELECT COUNT(*) FROM users WHERE user_id='FOO1'
SELECT COUNT(*) FROM users WHERE user_id='FOO2'
...
I see the query sent in the full each time in the protocol logs too (using tcpdump).
Using Connector/J 5.1.12 and MySQL 5.1.44. No funny JDBC options in the JDBC URL. Going straight to the driver for this test, no pool.
Why aren't the statements being prepared?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…