我应该在 Struts 2 应用程序中的哪里设置数据库连接?

发布于 2024-08-21 23:24:22 字数 150 浏览 5 评论 0原文

我正在使用 struts 2 开发一个小型 Web 应用程序。我想在 Web 应用程序启动时设置数据库连接。之后,我希望在整个 Web 应用程序中使用该数据库连接对象来更新数据库服务器中的记录。

我应该在 struts2 Web 应用程序中的哪里包含数据库连接设置代码?

I am developing a small web application using struts 2. I want to setup a database connection when the web application starts. After that, I want that database connection object to be used in the the entire web application to update records in the database server.

Where should I include the database connection setup code in my struts2 web application ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2024-08-28 23:24:22

您不应该在 web 应用程序启动时设置数据库连接。当释放时间过长且比 Web 应用程序的平均生存时间(天、月、年)真正短(约 30 分钟)时,DB 将使连接超时。而是在此时设置数据源/DAO 工厂。连接应在尽可能短的范围内获取和关闭。正常的 JDBC 习惯用法如下:

public void create(Entity entity) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try { 
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_CREATE);
        statement.setSomeObject(1, entity.getSomeProperty());
        // ...
        statement.executeUpdate();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
}

注意 finally 中的结束语。它可以防止发生异常时泄漏资源(即不关闭它们)。

为了提高连接性能,您通常需要使用连接池。任何像样的应用程序服务器都以 JNDI 数据源的方式提供此功能。例如,在 Tomcat 中,您可以在 JNDI 资源中找到 HOW- TO 如何创建一个。顺便说一句,这并不意味着您不需要关闭它们,您仍然需要根据通常的 JDBC 习惯用法来关闭它们。连接池即返回一个包装的连接,它在 close() 中执行类似以下操作:

public void close() throws SQLException {
    if (this.connection is still eligible for reuse) {
        do not close this.connection, but just return it to pool for reuse;
    } else {
        actually invoke this.connection.close();
    }
}

对于在 web 应用程序启动时设置数据源(或 DAO 工厂),有多种方法。一种是使用 ServletContextListener contextInitialized() 方法中执行任务,另一种方法是使用 Struts2 管理的应用程序作用域 bean 在构造函数中执行任务。

另请参阅基本 JDBC/DAO 教程了解更多信息提示如何以正确的方式开始使用 JDBC(在 Web 应用程序中)。

You shouldn't setup a database connection on webapp startup. The DB will timeout the connection when it is been released for a too long time and that is truly shorter (~30 mins) than the time an average webapplication lives (days, months, years). Rather setup the datasource / DAO factory at that point. Connections are to be acquired and closed in the shortest possible scope. The normal JDBC idiom is the following:

public void create(Entity entity) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try { 
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_CREATE);
        statement.setSomeObject(1, entity.getSomeProperty());
        // ...
        statement.executeUpdate();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
}

Note the closing in finally. It prevents leaking resources (i.e. not closing them) when an exception occurs.

To improve connecting performance you normally want to use a connection pool. Any decent appserver provides this facility in flavor of a JNDI datasource. In for example Tomcat, you can find in the JNDI Resources HOW-TO how to create one. This by the way does not mean that you don't need to close them, you still need to close them according the usual JDBC idiom. The connection pool namely returns a wrapped connection which does something like the following in the close():

public void close() throws SQLException {
    if (this.connection is still eligible for reuse) {
        do not close this.connection, but just return it to pool for reuse;
    } else {
        actually invoke this.connection.close();
    }
}

As to setting up the datasource (or DAO factory) on webapp startup, there are several ways. One is using a ServletContextListener which does the task in the contextInitialized() method, another way is using a Struts2-managed application scoped bean which does the task in the constructor.

Also see this basic JDBC/DAO tutorial for more hints how to get started with JDBC (in webapplications) the proper way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文