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

x
?
Solved

Usage of Connection object in java

Posted on 2012-04-10
2
Medium Priority
?
454 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
[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 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 1000 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
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 …
Suggested Courses

636 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