[英]Is there a way to re-use the MySQL Db connection in Java App Engine
我想重新使用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()
中someSQLFunction()
servlet的Connection
对象?
在App Engine中,前端被认为是服务于Web请求,而设计的一部分是其能够在实例之间移动,根据需要删除或启动的能力。 这意味着当您的Servlet中的另一个方法使用您的连接对象时,该对象可能无效。
或者,您可以在不需要建立连接的地方使用数据存储区或Google Cloud SQL
另一方面,如果您非常需要使用自己的MySQL,则可以在模块[1]中保持连接有效,该模块可能会超出前端请求。 例如,您可以将任务添加到拉入队列中,模块将在保持连接的同时使用其中的下一个任务。
在这种情况下,响应速度不会像使用数据存储区那样快。
[1] App Engine模块-https: //developers.google.com/appengine/docs/java/modules/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.