在一个方法中锁定对象而在另一个方法中没有锁定 [英] lock object in one mthod and no lock in another method

查看:134
本文介绍了在一个方法中锁定对象而在另一个方法中没有锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个方法ExecuteNoQuery(执行dbCommand.ExecuteNonQuery())和Query执行(dbCommand.ExecuteReader())。这两种方法都使用相同的连接对象。在ExecuteNoQuery方法中,实现了一个锁(使用连接对象),并使用out lock实现了Query方法。如果是多个thred,不同的线程同时访问这两个方法然后会发生什么?

注意:在Query方法中,自定义连接池是用同一个对象实现的。



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;
        }

推荐答案

如果同一对象上的某些锁定语句仅在代码的一个位置使用,则它在功能上是相当于没有使用锁的情况。这不是很明显吗?



锁定实现 mutial排除机制,该机制仅适用于相同的锁定对象。如果在具有相同锁对象的 lock 语句下执行两个或更多代码块,则所有这些块的执行都是互斥。如果这是在具有相同锁对象的 lock 语句下执行的相同代码块或不同块,则这一点并不重要;在所有情况下,每个块一次只能由一个线程执行,其他线程被延迟。



有关该主题的一些背景信息,请参阅: http://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ]。



参见:http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [ ^ ]。



-SA
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


这篇关于在一个方法中锁定对象而在另一个方法中没有锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆