I wanted to use an Object Pool of Apache for getting Connections with each in开发者_开发技巧vocation 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.
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 in destroy()
(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.
精彩评论