forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlTransaction.cs
182 lines (163 loc) · 7.92 KB
/
SqlTransaction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Data.Common;
using Microsoft.Data.Common;
namespace Microsoft.Data.SqlClient
{
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/SqlTransaction/*' />
public sealed partial class SqlTransaction : DbTransaction
{
private static readonly SqlDiagnosticListener s_diagnosticListener = new(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
////////////////////////////////////////////////////////////////////////////////////////
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Commit/*' />
public override void Commit()
{
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionCommitScope(_isolationLevel, _connection, InternalTransaction))
{
ZombieCheck();
using (TryEventScope.Create("SqlTransaction.Commit | API | Object Id {0}", ObjectID))
{
SqlStatistics statistics = null;
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlTransaction.Commit | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, Connection?.ClientConnectionId);
try
{
statistics = SqlStatistics.StartTimer(Statistics);
_isFromAPI = true;
_internalTransaction.Commit();
}
catch (SqlException ex)
{
diagnosticScope.SetException(ex);
// GitHub Issue #130 - When a timeout exception has occurred on transaction completion request,
// this connection may not be in reusable state.
// We will abort this connection and make sure it does not go back to the pool.
if (ex.InnerException is Win32Exception innerException && innerException.NativeErrorCode == TdsEnums.SNI_WAIT_TIMEOUT)
{
_connection.Abort(ex);
}
throw;
}
catch (Exception ex)
{
diagnosticScope.SetException(ex);
throw;
}
finally
{
SqlStatistics.StopTimer(statistics);
_isFromAPI = false;
}
}
}
}
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/DisposeDisposing/*' />
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!IsZombied && !Is2005PartialZombie)
{
_internalTransaction.Dispose();
}
}
base.Dispose(disposing);
}
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Rollback2/*' />
public override void Rollback()
{
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionRollbackScope(_isolationLevel, _connection, InternalTransaction, null))
{
if (Is2005PartialZombie)
{
// Put something in the trace in case a customer has an issue
SqlClientEventSource.Log.TryAdvancedTraceEvent("SqlTransaction.Rollback | ADV | Object Id {0}, partial zombie no rollback required", ObjectID);
_internalTransaction = null; // 2005 zombification
}
else
{
ZombieCheck();
SqlStatistics statistics = null;
using (TryEventScope.Create("SqlTransaction.Rollback | API | Object Id {0}", ObjectID))
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlTransaction.Rollback | API | Correlation | Object Id {0}, ActivityID {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, Connection?.ClientConnectionId);
try
{
statistics = SqlStatistics.StartTimer(Statistics);
_isFromAPI = true;
_internalTransaction.Rollback();
}
catch (Exception ex)
{
diagnosticScope.SetException(ex);
throw;
}
finally
{
SqlStatistics.StopTimer(statistics);
_isFromAPI = false;
}
}
}
}
}
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/RollbackTransactionName/*' />
#if NET6_0_OR_GREATER
public override void Rollback(string transactionName)
#else
public void Rollback(string transactionName)
#endif
{
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionRollbackScope(_isolationLevel, _connection, InternalTransaction, transactionName))
{
ZombieCheck();
using (TryEventScope.Create(SqlClientEventSource.Log.TryScopeEnterEvent("SqlTransaction.Rollback | API | Object Id {0}, Transaction Name='{1}', ActivityID {2}, Client Connection Id {3}", ObjectID, transactionName, ActivityCorrelator.Current, Connection?.ClientConnectionId)))
{
SqlStatistics statistics = null;
try
{
statistics = SqlStatistics.StartTimer(Statistics);
_isFromAPI = true;
_internalTransaction.Rollback(transactionName);
}
catch (Exception ex)
{
diagnosticScope.SetException(ex);
throw;
}
finally
{
SqlStatistics.StopTimer(statistics);
_isFromAPI = false;
}
}
}
}
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Save/*' />
#if NET6_0_OR_GREATER
public override void Save(string savePointName)
#else
public void Save(string savePointName)
#endif
{
ZombieCheck();
SqlStatistics statistics = null;
using (TryEventScope.Create("SqlTransaction.Save | API | Object Id {0} | Save Point Name '{1}'", ObjectID, savePointName))
{
try
{
statistics = SqlStatistics.StartTimer(Statistics);
_internalTransaction.Save(savePointName);
}
finally
{
SqlStatistics.StopTimer(statistics);
}
}
}
}
}