重用CallableStatement
的实例通常被认为是一种好的做法(检查here)。是否可以在不同的连接上执行CallableStament?
但是,当创建一个CallableStatement
时,该陈述(根据我的理解)绑定 到特定的Connection
。因此,我们通常做的:
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
实例,但另一方面仍然能够获得新的连接并关闭旧的连接(不总是有相同的连接打开)我该怎么办?
所以,这意味着在执行查询之前每次都执行'prepareCall'就可以了吗?我没有完全理解你的答案。它们被缓存,所以,我可以多次调用相同的'prepareCall'? – insumity
@foobar IIRC,是的,这是正确的。每次调用prepareCall,JDBC都会检查它是否已经存在于缓存中。 – djechlin
谢谢,这回答我的问题(将在5分钟内接受你的答案)。如果你可以在你的真实答案中附上你的评论答案,会很好。谢谢 ;) – insumity