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
Solved

In a servlet where should you create a JDBC connection?

Posted on 2002-07-30
3
170 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 58
null output 3 35
Which non-HTML GUI front end to use with Java? 3 24
Java: anonymous class 4 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

829 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