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,082,665
  • 372
  • 3,610
  • 3,555

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,512
  • 4
  • 35
  • 57