Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init(ServletConfig) not being called

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the init method always get called in a Servlet or only when there is a doPost method. I have a doGet method and my init method is not getting called. Is there a way to force it to be called every time?

If I move the code in init() to the doGet method, it works great. I am getting a NullPointerException at the stmt = connection.createStatement() line.
Thanks.
[ November 29, 2002: Message edited by: Gregg Bolinger ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API guarantees that init will get called as the first thing that happens after the servlet instance is constructed. Your problem must lie elsewhere.
I like to always have a System.out.println early in the init method.
Bill
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did read that init() gets called when the servlet is instantiated by the webserver (TOMCAT). So is it possible that my Connection object is being GC'd or closed somewhere else?
Should I be handleing a database connection some other way? This was the example given on this web page.
Thanks.
[ November 29, 2002: Message edited by: Gregg Bolinger ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be handling the database connection via a connection pool or creating as needed on a per request basis.
One (of many) reasons not to try to create the connection in init is that the database may decide to close the connection at any time. Remember, a servlet may stay running for weeks or more - everything in a servlet should be designed to reflect this. Connection pools will automatically handle re-creating a connection if the db closes one.
Bill
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill. Makes me wonder why Sun decided to show an example using the Connection in the init() method. They also state that the init method is used for initializing components for the Servlet and they even state Connections for Databases.

Thanks for the response though, and I think I will follow your advise.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a surprisingly sloppy example alright. However, note that you should use the init method to get the proper driver loaded as in the first part of your example. That way you discover right away if the driver exists.
If using a connection pool, you would also initialize it in the init method.
Bill
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good Bill. Thanks a ton!
I am still curious though as to why I am getting the nullPointerException. There is probably about a 30 second delay between when I reload the servlet and then run my test program. It is hard for me to believe that the connection is being dropped that fast. If anyone has any insight on this, that would be great.
Thanks.
 
Kees van Oosterhout
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a servlet should not maintain any state. Who knows how many instances of your servlet are in the container. Try to store your connection in the servlet context. I think this will work fine. You can recall the connection in the doGet() method. Just to be shure, you can display the connection just after creating it.....
Hope it helps....
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kees. However, that still doesn not explain why the example from Sun does not work. What is happening to my connection object in the init method that causes it to be null 30 seconds or less after it is created?
Is the example just wrong? I can buy that. But I would like to know why.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic