Link to home
Start Free TrialLog in
Avatar of ScottieSLG
ScottieSLG

asked on

TransactionScope hanging on second connection open

I am having trouble getting my TransactionScope to work correctly.  I need to access 2 different databases as a single transaction.  I can modify database one just fine, but when I try and open database two, it hangs.

I have enabled Network DTC Access, Allow Remote Clients, Allow Remote Administration, Allow Inbound, Allow Outbound and No Authentication Required for both my PC and the server where SQL Server is hosted.

This is a multi-threaded windows forms app.

I have posted a sample code snippet and where it is hanging.

My dev system is running Vista 64 and VS2008.  My SQL Environment is running on Windows Server 2008 with SQL Server 2008.

Thanks,
-Scott
public void TestTransactionScope()
{
  using (TransactionScope scope = new TransactionScope())
  {
    using (SqlConnection cn1 = new SqlConnection("Data Source=solitude;Database=DB1;User ID=CSDBUser;Password=PWD;Enlist=True;"))
    {
      using (SqlCommand cmd1 = new SqlCommand("Update Orders Set CustomerPO='NewValue' Where OrderID='SLGTest'", cn1))
      {
        cn1.Open();
        cmd1.ExecuteNonQuery();
      }
    }
 
    using (SqlConnection cn2 = new SqlConnection("Data Source=solitude;Database=DB2;User ID=CSDBUser;Password=PWD;Enlist=True;"))
    {
      using (SqlCommand cmd2 = new SqlCommand("Update PM40100 Set AgeBy=1", cn2))
      {        
        cn2.Open();  // <--  Hangs on this open
        cmd2.ExecuteNonQuery();
      }
    }
 
    scope.Complete();
  }
}

Open in new window

Avatar of williamcampbell
williamcampbell
Flag of United States of America image

If you remove the first db call does it still hang?


Avatar of ScottieSLG
ScottieSLG

ASKER

No, it doesn't hang on the second one if I try it independently.

I tried them independently, and I also tried reversing the order of the 2 SqlCommands and the first one works while the second one hangs.

It also appears that if I change the password to an invalid password in the connection string on the 2nd connection, it will give me an SqlException with an invalid password.
Have you tried closing cn1 ?
Tried closing cn1.  No change.
SOLUTION
Avatar of williamcampbell
williamcampbell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you figured it out