Connection timeout when using transactions

N

Natan Vivo

I'm getting a timeout error while trying to select data from SQL server
2005 after opening a transaction in asn asp.net application.

It goes like this:

SqlConnection conn1 = new SqlConnection(...);
conn1.Open();
SqlTransaction trans = conn1.Open();

// do some inserts and updates using the transaction

SqlConnection conn2 = new SqlConnection(...);
conn2.Open();

// select using conn2 and decide what to do

conn2.Close();

// do more inserts and updates based on that decision

trans.Commit();
conn1.Commit();

It probably has something to do with the connection pool returning the
already open conn1 to connection2.

In the actual process, the select using the 2nd connection is being
opened in another method, and I don't want to overload the method to
receive the transaction.

Is there any other option? Is there any way to really open another
connection, or the connection pool to not return open connections being
used in transactions?

Thanks.
 
T

Tracy McKibben

Natan said:
I'm getting a timeout error while trying to select data from SQL server
2005 after opening a transaction in asn asp.net application.

It goes like this:

SqlConnection conn1 = new SqlConnection(...);
conn1.Open();
SqlTransaction trans = conn1.Open();

// do some inserts and updates using the transaction

SqlConnection conn2 = new SqlConnection(...);
conn2.Open();

// select using conn2 and decide what to do

conn2.Close();

// do more inserts and updates based on that decision

trans.Commit();
conn1.Commit();

It probably has something to do with the connection pool returning the
already open conn1 to connection2.

In the actual process, the select using the 2nd connection is being
opened in another method, and I don't want to overload the method to
receive the transaction.

Is there any other option? Is there any way to really open another
connection, or the connection pool to not return open connections being
used in transactions?

Thanks.

Have you changed the default CommandTimeout from the default? I think
the default for ASP/ASP.NET is 30 seconds.
 
N

Natan Vivo

Tracy said:
Have you changed the default CommandTimeout from the default? I think
the default for ASP/ASP.NET is 30 seconds.

No, and it is not the problem. The whole process takes less than 1 second.

The thing is exactly what I told. If I open an second connection and
select something, while connection one is in transaction, it hangs and
gives me a connection timeout.

Can anyone reproduce this?
 
T

Tracy McKibben

Natan said:
No, and it is not the problem. The whole process takes less than 1 second.

The thing is exactly what I told. If I open an second connection and
select something, while connection one is in transaction, it hangs and
gives me a connection timeout.

Can anyone reproduce this?

Not knowing what selects you're running in connection 2, I'd have to say
you're probably deadlocking against the open transaction in connection 1.

http://realsqlguy.com/twiki/bin/view/RealSQLGuy/SimulatingADeadlock
 
N

Neil B

Natan said:
No, and it is not the problem. The whole process takes less than 1 second.

The thing is exactly what I told. If I open an second connection and
select something, while connection one is in transaction, it hangs and
gives me a connection timeout.

Can anyone reproduce this?

Yes, I had the exact same problem in an application of mine. In the
absence of finding a solution, I coded around it. Sorry I can't be of
more help, but you're not alone. :)

Neil B
 
D

Damien

Natan said:
I'm getting a timeout error while trying to select data from SQL server
2005 after opening a transaction in asn asp.net application.

It goes like this:

SqlConnection conn1 = new SqlConnection(...);
conn1.Open();
SqlTransaction trans = conn1.Open();

// do some inserts and updates using the transaction

SqlConnection conn2 = new SqlConnection(...);
conn2.Open();

// select using conn2 and decide what to do

conn2.Close();

// do more inserts and updates based on that decision

trans.Commit();
conn1.Commit();

It probably has something to do with the connection pool returning the
already open conn1 to connection2.
No, that's not how the connection pool works. If you have two open
connections in your code, you'll be using two connections from the
connection pool.
In the actual process, the select using the 2nd connection is being
opened in another method, and I don't want to overload the method to
receive the transaction.

Is there any other option? Is there any way to really open another
connection, or the connection pool to not return open connections being
used in transactions?

From your description, it's almost certainly a deadlock on sql server.
Without seeing the database code, it's going to be difficult to help
you out.

To confirm it's a deadlock, open a connection using query analyser,
start your code running, then run sp_who2. It'll show your second
connection being blocked by your first.

Damien
 
N

Natan Vivo

Tracy said:
Not knowing what selects you're running in connection 2, I'd have to say
you're probably deadlocking against the open transaction in connection 1.

http://realsqlguy.com/twiki/bin/view/RealSQLGuy/SimulatingADeadlock

Thanks. It was really a deadlock, the select was selecting data from the
same table I had already update.

It seems I can solve this by using the right IsolationLevel when
begining the transaction.

I'll test it later. Thanks!
 
T

Tracy McKibben

Natan said:
Thanks. It was really a deadlock, the select was selecting data from the
same table I had already update.

It seems I can solve this by using the right IsolationLevel when
begining the transaction.

I'll test it later. Thanks!

Be careful that you're not reading "dirty" data, meaning data that is
inconsistent because it's been partially altered by a transaction...
 

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

Top