Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

jdbc - No server-side prepared statements using MySQL Connector/J

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Connector/J driver handles prepared statements locally unless you turn on real server side statements using the connection parameter useServerPrepStmts=true.

http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...