• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use different connections

 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic