Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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
有没有办法在Java App Engine中重用MySQL Db连接_Java_Sql_Google App Engine_Connection - Fatal编程技术网

有没有办法在Java App Engine中重用MySQL Db连接

有没有办法在Java App Engine中重用MySQL Db连接,java,sql,google-app-engine,connection,Java,Sql,Google App Engine,Connection,我想重用java.sql.Connection对象。这是谷歌应用引擎,所以连接池不是一个选项(我想?)。通常我只是在每次servlet调用时创建一个新连接,但现在我有从servlet调用的函数,我不想在每次函数调用时创建一个新连接(除非我知道它会非常快),例如 如何在someSQLFunction()中重用servlet的Connection对象?在App Engine中,前端被认为是为web请求服务的,设计的一部分是它能够在实例之间移动、根据需要删除或启动。这意味着您的连接对象在从Servle

我想重用
java.sql.Connection
对象。这是谷歌应用引擎,所以连接池不是一个选项(我想?)。通常我只是在每次servlet调用时创建一个新连接,但现在我有从servlet调用的函数,我不想在每次函数调用时创建一个新连接(除非我知道它会非常快),例如


如何在
someSQLFunction()
中重用servlet的
Connection
对象?

在App Engine中,前端被认为是为web请求服务的,设计的一部分是它能够在实例之间移动、根据需要删除或启动。这意味着您的连接对象在从Servlet中的另一个方法使用时可能无效

作为替代方案,您可以使用不需要设置连接的数据存储或Google Cloud SQL

另一方面,如果您非常需要它来使用您自己的MySQL,您可以在模块[1]中保持连接的活动性,该模块可能比前端请求更有效。例如,您可以将任务添加到pull队列中,模块将使用其中的nexts任务,同时保持连接

在这种情况下,响应速度不如使用数据存储


[1] 应用程序引擎模块-

感谢您的回复。我只需要在单个请求期间重复使用连接,尽管如我的代码片段所示。为什么不将其分配给可以从任何方法访问的本地类变量?
    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;
        }
    }