?
Solved

In a servlet where should you create a JDBC connection?

Posted on 2002-07-30
3
Medium Priority
?
173 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
A solution for Fortify Path Manipulation.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 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