Skip to content

Commit 966fc6e

Browse files
Move to Shared - SqlTransaction (#1353)
1 parent 3082ae5 commit 966fc6e

File tree

5 files changed

+168
-334
lines changed

5 files changed

+168
-334
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@
256256
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs">
257257
<Link>Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs</Link>
258258
</Compile>
259+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlTransaction.Common.cs">
260+
<Link>Microsoft\Data\SqlClient\SqlTransaction.Common.cs</Link>
261+
</Compile>
259262
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs">
260263
<Link>Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs</Link>
261264
</Compile>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlTransaction.cs

+4-158
Original file line numberDiff line numberDiff line change
@@ -4,136 +4,22 @@
44

55
using System;
66
using System.ComponentModel;
7-
using System.Data;
87
using System.Data.Common;
9-
using System.Diagnostics;
108
using Microsoft.Data.Common;
119

1210
namespace Microsoft.Data.SqlClient
1311
{
1412
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/SqlTransaction/*' />
15-
public sealed class SqlTransaction : DbTransaction
13+
public sealed partial class SqlTransaction : DbTransaction
1614
{
17-
private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
18-
private static int _objectTypeCount; // EventSource Counter
19-
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
20-
internal readonly IsolationLevel _isolationLevel = IsolationLevel.ReadCommitted;
21-
22-
private SqlInternalTransaction _internalTransaction;
23-
private SqlConnection _connection;
24-
25-
private bool _isFromAPI;
26-
27-
internal SqlTransaction(SqlInternalConnection internalConnection, SqlConnection con,
28-
IsolationLevel iso, SqlInternalTransaction internalTransaction)
29-
{
30-
_isolationLevel = iso;
31-
_connection = con;
32-
33-
if (internalTransaction == null)
34-
{
35-
_internalTransaction = new SqlInternalTransaction(internalConnection, TransactionType.LocalFromAPI, this);
36-
}
37-
else
38-
{
39-
Debug.Assert(internalConnection.CurrentTransaction == internalTransaction, "Unexpected Parser.CurrentTransaction state!");
40-
_internalTransaction = internalTransaction;
41-
_internalTransaction.InitParent(this);
42-
}
43-
}
44-
45-
////////////////////////////////////////////////////////////////////////////////////////
46-
// PROPERTIES
47-
////////////////////////////////////////////////////////////////////////////////////////
48-
49-
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Connection/*' />
50-
new public SqlConnection Connection
51-
{
52-
get
53-
{
54-
if (IsZombied)
55-
{
56-
return null;
57-
}
58-
else
59-
{
60-
return _connection;
61-
}
62-
}
63-
}
64-
65-
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/DbConnection/*' />
66-
override protected DbConnection DbConnection
67-
{
68-
get
69-
{
70-
return Connection;
71-
}
72-
}
73-
74-
internal SqlInternalTransaction InternalTransaction
75-
{
76-
get
77-
{
78-
return _internalTransaction;
79-
}
80-
}
81-
82-
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/IsolationLevel/*' />
83-
override public IsolationLevel IsolationLevel
84-
{
85-
get
86-
{
87-
ZombieCheck();
88-
return _isolationLevel;
89-
}
90-
}
91-
92-
private bool Is2005PartialZombie
93-
{
94-
get
95-
{
96-
return (null != _internalTransaction && _internalTransaction.IsCompleted);
97-
}
98-
}
99-
100-
internal bool IsZombied
101-
{
102-
get
103-
{
104-
return (null == _internalTransaction || _internalTransaction.IsCompleted);
105-
}
106-
}
107-
108-
internal int ObjectID
109-
{
110-
get
111-
{
112-
return _objectID;
113-
}
114-
}
115-
116-
internal SqlStatistics Statistics
117-
{
118-
get
119-
{
120-
if (null != _connection)
121-
{
122-
if (_connection.StatisticsEnabled)
123-
{
124-
return _connection.Statistics;
125-
}
126-
}
127-
return null;
128-
}
129-
}
15+
private static readonly SqlDiagnosticListener s_diagnosticListener = new(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
13016

13117
////////////////////////////////////////////////////////////////////////////////////////
13218
// PUBLIC METHODS
13319
////////////////////////////////////////////////////////////////////////////////////////
13420

13521
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Commit/*' />
136-
override public void Commit()
22+
public override void Commit()
13723
{
13824
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionCommitScope(_isolationLevel, _connection, InternalTransaction))
13925
{
@@ -191,7 +77,7 @@ protected override void Dispose(bool disposing)
19177
}
19278

19379
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Rollback2/*' />
194-
override public void Rollback()
80+
public override void Rollback()
19581
{
19682
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionRollbackScope(_isolationLevel, _connection, InternalTransaction, null))
19783
{
@@ -284,45 +170,5 @@ public void Save(string savePointName)
284170
}
285171
}
286172
}
287-
288-
////////////////////////////////////////////////////////////////////////////////////////
289-
// INTERNAL METHODS
290-
////////////////////////////////////////////////////////////////////////////////////////
291-
292-
internal void Zombie()
293-
{
294-
// For 2005, we have to defer "zombification" until
295-
// we get past the users' next rollback, else we'll
296-
// throw an exception there that is a breaking change.
297-
// Of course, if the connection is already closed,
298-
// then we're free to zombify...
299-
SqlInternalConnection internalConnection = (_connection.InnerConnection as SqlInternalConnection);
300-
if (null != internalConnection && !_isFromAPI)
301-
{
302-
SqlClientEventSource.Log.TryAdvancedTraceEvent("SqlTransaction.Zombie | ADV | Object Id {0} 2005 deferred zombie", ObjectID);
303-
}
304-
else
305-
{
306-
_internalTransaction = null; // pre-2005 zombification
307-
}
308-
}
309-
310-
////////////////////////////////////////////////////////////////////////////////////////
311-
// PRIVATE METHODS
312-
////////////////////////////////////////////////////////////////////////////////////////
313-
314-
private void ZombieCheck()
315-
{
316-
// If this transaction has been completed, throw exception since it is unusable.
317-
if (IsZombied)
318-
{
319-
if (Is2005PartialZombie)
320-
{
321-
_internalTransaction = null; // 2005 zombification
322-
}
323-
324-
throw ADP.TransactionZombied(this);
325-
}
326-
}
327173
}
328174
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@
323323
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs">
324324
<Link>Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs</Link>
325325
</Compile>
326+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlTransaction.Common.cs">
327+
<Link>Microsoft\Data\SqlClient\SqlTransaction.Common.cs</Link>
328+
</Compile>
326329
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs">
327330
<Link>Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs</Link>
328331
</Compile>

0 commit comments

Comments
 (0)