Hi
There's a question in one of the JWeb+ exams that goes
You are writing a servlet that needs to fire SQL queries to retrieve the data from a database and generate html pages for HTTP GET requests. Which is the most appropriate place to create database connection?
They reckon the answer is to create a db connection from within the init() the comment being
Creating database connections is time consuming. So, generally, applications maintain a pool of connections and take a connection from the pool whenever needed. In this case, a connection can be created in the init() method and can be reused for each request.
If you create a connection in service(), doGet() or right before firing the query, a new connection will be created for each request which is waste of time.
Does anyone want to argue with this? I reckon the best place to create the connection is right before you execute the SQL ie get a connection from the connection pool, create your statement, resultset etc etc then put the connection back in the pool.
Anyone agree. Holding onto database connections for the lifetime of a
servlet doesn't seem to be a great idea.