Raj Inevitable
Raj Inevitable

Reputation: 23

How to reuse database connection in JSP/Servlet?

Currently I'm creating the DB connection again and again in every JSP/Servlet. I would like to reuse my DB connection in my JSP/Servlet project. How can I achieve this?

Upvotes: 1

Views: 1216

Answers (1)

Matt Fellows
Matt Fellows

Reputation: 6532

The "correct" way to do this is to make use of connection pooling. That way the overhead of creating / closing connections is reduced.

Apart from that there's is nothing wrong with the way you are doing things. Maintaining open connections for too long is generally not a good idea.

To make this less hassle - you can create a utility class which returns a connection from the pool and likewise has a method for returning the connection to the pool.

Upvotes: 6

Related Questions