Sql 是否可以在不同的连接上执行CallableStament?
通常认为重用Sql 是否可以在不同的连接上执行CallableStament?,sql,jdbc,prepared-statement,connection-pooling,callable-statement,Sql,Jdbc,Prepared Statement,Connection Pooling,Callable Statement,通常认为重用CallableStatement的实例(检查)是一种很好的做法 但在创建CallableStatement时,该语句(据我所知)是绑定的 到特定的连接。因此,我们通常会: Connection con = pool.getConnection(); CallableStatement st = con.prepareCall("{ some stmt; }"); st.executeQuery(); st.close(); con.close(); 根据我
CallableStatement
的实例(检查)是一种很好的做法
但在创建CallableStatement
时,该语句(据我所知)是绑定的
到特定的连接
。因此,我们通常会:
Connection con = pool.getConnection();
CallableStatement st = con.prepareCall("{ some stmt; }");
st.executeQuery();
st.close();
con.close();
根据我的检查,以下内容不执行查询:
Connection con = pool.getConnection();
CallableStatement st = con.prepareCall("{ some stmt; }");
con.close();
con = pool.getConnection(); // possibly another new connection, different than the one used to create the CallableStatement instance
st.executeQuery();
st.close();
我的问题是:如果我想重用所有的
CallableStatement
实例,但另一方面仍然能够获得新的连接并关闭旧的连接(并非总是打开相同的连接)我能做什么?PreparedStatement
由JDBC驱动程序缓存(或应该缓存)。见例
这意味着您不应该保留一个并在连接之间使用,但别担心,您的驱动程序将为您管理缓存。基本上,每个连接都将缓存自己的连接,因此如果您有5个连接,您将有5个缓存副本,这可能足够小了
调用prepareStatement
将在缓存时从缓存中检索,否则将分配。因此重复调用prepareStatement
是轻量级的。这是API的正确用法
例如,请参阅技术上特定于Oracle的信息,但我相信这些信息是标准的。因此,这意味着在执行查询之前,每次都可以执行
prepareCall
?我没有完全理解你的答案。它们是缓存的,所以我可以多次调用相同的prepareCall
?@foobar IIRC,是的,这是正确的。每次调用prepareCall
,JDBC将检查它是否已经在缓存中。谢谢,这回答了我的问题(将在5分钟内接受您的回答)。如果你能在真实答案中附上你的评论答案,那就太好了。谢谢;)