• Post Reply Bookmark Topic Watch Topic
  • New Topic

JDBC Connection  RSS feed

 
Sandeep Awasthi
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Where should we get JDBC connnection, init() method or service method?

To my knowledge it should be in init method colse in destroy.

But if some exception or rather error because of which init method doesnt get successfully executed and load-on-startup for servlet is 1, will server crash?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what you are going to do with that connection.

High performance / high load applications don't establish their connections in a servlet at all. They use connection pools which allow your servlet to access an already running connection when it needs it.

If you create your connection in the init method, all requests will have to share the same connection. You will have to insure that there are no concurrency issues if several requests try to access that connection at the same time.

If you establish your connection in one of the service methods, each hit will have to wait while the (usually expensive) connection is made.


You can find a quick explanation of connection pools here.
 
Sandeep Awasthi
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,
thanks alot for reply.

I understood what you said.

I just wanted to know if some error occurs during the init method, or unhandled exception, and load-on-startup is 1 for that servlet, will application server crash?

Kind Regards

Rajesh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!