• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in ServletContextListener, how it can be used for JDBC connection management?  RSS feed

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you for reading my post
I saw this sample code in a book and I am wondering how it can be used in order to make put a connection into the Servlet itself?
Can you please explain how we should use this connection inside the servlet?



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewwww, bad idea!

This code grabs a connection when the web application starts up, and releases it when the web application is shut down. Bad, bad idea. Hugely bad!

Rather, you should use connection pooling -- preferably container-managed -- to obtain a connection when you need it, and to return it to the pool as quickly as possible.

I wonder other specious practices this book promotes?
[ January 30, 2008: Message edited by: Bear Bibeault ]
 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reply.
This code come from "Java/J2EE Job Interview Companion" second edition which published in 2007.

Thing that I can not figure out is, how we can access the connection object which is created in listener in the Servlet.


Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't advise mentioning that technique in a real job interview.
It really is a bad idea.
If asked, say that you would use container managed connection pooling.

That being said.
You can access any object bound to context scope from a servlet by calling getServletContext().getAttribute("attributeName");

You'll need to cast it down to Connection as the return type for getAttribute is java.lang.Object.

If you want to look into some better books, check our our BunkHouse.
There are reviews of lots of them, including servlet and JSP books.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, this is a bad idea! Don't do this!

But to answer your question, the listener places the connection in the application context (servletContent) as a scoped variable named con using the setAttribute() method.

In a Servlet you can obtain the scoped variable from the servlet context in the same manner that the contextDestroyed() method does.

But I cannot emphasize this enough: don't do this with connections!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear + Ben = klunk!

And yes, I agree with Ben. Espousing this as a good idea in a job interview would pretty much guarantee a short one.

The idea of binding variables into application scope in a context listener for use throughout the application is fine -- I do it all the time. It's binding a DB connection as such that is a colossal bone-headed move.
[ January 30, 2008: Message edited by: Bear Bibeault ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!