PostgreSQL 用の JDBC ドライバーを使用しており、ストアド プロシージャを呼び出したいと考えています。
次のようにストアド プロシージャを呼び出すメソッドがあるとします。
public void callProcedure(int someValue) {
Connection con = null;
try {
con = connectionPool.getConnection();
CallableStatement st = con.prepareCall("{ call some_procedure(?) }");
st.setInt(1, someValue);
st.execute();
st.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
if (con != null) {
try { con.close(); } // assure connection "goes" back to the pool
catch (SQLException e) { }
}
}
}
callProcedure
ここで、このメソッドが何百万回も呼び出される可能性があると仮定しましょう。私の質問は次のとおりです。
(1.) 自分のクラスのコンストラクターとコンストラクターの両方で接続を作成した方が (パフォーマンス的に) 良いprepare_the_Call
ので、次のようにします。
CallableStatement st;
public Constructor() {
Connection con = connectionPool.getConnection();
st = con.prepareCall("{call some_procedure(?)}");
}
そして、メソッド内で次のことを行います。
public void callProcedure(int someValue) {
try {
st.setInt(1, someValue);
st.execute();
st.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
finally {
if (con != null) {
try { con.close(); } // assure connection "goes" back to the pool
catch (SQLException e) { }
}
}
}
close
(2.)ステートメントの実行後は常に接続に意味がありますか? それとも開けたほうがいいですか?私の理解では、それを閉じると接続が接続プールに戻り、他の誰かが使用できるようになります。あれは正しいですか?