Solved

Usage of Connection object in java

Posted on 2012-04-10
2
445 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
String array comparison 4 46
QUerry require with a procedure. 4 49
junit as external jar or library 7 40
Github api to fetch the number of collaborators in a git repo 1 33
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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