• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when to use different connections

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic