Question about transaction in Web service

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top