我有两种方法“ExecuteNoQuery”(执行 dbCommand.ExecuteNonQuery())和“Query”执行(dbCommand.ExecuteReader())。这两种方法都使用相同的连接对象。在 ExecuteNoQuery 方法中实现了一个锁(使用连接对象)和 Query 方法实现了无锁。如果有多个线程,不同的线程同时访问这两个方法,那会发生什么?
注意:在 Query 方法中,自定义连接池是使用相同的对象实现的。
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;
}