Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 Oracle multithreading , +
I have two method “ExecuteNoQuery” (performs dbCommand.ExecuteNonQuery()) and “Query” performs (dbCommand.ExecuteReader()). Both the methods are using same connection object. In ExecuteNoQuery method a lock is implemented(using connection object) and Query method implemented with out lock. In case of multiple thred, different thread accessing both the method simultaneously then what will happen?
Note: In Query method custom connection pooling is implemented with the same object.
 
public int ExecuteNoQuery(string sqlquery, Hashtable htData) {
try {
 lock(Myservice.dbcon)  
  {
    using (OracleCommand dbCommand = new OracleCommand(sqlquery, Myservice.dbcon)) 
        {
              int rowCount = dbCommand.ExecuteNonQuery();
              return 1;
        }
  }
}
 
public OracleDataReader Query(string sqlquery, Hashtable htData)
    {
        try
        {
            OracleDataReader dbReader = null;
            Random ran = new Random();
            int randomnumber = ran.Next(1,5);
           Myservice.dbcon = (OracleConnection) Myservice.htdbcon
           ["Connection_" +randomnumber];
            if (Myservice.dbcon.State != System.Data.ConnectionState.Executing 
              || Myservice.dbcon !=  System.Data.ConnectionState.Fetching)
                {
                    using (OracleCommand dbCommand = new OracleCommand(sqlquery,
                     Myservice.dbcon))
                    { 
                        dbReader = dbCommand.ExecuteReader();
                    }
                }
                return dbReader;
        }
Posted 15-Feb-13 2:24am
Edited 15-Feb-13 10:09am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If some lock statement on the same object is used only in one place of the code, it is functionally equivalent to the situation when no lock is used. Isn't this obvious?
 
Locks implement mutial exclusion mechanism which works for only for identical lock object. If two or more blocks of code are executed under lock statement with the same lock object, the execution of all these blocks is mutually exclusive. It is not important if this is the same block of code or different block executed under the lock statement with the same lock object; in all cases, each of the blocks can be executed by only one thread at a time, other threads being delayed.
 
For some background on the topic, please see: http://en.wikipedia.org/wiki/Mutual_exclusion[^].
 
See also: http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100