加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

java – 如果我关闭PreparedStatement,数据库中的缓存是否会丢

发布时间:2020-12-11 23:50:16 所属栏目:MySql教程 来源:网络整理
导读:PreparedStatment ps = null;public void executeQueries(){ try{ ps = conn.prepareStatement(Query1); // Execute Query1 here and do the processing. ps = conn.prepareStatement(Query2); // Execute Query2 here and do the processing. //... more qu

PreparedStatment ps = null;
public void executeQueries(){
    try{
        ps = conn.prepareStatement(Query1);
        // Execute Query1 here and do the processing.           

        ps = conn.prepareStatement(Query2);
        // Execute Query2 here and do the processing.

        //... more queries
    }catch(){}
    finally{
        ps.close(); // At this point would the caching of queries in DB be lost?
    }
}

在我的应用程序中,我经常调用方法executeQueries().

我的问题是,如果我在方法(我经常使用)中的finally块中关闭PreparedStatement,数据库系统是否会删除缓存?如果是,我可以为整个应用程序创建一个全局PreparedStatement,因为我的应用程序中有大量JAVA CLASSES查询数据库.

谢谢!

更新:问题已标记为重复,但链接的线程根本不回答我的问题. AFAIK,数据库系统将执行的查询存储在高速缓冲存储器中.它还存储了他们的执行计划.这是PreparedStatement比Statement更好的地方.但是,一旦PreparedStatement关闭,我不确定是否删除了与查询相关的信息.

最佳答案 特别关于MySQL,据说

8.10.3?Caching of Prepared Statements and Stored Programs

The server maintains caches for prepared statements and stored programs on a per-session basis. Statements cached for one session are not accessible to other sessions. When a session ends,the server discards any statements cached for it.

因此,关闭PreparedStatement不会从缓存中删除语句,但可能会关闭Connection.

…除非应用程序使用连接池,否则关闭Connection可能不一定会结束数据库会话;它可以保持会话打开,只返回到池的连接.

然后还有一个问题,即语句是否实际上是在服务器上进行PREPAREd.这是由useServerPrepStmts连接字符串属性控制的.默认情况下,IIRC不启用服务器端预处理语句.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读