日期:2014-05-18  浏览次数:20758 次

请高手看看这段代码的可行性?谢谢
一段实现WEBSERVICE有状态的一段代码实现,请高手看看这样做的可行性?

实现思路如下:

  WebService提供创建数据库连接和控制事务的方法,在创建SqlConnection的时候同时返回给客户端一个GUID,服务器端通过把该GUID作为Key将SqlConnection对象和SqlTransaction对象放到Session(要用[WebMethod(EnableSession = true)来使Session可用])或着Application中,通过有状态的WebService记住服务器端的状态。

  具体代码如下所示:


[WebMethod(EnableSession=true)]

    public string CreateConnection()

    {

        string strGUID = Guid.NewGuid().ToString();

        SqlConnection mySqlConnection = new SqlConnection(strConnString);

        Session[strGUID + "Connection"] = mySqlConnection;

        Common.JTLog.WriteEventLog("toby", Common.JTLog.LogType.Error, strGUID + "Connection");

        return strGUID;

    }

    [WebMethod(EnableSession = true)]

    public void BeginTransaction(string strGUID)

    {

        SqlConnection mySqlConnection = (SqlConnection)Session[strGUID + "Connection"];

        if (mySqlConnection.State == ConnectionState.Closed)

        {

            mySqlConnection.Open();

        }

        SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();

        Session[strGUID + "Transaction"] = mySqlTransaction;

    }

    [WebMethod(EnableSession = true)]

    public void CommitTransaction(string strGUID)

    {

        SqlConnection mySqlConnection = (SqlConnection)Session[strGUID + "Connection"];

        SqlTransaction mySqlTransaction = (SqlTransaction)Session[strGUID + "Transaction"];

        mySqlTransaction.Commit();

        if (mySqlConnection.State == ConnectionState.Open)

        {

            mySqlConnection.Close();

        }

    }

    [WebMethod(EnableSession = true)]

    public void RollbackTransaction(string strGUID)

    {

        SqlConnection mySqlConnection = (SqlConnection)Session[strGUID + "Connection"];

        SqlTransaction mySqlTransaction = (SqlTransaction)Session[strGUID + "Transaction"];

        mySqlTransaction.Rollback();

        if (mySqlConnection.State == ConnectionState.Open)

        {

            mySqlConnection.Close();

        }

    }

    [WebMethod(EnableSession = true)]

    public int ExecuteNonQuery(string strGUID, string strCmdText)

    {

        SqlConnection mySqlConnection = (SqlConnection)Session[strGUID + "Connection"];

        SqlTransaction mySqlTransaction = (SqlTransaction)Session[strGUID + "Transaction"];

        SqlCommand objSqlCommand = new SqlCommand(strCmdText);

        objSqlCommand.Connection = mySqlConnection;

        objSqlCommand.Transaction = mySqlTransaction;

        return objSqlCommand.ExecuteNonQuery();

    }
  如果用Session保存状态的话,那客户端一定要通过myService.CookieContainer = new System.Net.CookieContainer();创建一个CookieContainer 来关联Session,如果使用Application对象保存的话就没有必要了。

  客户端代码如下:


static void Main(string[] args)

        {

            TransactionService.Service myService = new TestProject.TransactionService.Service();

            myService.CookieContainer = new System.Net.CookieContainer();

            string strGUID = myService.CreateConnection();

            try

            {

                myService.BeginTransaction(strGUID);

                myService.ExecuteNonQuery(strGUID, "update ATMAccount set Amount = 1000");

                myService.Exec