2016-06-08 45 views
0

當我使用「Select ... for update」查詢代碼無一例外地停止或凍結。它只是不會去下一條指令。它適用於常規選擇查詢。任何想法爲什麼?Oracle DataAccess鎖代碼停止

string selectQuery = "select * from table_name where id = 1 FOR UPDATE"; // Code stops on data fill 
string selectQueryNoLock = "select * from table_name where id = 1"; // Code execute normally 
string connectionString = "fake"; 
OracleConnection oracleConnection = new OracleConnection(connectionString); 
Console.WriteLine("Open Connection"); 
oracleConnection.Open(); 
OracleTransaction oracleTransaction = oracleConnection.BeginTransaction(); 
OracleCommand oracleCommand = new OracleCommand(selectQuery, oracleConnection); 
oracleCommand.Transaction = oracleTransaction; 

OracleDataAdapter dataAdapter = new OracleDataAdapter(oracleCommand); 
DataSet dataSet = new DataSet(); 
dataAdapter.Fill(dataSet); 

// This line is never reached! 
Console.WriteLine("Press any key..."); 
+3

因爲另一個數據庫連接在正在進行的事務中已經在該行上有一個鎖? – sstan

+0

沒有。我嘗試在dataadapter.fill(dataSet)行上放置一個斷點,然後在Oracle SqlDeveloper的同一行上執行更新,該行未被鎖定 – Yvain

+1

這可能太明顯,但您確定是不是打開Oracle Sql Developer會話持有鎖?嘗試關閉所有Oracle Sql Developer窗口並重試。 – sstan

回答

1

標準select ... for update將無限期地等待,如果另一個事務目前持有該行上的鎖。在正常的應用程序中,這不是一個真正的問題,因爲交易應該是短暫的。

但是,如果無限期的等待是不是你想要的行爲,如果你無法掌握一定的時間後,鎖,你寧願被告知,你可以指定一個像這樣的超時:

select ... for update wait 10 

...如果超時過期並且您無法獲取該鎖定,它將返回一個錯誤。

另外,如果你不想等待所有的,你也可以這樣做:

select ... for update nowait 

...這將立即返回一個錯誤,如果你不能獲得鎖的時候了。