異常提示BeginTrans Nested transactions are not supported.(即事務嵌套異常)
檢查代碼並沒有發現嵌套事務問題,最後發現是AUTOCOMMIT參數設定問題
SET GLOBAL AUTOCOMMIT=0 時事務需要在使用sql之前(包含OpenDataTable)
SET GLOBAL AUTOCOMMIT=1 時事務可以在任何位置
try
{
//conn.BeginTrans();//SET GLOBAL AUTOCOMMIT=0時事務需要放在開頭
//獲取任務單號的的表名
DataTable Dtemp = conn.OpenDataTable("select t_name from lrtablefilter where task_no='" + TaskNo + "'", CommandType.Text);
if (Dtemp.Rows.Count > 0)
{
string tableName = Dtemp.Rows[0]["t_name"].ToString();
conn.BeginTrans();//事務放在這個地方提示事務嵌套,挪到查詢前面就沒有問題 問題由於SET GLOBAL AUTOCOMMIT=0;
//獲取執行前最大uid ,for update 寫鎖,必須放在事務里,否則沒有意義
Dtemp = conn.OpenDataTable("select ifnull(max(uid),0) from " + tableName, CommandType.Text);
if (Dtemp.Rows.Count > 0)
{
result = Dtemp.Rows[0][0].ToString();
}
MultInsert(conn, DT, tableName, "from_sit", SitNo);
//獲取執行后最大uid
Dtemp = conn.OpenDataTable("select ifnull(max(uid),0) from " + tableName, CommandType.Text);
if (Dtemp.Rows.Count > 0)
{
result += "," + Dtemp.Rows[0][0].ToString();
}
conn.Commit();
}
Dtemp.Dispose();
Dtemp = null;
}
catch (Exception ex)
{
if (conn._Transaction != null)
{
conn.Rollback();
}
throw ex;
}