Java servlet和数据库连接池 [英] Java servlets and database connection pooling
问题描述
仅查看Web上的连接池示例,它们都基于每个servlet实现连接池。因此每个servlet都有自己的数据库连接池。我的问题是,为什么这类似于全局数据库连接池?由于全局池似乎比每个servlet池更有效..
Just looking at examples of connection pooling on the web, they all implement connection pooling on a per servlet basis. So each servlet has its own pool of database connections. My question is, why is that preferable to something like a global pool of db connections? Since a global pool is seems more efficient than a per servlet pool..
此外,因为我正在考虑实现这样的池。有没有办法在servlet之前初始化一个类(我正在使用jetty btw)?我刚刚开始进行servlet开发,但这似乎对配置等其他东西很有用。否则我只会使用某种类似模式的单例..
Also, since I'm thinking about implementing such a pool. Is there a way to have a class initialized before the servlets(I'm using jetty btw)? I'm just starting servlet development but that would seem useful for other things too like configuration. Otherwise I was just going to use some sort of singleton like pattern..
推荐答案
1)我会说标准做法是在上下文描述符中将连接池设置为JNDI资源,不是每个servlet的事情。
1) I'd say that standard practice is to set up a connection pool as a JNDI resource in the context descriptor, which would not be a per-servlet thing to do.
2 )您将要实现并声明 ServletContextListener 。
2) You'll want to implement and declare a ServletContextListener.
这篇关于Java servlet和数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!