net c# lock statement in data access layer

Posted by Pedro Rivera on Stack Overflow See other posts from Stack Overflow or by Pedro Rivera
Published on 2011-01-01T04:49:15Z Indexed on 2011/01/01 4:53 UTC
Read the original article Hit count: 263

I saw a code where they have the data access layer like this:

public class CustomerDA{  

    private static readonly object _sync = new object();  
    private static readonly CustomerDA _mutex = new CustomerDA();  

    private CustomerDA(){  
    }

    public CustomerDA GetInstance(){    

        lock(_sync){      
            return _mutex;        
        }    
    }  

    public DataSet GetCustomers(){  
        //database SELECT
        //return a DataSet
    }  

    public int UpdateCustomer(some parameters){  

        //update some user
    }

}  


public class CustomerBO{  

    public DataSet GetCustomers(){  

        //some bussiness logic  
        return CustomerDA.GetInstance().GetCustomers();
    }
}

I was using it, but start thinking... "and what if had to build a facebook like application where there are hundreds of thousands of concurrent users? would I be blocking each user from doing his things until the previous user ends his database stuff? and for the Update method, is it useful to LOCK THREADS in the app when database engines already manage concurrency at database server level?"

Then I started to think about moving the lock to the GetCustomers and UpdateCustomer methods, but think again: "is it useful at all?"

© Stack Overflow or respective owner

Related posts about locking

Related posts about data-access-layer