Solved

In a servlet where should you create a JDBC connection?

Posted on 2002-07-30
3
166 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now