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?
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!
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.
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.
M/s. Anand Karia Concreting IT
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop