Reuse Transaction?

A

Axel Dahmen

Hi,

after committing a transaction I'd like to create a new transaction on an existing connection. However, this doesn't seem to work.

Here's some pseudo code I'm using

using (SqlConnection con = new SqlConnection())
{
SqlTransaction trans = con.BeginTransaction();
...
trans.Commit();
trans = con.BeginTransaction(); // this throws an exception
}

Can someone please enlighten me on why this throws an exception?

TIA,
www.axeldahmen.de
Axel Dahmen
 
J

\Ji Zhou [MSFT]\

Hello Axel,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on the test in my side, I can create a new Transaction using
connection.BeginTransaction after the previous one is committed. It does
not pop up any exception to me. The codes in my side look like,

private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

//First Time
transaction = connection.BeginTransaction("SampleTransaction");

command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into People (Id, Name) VALUES (1,
'Colbert')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into People (Id, Name) VALUES (2, 'Erika')";
command.ExecuteNonQuery();

transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}",
ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}

//Second Time
transaction = connection.BeginTransaction("SampleTransaction");

command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into People (Id, Name) VALUES (3, 'Grace')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into People (Id, Name) VALUES (4, 'Emmy')";
command.ExecuteNonQuery();

transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}",
ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}

Currently, the best guess of this issue you described is the parallel
transaction. When your query returns a large amount of data and then calls
BeginTransaction, a SqlException is thrown because SQL Server 2005 does not
allow parallel transactions when using MARS. To avoid this problem, always
associate a transaction with the command, the connection, or both before
any readers are open. This information is documented here
http://msdn.microsoft.com/en-us/library/5ha4240h.aspx.

Please let me know if this fixes the issue in your side.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Miha Markic

What does the exception say?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Hi,

after committing a transaction I'd like to create a new transaction on an
existing connection. However, this doesn't seem to work.

Here's some pseudo code I'm using

using (SqlConnection con = new SqlConnection())
{
SqlTransaction trans = con.BeginTransaction();
...
trans.Commit();
trans = con.BeginTransaction(); // this throws an exception
}

Can someone please enlighten me on why this throws an exception?

TIA,
www.axeldahmen.de
Axel Dahmen
 
A

Axel Dahmen

Thanks Ji and Miha,

I'm afraid I couldn't reproduce the problem on my machine. A colleague of mine had that problem and I was quite convinced that everything was alright with his code. But since I can't reproduce the exception on my machine I believe something must be wrong with his code.

I apologize for posting here before having the problem tested myself.

Best regards,
Axel Dahmen





-----------------
""Ji Zhou [MSFT]"" said:
Hello Axel,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on the test in my side, I can create a new Transaction using
connection.BeginTransaction after the previous one is committed. It does
not pop up any exception to me. The codes in my side look like,

private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

//First Time
transaction = connection.BeginTransaction("SampleTransaction");

command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into People (Id, Name) VALUES (1,
'Colbert')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into People (Id, Name) VALUES (2, 'Erika')";
command.ExecuteNonQuery();

transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}",
ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}

//Second Time
transaction = connection.BeginTransaction("SampleTransaction");

command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into People (Id, Name) VALUES (3, 'Grace')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into People (Id, Name) VALUES (4, 'Emmy')";
command.ExecuteNonQuery();

transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}",
ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}

Currently, the best guess of this issue you described is the parallel
transaction. When your query returns a large amount of data and then calls
BeginTransaction, a SqlException is thrown because SQL Server 2005 does not
allow parallel transactions when using MARS. To avoid this problem, always
associate a transaction with the command, the connection, or both before
any readers are open. This information is documented here
http://msdn.microsoft.com/en-us/library/5ha4240h.aspx.

Please let me know if this fixes the issue in your side.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications..

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

\Ji Zhou [MSFT]\

Hello Axel,

You are more than welcome to post in the newsgroup. And do you need any
future help on this issue? If yes, would you mind posting some more
background information about the error and your colleague's development
environment? What is the exception text in his side? So that we can try to
do some more research on this.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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