Solved

In a servlet where should you create a JDBC connection?

Posted on 2002-07-30
3
172 Views
Last Modified: 2013-11-24
I've got a couple of textbooks about JDBC and servlets. They differ in where they create a connection.
One holds the connection as an instance variable; creates the connection in the init method; closes it in the doGet method and also closes it in the destroy method and in a finally clause.
The other author creates and closes the connection inside the try clause, no instance variable.
What are the advantages and disadvantages of each method?

Colin
0
Comment
Question by:cjmackenzie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
twh270 earned 50 total points
ID: 7198376
A database connection is "expensive" to set up and tear down.  Most people use a connection pool to cache connections.  There are a lot of them out there, everything from free stuff to high-end expensive kits.  The servlet acquires a connection from the pool via something like "connectionPool.acquireConnection()" and releases it through e.g. "connectionPool.releaseConnection()".

To directly answer your question: Setting up the connection in init() means you create a single connection, only once, for the lifetime of each servlet instance.  This means you don't have the overhead of creating a database connection each time the servlet services a request, but it means only one request at a time can use the connection, so you'll need to synchronize access to it.  Creating a connection in try/catch means every request creates a new connection.  This is expensive.  However, it allows you to service N requests simultaneously.  (N is usually limited by the number of simultaneous connections your database allows.)

Both of these problems are mitigated through a connection pool (though not necessarily eliminated).

Keep in mind: textbook example code shows you how to write code that works.  It isn't intended to demonstrate good design.

-Thomas
0
 

Author Comment

by:cjmackenzie
ID: 7200606
Thanks Thomas, I was finding it hard to get my head round pooled connections and threads and your answer has helped clarify my options.
Within my application I have a static class that takes a connection and a query as parameters, then puts the results into a List of Javabeans for use in a JSP.
So I think I could very quickly improve the performance by creating the connection in the init method, then make the static method synchronized.
I am up against a deadline and I have a small number of potential users users, anyway (10 at the most). I don't think I've got time to look at pooled connections although that is obviously better still.


Colin
0
 
LVL 1

Expert Comment

by:twh270
ID: 7200978
Glad to help.

One thing I forgot to mention is that if the connection is unused for a long time it may timeout -- get closed by the database.  You can call Connection.isClosed() before using the connection.

-Thomas
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question