Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

In a servlet where should you create a JDBC connection?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 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