Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization Problem whil accessing JDBC Code

 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my project i am having DBUtils package. This package consists of classes with all static methods that interact with database. My project is web based.will i get concurrency problems when accessing static methods of my DBUtils package.

Can any one tell me where can i get concurrency problem...

Thanks & Regards,
Vipul Kumar Bondugula.
 
Abhishek Ralhan
Ranch Hand
Posts: 40
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard in a web application, static methods would access database.
Would like to know the responses though.

Anyone please respond..
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhishek,

Sorry ,yesterday i posted wrongly..DBUtils is a class not a package.
I am posting a sample code ..static method that interacts with database


This is the sample code.I am having DAO layer.In DAO I won't write the JDBC code.I am writing in DBUtils class.
In DAO's iam calling the static methods of DBUtils class

So when 2 users send request then 2 threads will create.while accessing this static method any concurrent issues arise or not.
If arise ,what is the solution.
Please anyone tell the solution..
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency problems will arise. Please synchronize all the static methods in the DBUtils class.
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need to synchronize all the static methods. Especially if your static methods do not modify any shared resources. Databases have their own implementation of concurrency control. You need to visit each and every method and evaluate whether or not you need to synchronize. These decisions has to be taken on a case by case basis.
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sudheer Bhat,..

can you guide me how to know the different cases. Is there any document on how & when to synchronize...

Thanks In Advance
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every one,

Please anyone guide me in when to use synchronization.Please provide any document.

Thanks In Advance
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is everything static? That's where the problem lies. Fix the problem, don't band-aid it with synchronization.
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks For Your Reply Bear,

In my project iam almost everywhere using DBUtils class which consists of database access static methods.Suppose ,If i go on creating the objects for DBUtils class then a lot of objects will be created.Instead of creating objects i made my methods static. I thought that adds support to performance....

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating objects are almost never a performance problem. The time to create an object is negligible compared to a single database access.

What does this method do?
con = ConnectionManager.getInstance().getConnection();
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne,

ConnectionManager is a singleton class.ConnectionManager.getInstance() returns the instance and ConnectionManager.getInstance.getConnection method returns connection object.

In the getConnection method i wrote the database connection code.

I used the DBUtils class almost everywhere in the project. Now if make every method in DBUtils as instance method, my team had to work a lot.In the whole project they need to modify..So ,tell me that i can modify in only DBUtils class.

Thanking & Regards,
Vipul Reddy Bondugula.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote: ConnectionManager is a singleton class.ConnectionManager.getInstance() returns the instance and ConnectionManager.getInstance.getConnection method returns connection object.

In the getConnection method i wrote the database connection code.

I see. And where do you close the connection?
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In DAO layer I call this method and get the connection object.Then in the DAO layer i'll close the connection.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic