Solved

Usage of Connection object in java

Posted on 2012-04-10
2
443 Views
Last Modified: 2012-04-20
Hi Experts...
I have a requirement that involves me to call aroung 30 -40 separate functions housing sql queries and update and/or insert.
In this case how can i get the connection object.
Am i suppose to create at one place and pass the connection object to all the 30 functions  or do i create the connection object in the function itself.
There are many updates happening in these queries in separate functions and time is of the essence.
So how do i create the connection object and use the same connection object over and over again without creating a new one.
Please help...
0
Comment
Question by:gaugeta
2 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 37830603
You should get the connection object once and then pass it to the different functions to do the work.

Ideally rather than getting the connection object directly you would use a connection pooler (e.g. http://commons.apache.org/dbcp/).  That saves a lot of time because rather than establishing a new connection to the database each time (which is a slow operation) the pool keeps a set of connections open and when you "close" your connection it just puts it back in the pool without actually closing it.

One more step that can make your code easier to maintain is to switch how you create connections and execute queries.  So rather than:

1) Create connection
2) Execute query1(connection)
3) Execute query2(connection)
4) Execute query3(connection)
5) Close connection

You can use a query runner class (see http://commons.apache.org/dbutils/examples.html) so your logic is switched around:
1) Create query runner
2) Execute query1(queryrunner)
3) Execute query2(queryrunner)
4) Execute query3(queryrunner)
5) NOTE: You never created a connection so you don't need to close the connection

The only difference is that you no longer are responsible for closing the connection.  That might sound like a small point now but when you do a lot of this it becomes a huge challenge because failing to close connections correctly causes databases to eventually stop working - and are very hard bugs to find.

But writing your code like that requires a bit more sophistication - but it's all explained on that link above.

Anyway, hope that helps,

Doug
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
ID: 37831161
its cumbersome to pass the connection to each method, I would recommend using ThreadLocal,

check this example, here it sets and gets a list, instead you can set and get a conn!

http://javaboutique.internet.com/tutorials/localdata/
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
diffSum example 4 50
ejb entity bean example issue 2 16
learn programming 8 34
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 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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

840 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