Solved

Usage of Connection object in java

Posted on 2012-04-10
2
448 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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 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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

689 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