1

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?

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513

1 Answers1

6

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.

Matt Fellows
  • 6,462
  • 4
  • 33
  • 55