Solved

Usage of Connection object in java

Posted on 2012-04-10
2
437 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 26

Accepted Solution

by:
dpearson earned 250 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

13 Experts available now in Live!

Get 1:1 Help Now