servlet 中的 dbcp 对象池
我想使用 Apache 的对象池来获取每次调用 servlet 时的连接。没有其他 Servlet 使用此池。我在 init() 中创建了一个 ObjectPool。在我的 doPost() 中,我从池中获取连接,使用它并释放它。
该servlet 不是由用户调用,而是由其他一些机器定期调用,即没有用户特定的操作。
我是否需要将池设置为静态,还是应该使用 ServletContext、Listeners 等。
I wanted to use an Object Pool of Apache for getting Connections with each invocation of a servlet. No other servlets use this pool. I created an ObjectPool in init(). And in my doPost() I get connection from the pool, use it and release it.
This servlet is not invoked by users but periodically by a few other machines,ie, no user specific operations.
Do I need to make the pool as static or should I use ServletContext, Listeners,etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Servlet 始终是单例,因此,如果您没有在该 Servlet 之上使用某些更高级别的框架,则实例变量会在
init()
中初始化并在destroy()
中销毁(它关闭池(关闭所有底层连接)是一个很好的做法)完全没问题。此外,所有连接池都设计为在多线程环境中工作,因此您不需要任何额外的同步。
Servlets are always singletons, so if you are not using some higher level framework on top of that servlet, instance variable initialized in
init()
and destroyed indestroy()
(it is a good practice to close the pool closing all underlying connections) is perfectly fine.Also all connection pools are designed to work in multi-threaded environment hence you don't need any extra synchronization.