2011-06-29 178 views
0

在我的應用程序的事務範圍不工作的第二transaction..It工作正常意味着,如果一個異常被拋出了第一個回滾事務,但第二個沒..交易範圍不適用於第二筆交易?對於第一筆交易

public string InsertRealm(string adminuser, string adminpassword, string CustomerCode, string CustomerName, string address, string email, string phone, string NoOfUsers, string ProductType) 
{ 

    try 
    { 
    using (Transactions.TransactionScope scope = new Transactions.TransactionScope(TransactionScopeOption.Required, new TimeSpan(2, 0, 0))) { 
     string _strServer = sqlmethod.Read_config("deploymentSetting", "Server"); 
     string _strDatabase = sqlmethod.Read_config("deploymentSetting", "database"); 
     string _strUserid = sqlmethod.Read_config("deploymentSetting", "username"); 
     string _strPassword = sqlmethod.Read_config("deploymentSetting", "password"); 
     string constr_str = "server=" + _strServer + ";" + "database=" + _strDatabase + ";" + "uid=" + _strUserid + ";" + "pwd=" + _strPassword + ";"; 

     string _blankdbCrm = sqlmethod.Read_config("dbSettings", "blankcrmdbName"); 
     string userCountdb = sqlmethod.Read_config("dbSettings", "userCountdb"); 
     string bakRestorePath = sqlmethod.Read_config("dbSettings", "bakRestorePath"); 
     string mdfRestorePath = sqlmethod.Read_config("dbSettings", "mdfRestorePath"); 
     string ldfRestorePath = sqlmethod.Read_config("dbSettings", "ldfRestorePath"); 
     string _dbCrm = ""; 

     con.ConnectionString = constr_str; 


     if (flag == "Yes") 
     { 
     SqlCommand cmddb = new SqlCommand("crt_crm_db_details", con); 
     cmddb.CommandType = CommandType.StoredProcedure; 
     cmddb.CommandTimeout = 0; 
     cmddb.Parameters.Add("@custname", SqlDbType.NVarChar).Value = ccode; 
     cmddb.Parameters.Add("@blank_dbname", SqlDbType.NVarChar).Value = _blankdbCrm; 
     cmddb.Parameters.Add("@blank_bakpath", SqlDbType.NVarChar).Value = bakRestorePath; 
     cmddb.Parameters.Add("@blank_mdfpath", SqlDbType.NVarChar).Value = mdfRestorePath; 
     cmddb.Parameters.Add("@blank_ldfpath", SqlDbType.NVarChar).Value = ldfRestorePath; 
     cmddb.Parameters.Add("@usercnt", SqlDbType.NVarChar).Value = userCountdb; 

     SqlParameter Typedb = new SqlParameter("@errorid", SqlDbType.NVarChar); 
     Typedb.Direction = ParameterDirection.Output; 
     Typedb.Value = null; 
     Typedb.Size = 50; 
     cmddb.Parameters.Add(Typedb); 
     try 
     { 
      con.Open(); 
      cmddb.ExecuteNonQuery(); 
     } 
     catch (SqlException ee) 
     { 
      VWLogger.LogMessage("Exception in crt_crm_db_details:", TraceEventType.Critical); 
      VWLogger.LogMessage(ee, TraceEventType.Critical); 
      return ee.Message; 
      flag = ee.Errors(0).ToString(); 
     } 
     con.Close(); 
     } 

     if (flag == "Yes") 
     { 
     Ramco.VW.Types.Message[] Msg_cust = null; 
     try 
     { 
      Msg_cust = client.addCustomer(ccode, cname, add1, "", mail); 
     } 
     catch (SqlException ex) 
     { 
      VWLogger.LogMessage("Exception in addCustomer:", TraceEventType.Critical); 
      VWLogger.LogMessage(ex, TraceEventType.Critical); 
      return ex.Message; 
      flag = ex.Errors(0).ToString(); 
     } 
     } 

     scope.Complete(); 
     return "Success"; 
    } 

    } 
    catch (Exception ex) 
    { 
    VWLogger.LogMessage("Exception in insertrealm:", TraceEventType.Critical); 
    VWLogger.LogMessage(ex, TraceEventType.Critical); 
    return ex.Message; 
    throw new CustomSoapException(CustomSoapException.ExceptionCode.AuthenticationException, ex.Message); 
    } 

} 

這裏cmddb.ExecuteNonQuery()得到回滾,但這一個

Msg_cust = client.addCustomer(ccode, cname, add1, "", mail) 

沒有得到回滾任何建議?

編輯:

<bindings> 
     <basicHttpBinding> 
     <binding name="UserNameTokenOverSSLBinding"> 
      <security mode="TransportWithMessageCredential" /> 
     </binding> 
     <binding name="BinaryCertTokenOverSSLBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00"> 
      <security mode="TransportWithMessageCredential"> 
      <message clientCredentialType="Certificate" /> 
      </security> 
     </binding> 
     <binding name="Vw30BasicHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="65536" maxReceivedMessageSize="65536"> 
      <security> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
     <customBinding> 
      <binding name="Vw30netTcpHABinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"> 
      <binaryMessageEncoding> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      </binaryMessageEncoding> 
      <tcpTransport maxBufferPoolSize="524288" maxReceivedMessageSize="99999998" connectionBufferSize="8192" hostNameComparisonMode="StrongWildcard" channelInitializationTimeout="00:01:00" maxBufferSize="99999998" maxPendingConnections="20" maxOutputDelay="00:00:00.2000000" maxPendingAccepts="5" transferMode="Buffered" listenBacklog="20" portSharingEnabled="false" teredoEnabled="false"> 
      <connectionPoolSettings groupName="default" leaseTimeout="00:00:02" idleTimeout="00:02:00" maxOutboundConnectionsPerEndpoint="20" /> 
      </tcpTransport> 
      </binding> 
      <binding name="Vw30HttpHABinding"> 
      <textMessageEncoding /> 
      <httpTransport keepAliveEnabled="false"> 
      </httpTransport> 
     </binding> 

     <binding name="UserNameTokenOverSSLOffloaderBinding"> 
      <textMessageEncoding messageVersion="Soap11" /> 
      <security authenticationMode="UserNameOverTransport"> 
      <secureConversationBootstrap /> 
      </security> 
      <sslOffloadedHttpsTransport /> 
     </binding> 
     <binding name="BinaryCertTokenOverSSLOffloaderBinding"> 
      <textMessageEncoding messageVersion="Soap11" /> 
      <security authenticationMode="UserNameOverTransport"> 
      <secureConversationBootstrap /> 
      </security> 
      <sslOffloadedHttpsTransport /> 
     </binding> 
     </customBinding> 
     <netTcpBinding> 
     <binding name="Vw30netTcpBinding" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:05:00" sendTimeout="00:05:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" protectionLevel="None" /> 
      <message clientCredentialType="None" /> 
      </security> 
     </binding> 
     </netTcpBinding> 
     <wsHttpBinding> 
     <binding name="WSUserNameBinding" transactionFlow="true"> 
      <security> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="UserName" negotiateServiceCredential="false" establishSecurityContext="false" /> 
      </security> 
     </binding> 
     <binding name="WSBinaryCertBinding" transactionFlow="true"> 
      <security> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="Certificate" negotiateServiceCredential="false" establishSecurityContext="false" /> 
      </security> 
     </binding> 
     <binding name="WSUserNameReliableBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="true"> 
      <reliableSession enabled="true" /> 
      <security> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="UserName" negotiateServiceCredential="false" /> 
      </security> 
     </binding> 
     <binding name="WsBinaryCertReliableBinding" transactionFlow="true"> 
      <reliableSession enabled="true" /> 
      <security> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="Certificate" negotiateServiceCredential="false" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
+0

什麼是'client'?它似乎是一個Web服務調用,在這種情況下,交易範圍當然不適用。 –

+0

ya..its一個Web服務調用.. – bala3569

+0

但我要回滾also..what我必須做 – bala3569

回答

2

什麼網絡服務的類型,你打電話,和你擁有控制權?

如果這是一個WCF Web服務,並且您可以對其進行編輯,那麼您可以使用WCF事務流程來控制來自客戶端的事務。基本上,您的TransactionScope將成爲一個分佈式事務,並轉移到WCF Web服務。您的交易將會正確回滾。

更多信息:

Enabling Transaction Flow (MSDN)

WCF Transaction Propagation (MSDN)

+0

這個。另請參見:http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.transactionoption(v=VS.100).aspx – Godeke

+0

是新來的this..how我可以檢查是否礦是WCF Web服務或asp.net webservice..it可能是愚蠢的,但沒辦法 – bala3569

+0

在我的webconfig我有<的wsHttpBinding>所以我的是WCF web服務嗎? – bala3569

0

事務範圍選項必需指定如果存在數據庫訪問代碼將加入現有的事務。如果即使第一個事務回滾,您也想提交第二個事務,則必須指定RequiresNew。

+0

人無我有回滾一切 – bala3569

+0

@ bala3569:我的錯誤!我以爲你在談論同一個數據庫上的兩個事務。您似乎試圖實施分佈式事務。 – Olaf

0

Web服務發生在不同的機器上的一個不同的線程(至少)或可能(甚至可能)。 TransactionScope是基於線程的。它的工作原理是根據當前線程註冊的當前事務。因此,在進行Web服務調用時,無法使用此工具。

+0

但我已經創建了使用.svc文件的代理並聲明瞭客戶端,如VWSecurityServicesClient client = new VWSecurityServicesClient(「WSBinaryCert」); – bala3569

+0

你創建代理與你有什麼關係?該代碼仍在另一個應用程序域(或主機!)中運行。 Web服務可能運行在互聯網上的其他地方。 (有可能是因爲您使用的是證書)如何加入您的計算機上的交易? –

+0

看看我的編輯 – bala3569