先贴代码,服务端方法:
[OperationBehavior(TransactionAutoComplete=true,TransactionScopeRequired=true)] [TransactionFlow(TransactionFlowOption.Allowed)] public string SaveCardLoss(string token, string strOperate, string strSaveData) { System.Transactions.Transaction myTran = System.Transactions.Transaction.Current; string strJsonResult = ""; int iResult = 0; Message myMessage = new Message(); try { #region 验证令牌 userTOtoken ut = TokenManage.tokenmanager.Instance.TokenQuery(token); if (ut == null) { return "noToken"; } #endregion List<CardLoss> listCardLoss = JsonConvert.DeserializeObject<List<CardLoss>>(strSaveData); using (var myDb = new LFACSDB<CardLoss>(LFACSDB<CardLoss>.GetConn)) { switch (strOperate) { case "add": listCardLoss.ForEach(delegate(CardLoss item) { myMessage.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); myMessage.CreateUser = ut.user_code; item.CreateDate = myMessage.CreateDate; item.CreateUser = myMessage.CreateUser; iResult = myDb.Insert(item); myMessage.ResultInfo = item.AutoID.ToString(); }); break; case "edit": listCardLoss.ForEach(delegate(CardLoss item) { item.ModiDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); item.ModiUser = ut.user_code; myMessage.ResultInfo = myDb.Update(item).ToString(); }); break; case "del": listCardLoss.ForEach(delegate(CardLoss item) { myMessage.ResultInfo = myDb.Delete(item).ToString(); }); break; } } strJsonResult = JsonConvert.SerializeObject(myMessage); } catch (Exception ex) { throw ex; } return strJsonResult; }
客户端代码:
using (System.Transactions.TransactionScope myTran = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required)) { using (AccessService.AccessServiceClient myClient = new AccessService.AccessServiceClient()) { if (strOperas == "add" || strOperas == "edit" || strOperas == "del") { string strsaveData = context.Request.Form["saveData"]; strResult = myClient.SaveCardLoss(token, strOperas, strsaveData); myTran.Complete(); //提交事务 context.Response.Write(strResult); } }}
这里的问题是,事务还没有提交, myTran.Complete(); //提交事务,为什么数据库就可以查询数据出来了呢?按照事务隔离的概念,事务没有提交的话,数据是查询不出来的,会处于等待的状态,请兄弟们回答哦.......
[OperationBehavior(TransactionAutoComplete=true 已经设置了自动提交,你将TransactionAutoComplete改为false试试。
TransactionAutoComplete改为false,配置文件要做什么修改,因为我刚这样改过,但是客户端更新引用的时候报错,谢谢啦...
@勇哥哥: 将 TransactionScopeRequired = true 去掉试试
@dudu: 去掉也不行,不能过猜啊兄弟,麻烦请试下!!!
@勇哥哥: 不好意思
@dudu: 没事,只是研究下,之前是在wcf服务方法里使用的数据库事务用的也没什么问题,但是书上说要避免那样使用,说的肯定也是有点道理的,所以就研究下.你平常是怎么用的?