简体   繁体   English

Servlet的JDBC连接池

[英]JDBC Connection Pooling for Servlets

Currently I'm using a separate DBConnectionManager class to handle my connection pooling, but I also realized that this was the wrong way to go as the servlet was not calling the same pool each time a doGet() is performed. 当前,我正在使用一个单独的DBConnectionManager类来处理我的连接池,但是我也意识到这是错误的方法,因为每次执行doGet()时Servlet都不调用相同的池。

  1. Can someone explain to me why the above is happening? 有人可以向我解释为什么会发生上述情况吗?
  2. Is JNDI the way to go for java servlets with tomcat for proper connection pooling? JNDI是使用带有适当连接池的tomcat的Java servlet的方法吗?

I have links to 2 articles, is this the correct way to implement connection pooling with servlets? 我有2篇文章的链接,这是用servlet实现连接池的正确方法吗?

http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html

http://onjava.com/onjava/2006/04/19/database-connection-pooling-with-tomcat.html http://onjava.com/onjava/2006/04/19/database-connection-pooling-with-tomcat.html

Is it possible to save the db manager object in the context like so: 是否可以在上下文中保存数据库管理器对象,如下所示:

mtdb = (MTDbManager) context.getAttribute("MTDBMANAGER");
if (mtdb == null) {
            System.out
                    .println("MTDbManager is null, reinitialize MTDbManager");              

            initMTDB(config);
            context.setAttribute("MTDBMANAGER", mtdb);
        }

And then I call mtdb.getInstance().getConnection() and it will always reference this object. 然后,我调用mtdb.getInstance()。getConnection(),它将始终引用该对象。

Thanks. 谢谢。

Generally, the best advice is to leave the connection pooling to the application server. 通常,最好的建议是将连接池留给应用程序服务器。 Just look up the data source using JNDI, and let the application server handle the rest. 只需使用JNDI查找数据源,然后让应用程序服务器处理其余的数据即可。 That makes your application portable (different application servers have different pooling mechanisms and settings) and most likely to be most efficient. 这使您的应用程序具有可移植性(不同的应用程序服务器具有不同的缓冲机制和设置),并且最有可能是最有效的。

查看并使用C3P0,而不是滚动自己的解决方案: http : //sourceforge.net/projects/c3p0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM