A
Adam
Dear all,
I seem to be having a strange problem when an sql statement times out
when it is running inside a transaction. I've written a small example
program of the problem (see below).
If the command text is changed from one that will timeout, to one that
is just invalid SQL (both throw SqlException's) the transaction
rollback behaviour is different.
In the case of a timeout I see get the following
InvalidOperationException when I try and rollback the transaction.
"This SqlTransaction has completed; it is no longer usable."
And in the case of invalid SQL the rollback complete's OK.
It seems as though the Sql timeout causes an automatic rollback, but
the invalid sql does not. This is a bit of a pain, as you can't tell
which of these have occured by looking at the exception type, but have
to examain the exception message.
Has anyone else seen any problems like this? Have I found a bug (well
quirk...) in ADO.Net or am I missing something!
TIA
Adam Pridmore
static void Main(string[] args)
{
try
{
FakeTest2();
Console.WriteLine("ok");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void FakeTest2()
{
SqlConnection connection = new SqlConnection(ConnectionString);
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.CommandText = "WAITFOR TIME '00:00:30'";
//command.CommandText = "INVALID SQL'";
command.CommandTimeout = 3;
command.Connection = connection;
command.Transaction = transaction;
try
{
command.ExecuteNonQuery();
}
catch(SqlException ex)
{
transaction.Rollback();
}
}
I seem to be having a strange problem when an sql statement times out
when it is running inside a transaction. I've written a small example
program of the problem (see below).
If the command text is changed from one that will timeout, to one that
is just invalid SQL (both throw SqlException's) the transaction
rollback behaviour is different.
In the case of a timeout I see get the following
InvalidOperationException when I try and rollback the transaction.
"This SqlTransaction has completed; it is no longer usable."
And in the case of invalid SQL the rollback complete's OK.
It seems as though the Sql timeout causes an automatic rollback, but
the invalid sql does not. This is a bit of a pain, as you can't tell
which of these have occured by looking at the exception type, but have
to examain the exception message.
Has anyone else seen any problems like this? Have I found a bug (well
quirk...) in ADO.Net or am I missing something!
TIA
Adam Pridmore
static void Main(string[] args)
{
try
{
FakeTest2();
Console.WriteLine("ok");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void FakeTest2()
{
SqlConnection connection = new SqlConnection(ConnectionString);
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.CommandText = "WAITFOR TIME '00:00:30'";
//command.CommandText = "INVALID SQL'";
command.CommandTimeout = 3;
command.Connection = connection;
command.Transaction = transaction;
try
{
command.ExecuteNonQuery();
}
catch(SqlException ex)
{
transaction.Rollback();
}
}