confusion about transactions and msdtc

前端 未结 3 2076
生来不讨喜
生来不讨喜 2021-02-04 15:09

I have some basic confusion about how transactions and msdtc work together.

I have a basic server/client winforms app. The app uses transactionscope to encapsulate sever

3条回答
  •  甜味超标
    2021-02-04 15:31

    Update: I found an article that explains why transactions are being promoted form LTM to MSDTC when only usign both GetData and Update on the same data adapter within a TransactionScope along with a workaround.

    The definitive TableAdapters + Transactions blog post http://blah.winsmarts.com/2006/06/18/the-definitive-tableadapters--transactions-blog-post.aspx

    I understand the part about having multiple connections open at once escalating a transaction to be distributed. However, I'm having a problem where there is only one connection, and one query against a database that is escalating it. There are not any transactions in the stored procedure either. If anyone has a clue, I'd like to hear of it. From my code example, the "adapter.Update(table)" will trigger a distributed transaction.

    I've brought the guts of the code out of my existing project and simplified most of what was going on, and I am still having the same problems. This is basically creating a dataset with a table adapter and setting it up with a stored procedure to select, insert, and delete. I select all related records with a specific user. Then, depending on if a "myPPID" exists for one of the records, I add it or delete it. I then call the update method and see the transaction escalate to be distributed by watching the Transaction Statistics in component services.

    I'm using windows XP Pro SP3 and .Net Framework 3.5 for the client program. It connects to a SQL 2005 database over the LAN to Windows Server 2003 R2 Enterprise Edition SP2.

    private void button1_Click(object sender, EventArgs e)
    {
    int userId = 3;
    int myPPId = 881;
    using (TransactionScope ts = new TransactionScope())
    {
        using (DataSet1TableAdapters.AssignedPPTableAdapter adapter 
        = new MSDTCPromotionTest.DataSet1TableAdapters.AssignedPPTableAdapter())
        {
            using (DataSet1.AssignedPPDataTable table = adapter.GetData(userId))
            {
                DataSet1.AssignedPPRow row = table.FindByUserIdmyPPId(
                    userId, myPPId);
                if (row == null)
                {
                    table.AddAssignedPPRow(userId, myPPId, string.Empty, 
                        string.Empty, true);
                }
                else
                {
                    row.Delete();
                }
                adapter.Update(table);
            }
            ts.Complete();
        }
    }
    }
    

    The connection string is nothing special:

    
    

    Also, the stored procedures are simple crud calls.

    Create:

    ALTER procedure [dbo].[p_UserForm_AssignedPP_Insert]
    (
        @UserId INT,
        @myPPId int
    )
    AS
    SET NOCOUNT ON;
    INSERT INTO [UsermyPP] ([UserID],[myPPID],[DateCreated])
         VALUES (@UserId,@myPPId,GETutcDATE()) 
    

    Read:

    ALTER procedure [dbo].[p_UserForm_AssignedPP_SelectByUserId]
    (
        @UserId int
    )
    AS
    SELECT  
        [UserId],
        [myPPId], 
        '' Title,
        '' Abbreviation,
        0 IsArchived
    from
        UsermyPP  unpp
    where
        unpp.[userid] = @UserId
    

    Delete:

    ALTER procedure [dbo].[p_UserForm_AssignedPP_Delete]
    (
        @Original_UserId INT,
        @Original_MyPPId INT
    )
    AS
    SET NOCOUNT ON;
    DELETE FROM usermypp WHERE [UserID] = @Original_UserId 
        AND [MyPPID] = @Original_MyPPId
    

提交回复
热议问题