如何管理服务器上的数据库连接? [英] How to manage db connections on server?

查看:137
本文介绍了如何管理服务器上的数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序中的数据库连接存在严重问题.由于我从单例数据库类对整个应用程序使用单个数据库连接,因此,如果尝试并发数据库操作(两个用户),数据库将回滚事务. 这是我使用的静态方法:

I have a severe problem with my database connection in my web application. Since I use a single database connection for the whole application from singleton Database class, if i try concurrent db operations (two users) the database rollsback the transactions. This is my static method used:

所有线程/servlet都调用静态Database.doSomething(...)方法,该方法又调用以下方法.

All threads/servlets call static Database.doSomething(...) methods, which in turn call the the below method.

private static /* synchronized*/ Connection getConnection(final boolean autoCommit) throws SQLException {
    if (con == null) {
        con = new MyRegistrationBean().getConnection();
    }
    con.setAutoCommit(true); //TODO
    return con;
}

建议使用什么方法来管理此数据库连接,这样就不会出现相同的问题.

What's the recommended way to manage this db connection/s I have, so that I don't incurr in the same problem.

推荐答案

永远保持Connection打开是一个非常糟糕的主意.它没有无限的生存期,每当DB超时并关闭连接时,您的应用程序都可能崩溃.最佳做法是在最短范围内获取关闭ConnectionStatementResultSet,以避免资源泄漏以及由泄漏和超时.

Keeping a Connection open forever is a very bad idea. It doesn't have an endless lifetime, your application may crash whenever the DB times out the connection and closes it. Best practice is to acquire and close Connection, Statement and ResultSet in the shortest possible scope to avoid resource leaks and potential application crashes caused by the leaks and timeouts.

由于连接数据库是一项昂贵的任务,因此您应该考虑使用连接池来提高连接性能.体面的applicationserver/servlet容器通常已经提供了具有JNDI DataSource风格的连接池功能.有关如何创建的详细信息,请查阅其文档.例如在Tomcat的情况下,您可以在此处.

Since connecting the DB is an expensive task, you should consider using a connection pool to improve connecting performance. A decent applicationserver/servletcontainer usually already provides a connection pool feature in flavor of a JNDI DataSource. Consult its documentation for details how to create it. In case of for example Tomcat you can find it here.

即使使用连接池,您仍然必须编写适当的JDBC代码:获取在尽可能短的范围内关闭所有资源.反过来,连接池将担心实际上关闭连接还是将其释放回池以供进一步重用.

Even when using a connection pool, you still have to write proper JDBC code: acquire and close all the resources in the shortest possible scope. The connection pool will on its turn worry about actually closing the connection or just releasing it back to pool for further reuse.

您可能会从本文<如何正确使用JDBC基础知识.作为完全不同的替代方法,学习EJB和JPA.它将为您将所有JDBC样板抽象为oneliners.

You may get some more insights out of this article how to do the JDBC basics the proper way. As a completely different alternative, learn EJB and JPA. It will abstract away all the JDBC boilerplate for you into oneliners.

希望这会有所帮助.

  • Is it safe to use a static java.sql.Connection instance in a multithreaded system?
  • Am I Using JDBC Connection Pooling?
  • How should I connect to JDBC database / datasource in a servlet based application?
  • When is it necessary or convenient to use Spring or EJB3 or all of them together?

这篇关于如何管理服务器上的数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆