Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 是否可以在不同的连接上执行CallableStament?_Sql_Jdbc_Prepared Statement_Connection Pooling_Callable Statement - Fatal编程技术网

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分钟内接受您的回答)。如果你能在真实答案中附上你的评论答案,那就太好了。谢谢;)