【发布时间】:2014-09-01 11:50:07
【问题描述】:
我想重用 java.sql.Connection 对象。它与 Google App Engine 一起使用,因此连接池不是一种选择(我认为?)。通常我只是为每个 servlet 调用创建一个新连接,但现在我有从 servlet 调用的函数,我不想在每个函数调用上创建一个新连接(除非我知道它会非常快),例如
public void someServlet() { // Called from web page - speed is thus of concern
try (Connection con = DriverManager.getConnection("connection string")) { // might be slow - want to re-use con
// Do something with connection, e.g. run some queries
someSQLFunction();
someSQLFunction();
someSQLFunction();
} catch (SQLException e) {
// Some error handling
}
}
public void someSQLFunction() throws SQLException {
// I don't want to re-create another Connection here, but use the one from the servlet.
try (Connection con = DriverManager.getConnection("connection string")) { // might be slow - want to re-use con
// Do something with connection, e.g. run some queries
return;
}
}
如何在someSQLFunction() 中重用servlet 的Connection 对象?
【问题讨论】:
标签: java sql google-app-engine connection