P
pc690403
I'm trying to figure out the exact behaviour of RequiresNew and
Required transaction attribute when applying to web service. My
understanding (from MSDN doc) of the RequiresNew attribute is that the
web method will run in its own transaction context. I wrote a small
sample program to demostrate this:
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public void Method1()
{
try
{
Method2(); // insert record in test table
Method2(); // insert the same record again and cause primary key
exception
ContextUtil.SetComplete();
}
catch(Exception ex)
{
ContextUtil.SetAbort();
}
}
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public void Method2()
{
// this method insert a row into the test table
string s = "insert into test (column_one, column_two, column_three)
values (923, 45.64, '3232')";
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandText = s;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
ContextUtil.SetComplete();
}
After I called the web method Method1() from the client, the test data
did not appear in the test table. I thougth Method2() was executed in
its own transaction context. So the outcome of Method1() should have
no impact on Method2(). Can someone explain the difference between
Required and RequiresNew. Thanks
Required transaction attribute when applying to web service. My
understanding (from MSDN doc) of the RequiresNew attribute is that the
web method will run in its own transaction context. I wrote a small
sample program to demostrate this:
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public void Method1()
{
try
{
Method2(); // insert record in test table
Method2(); // insert the same record again and cause primary key
exception
ContextUtil.SetComplete();
}
catch(Exception ex)
{
ContextUtil.SetAbort();
}
}
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public void Method2()
{
// this method insert a row into the test table
string s = "insert into test (column_one, column_two, column_three)
values (923, 45.64, '3232')";
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandText = s;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
ContextUtil.SetComplete();
}
After I called the web method Method1() from the client, the test data
did not appear in the test table. I thougth Method2() was executed in
its own transaction context. So the outcome of Method1() should have
no impact on Method2(). Can someone explain the difference between
Required and RequiresNew. Thanks