WCF中的程序事务和数据库事务转换

    服务定义如下:

    [ServiceContract(SessionMode = SessionMode.Required)]
    public interface IComplexService
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        void ExecuteNoneQuery1(string sql);
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        void ExecuteNoneQuery2(string sql);
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        void ExecuteNoneQuery3(string sql);
    }

    服务实现如下:

    [ServiceBehavior(TransactionTimeout = "00:00:45",TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable)]
    public class ComplexServiceImplement : IComplexService
    {
        private static void ExecuteNoneQuery(string sql)
        {
            string sqlConnectionString = "Password=sa;User ID=sa;Initial Catalog=ipedf;Data Source=.";
            string oraConnectionString = "User ID=sample;Password=sample;Data Source=insapp";
            using (OracleConnection oraConnection = new OracleConnection(oraConnectionString))
            {
                try
                {
                    oraConnection.Open();
                    OracleCommand cmd = oraConnection.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    string Debug = e.Message;
                }
                finally
                {
                    oraConnection.Close();
                }
            }
            using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
            {
                try
                {
                    sqlConnection.Open();
                    SqlCommand cmd = sqlConnection.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    string Debug = e.Message;
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }
        [OperationBehavior(
        TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void ExecuteNoneQuery1(string sql)
        {
            ExecuteNoneQuery(sql);
            //OperationContext.Current.SetTransactionComplete();
        }
        [OperationBehavior(
        TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void ExecuteNoneQuery2(string sql)
        {
            ExecuteNoneQuery(sql);
            //OperationContext.Current.SetTransactionComplete();
        }
        [OperationBehavior(
        TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void ExecuteNoneQuery3(string sql)
        {
            ExecuteNoneQuery(sql);
            //OperationContext.Current.SetTransactionComplete();
        }

        服务端配置如下:

       <system.serviceModel>
       <!--提供的服务-->
       <services>
       <service name="Wf.Complex.Service.ComplexServiceImplement" behaviorConfiguration="myServiceBehavior">
       <endpoint address="" binding="wsHttpBinding" bindingConfiguration="transactionalWsatHttpBinding" contract="Wf.Complex.Service.IComplexService"/>
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
       </service>
       </services>
       <behaviors>
       <serviceBehaviors>
       <behavior name="myServiceBehavior">
       <serviceMetadata httpGetEnabled="True"/>
       <serviceDebug includeExceptionDetailInFaults="False"/>
       </behavior>
       </serviceBehaviors>
       </behaviors>
       <bindings>
       <wsHttpBinding>
       <binding name="transactionalWsatHttpBinding" transactionFlow="true"/>

       </wsHttpBinding>
       </bindings>
       </system.serviceModel>

        客户端代码如下:

        IComplexService complex = new ComplexServiceClient();
        try
        {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = "delete from student";
                    complex.ExecuteNoneQuery1(sql);
                    sql = "insert into student(id,name) values(1,'yeyang_1')";
                    complex.ExecuteNoneQuery2(sql);
                    sql = "insert into student(id,name) values(2,'yeyang_2')";
                    complex.ExecuteNoneQuery3(sql);
                    scope.Complete();
                }
                Console.WriteLine("succeed");
        }
        catch (Exception expt)
        {
                Console.WriteLine(expt.Message);
        }
        finally
        {
                (complex as ComplexServiceClient).Close();
        }   

        运行结果:

        服务端的ExecuteNoneQuery方法中using (OracleConnection oraConnection = new OracleConnection(oraConnectionString))代码块发生异常,异常内容如下:

        "无法加载 DLL“oramts.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"

        结论:

        对于数据库事务,可以通过程序事务变形的方式来实现,但必须获取对应数据库产品的支持,sqlserver产品默认支持,oracle数据库产品需要考虑本风险,及时规避.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值