Don’t use static synchronized methods for database access

I was busy performance tuning my J2EE application in fact struts based web application .
I came to know about a serious design issue , as i was using singleton design pattern (i guess) in my database access logic . That is all the methods for every database class were declared static , and to keep thread safety they were synchronized.

public class MyDBAccessor {
public static synchronized void accessMyDBFirst(){
// The logic for accessing the database
public static synchronized void accessMyDBSecond(){
// The logic for accessing the database

But the above code has severe performance penalty as for multiple threads accessing the database , if one thread , say T1 acquires the lock of accessMyDBFirst() , but in fact it locks the whole class methods , that another thread T2 which requires to access accessMyDBSecond() method must wait until the first T1 relieves the lock.

So this mechanism makes more more threads to wait i.e. more requests to wait which severely degrades the performance and response time.

So my sincere advice is not to use above code logic in your database access code logic ..

So i had to rework all of my database code in order to improve performance , i just did one thing removed static keyword in above code logic , and for every method access i instantiated the class from a static factory method .
This approach subsequently improves performance and response time.
The second approach is apparently actual DAO (data access object) pattern approach .
My first implementation of Data access logic was wrong and did not stick to the DAO design pattern.

So i will implementing the from DAO’s next time .
Further I am curious about ORM too . hibernate is buzzword these days .Hopefully we will use hibernate this time.

I will appreciate your thoughts on above post..


One thought on “Don’t use static synchronized methods for database access

  1. Hi dude, it is a obvious good post. I remember of making a similar blunder in an earlier project. Alas, I can’t do anything to that but will beware of this from now. Thanx….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s