Get your CodeRanch badge!*
The moose likes JDBC and the fly likes when to use different connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "when to use different connections" Watch "when to use different connections" New topic
Author

when to use different connections

vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
I am writing java code that accesses (selects and inserts ) data into different tables.
For each table should I open a different connection?
All the transactions are either simple selects (with joins) or inserts, where data from different table are picked up and then inserted.
I think new connections are not required, but currently in the code I have, which was already written by someone else, it is done that way.
For different sets of tables, different connections are open. I dont understand the logic behind that, can someone tell me what is the correct way to work with jdbc connections?

Thank you!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

It really depends on the structure of the application. Is this a single-threaded console program? A multi-threaded daemon or web app? A Swing app?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
This is a single threaded java application that talks to the database
Is it like, if there are multiple threads that access the tables, then we will create a new connection for each table thats accessed.
My doubt is, how will it help even if i have different connections, I dont understand the difference.
My question may sound a little silly, but then when am started to design this one, I am getting all kinds of doubts!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

In a single threaded application that is going to perform a bunch of statements then shut down, having them all happen in the context of a single connection shouldn't be a problem.

If the app is going to hang around a while though and hold onto the connection, that could create a resource problem for the database. You should only hold onto a connection as long as you are going to be using it and release it when done.

In a multi-threaded app, all hell will break loose if you try to share a single connection across the multiple threads which all try to use it at the same time.
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Totally agreed with Bear Bibeault!

In simple, If you are using single database in making desktop application then make single connection with its instance and bunch of statement,when and where required, rather making number of connections and then delete bcz it will also slow down your working a bit. This is my personal experience.

Further, some database has limited connection. If your application is based on multiple user then here you might face problem.

I am using multiple connection as my desktop application is connected with multiple database at a time in a single program.


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use different connections
 
Similar Threads
sendParameter() !?!?
Pattern to retrieve data from multiple tables
JDBC multiple statements in prepatedStatement?
External Database Connection
MySQL Login Form