Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Shared - SqlTransaction #1353

Merged
merged 22 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlTransaction.Common.cs">
<Link>Microsoft\Data\SqlClient\SqlTransaction.Common.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs</Link>
</Compile>
Expand Down Expand Up @@ -645,7 +648,7 @@
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnection.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs"/>
David-Engel marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="Microsoft\Data\SqlClient\SqlUtil.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.RegisterEncoding.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,136 +4,22 @@

using System;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
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 class SqlTransaction : DbTransaction
public sealed partial class SqlTransaction : DbTransaction
{
private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
private static int _objectTypeCount; // EventSource Counter
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
internal readonly IsolationLevel _isolationLevel = IsolationLevel.ReadCommitted;

private SqlInternalTransaction _internalTransaction;
private SqlConnection _connection;

private bool _isFromAPI;

internal SqlTransaction(SqlInternalConnection internalConnection, SqlConnection con,
IsolationLevel iso, SqlInternalTransaction internalTransaction)
{
_isolationLevel = iso;
_connection = con;

if (internalTransaction == null)
{
_internalTransaction = new SqlInternalTransaction(internalConnection, TransactionType.LocalFromAPI, this);
}
else
{
Debug.Assert(internalConnection.CurrentTransaction == internalTransaction, "Unexpected Parser.CurrentTransaction state!");
_internalTransaction = internalTransaction;
_internalTransaction.InitParent(this);
}
}

////////////////////////////////////////////////////////////////////////////////////////
// PROPERTIES
////////////////////////////////////////////////////////////////////////////////////////

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Connection/*' />
new public SqlConnection Connection
{
get
{
if (IsZombied)
{
return null;
}
else
{
return _connection;
}
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/DbConnection/*' />
override protected DbConnection DbConnection
{
get
{
return Connection;
}
}

internal SqlInternalTransaction InternalTransaction
{
get
{
return _internalTransaction;
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/IsolationLevel/*' />
override public IsolationLevel IsolationLevel
{
get
{
ZombieCheck();
return _isolationLevel;
}
}

private bool Is2005PartialZombie
{
get
{
return (null != _internalTransaction && _internalTransaction.IsCompleted);
}
}

internal bool IsZombied
{
get
{
return (null == _internalTransaction || _internalTransaction.IsCompleted);
}
}

internal int ObjectID
{
get
{
return _objectID;
}
}

internal SqlStatistics Statistics
{
get
{
if (null != _connection)
{
if (_connection.StatisticsEnabled)
{
return _connection.Statistics;
}
}
return null;
}
}
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/*' />
override public void Commit()
public override void Commit()
{
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionCommitScope(_isolationLevel, _connection, InternalTransaction))
{
Expand Down Expand Up @@ -191,7 +77,7 @@ protected override void Dispose(bool disposing)
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlTransaction.xml' path='docs/members[@name="SqlTransaction"]/Rollback2/*' />
override public void Rollback()
public override void Rollback()
{
using (DiagnosticTransactionScope diagnosticScope = s_diagnosticListener.CreateTransactionRollbackScope(_isolationLevel, _connection, InternalTransaction, null))
{
Expand Down Expand Up @@ -284,45 +170,5 @@ public void Save(string savePointName)
}
}
}

////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL METHODS
////////////////////////////////////////////////////////////////////////////////////////

internal void Zombie()
{
// For 2005, we have to defer "zombification" until
// we get past the users' next rollback, else we'll
// throw an exception there that is a breaking change.
// Of course, if the connection is already closed,
// then we're free to zombify...
SqlInternalConnection internalConnection = (_connection.InnerConnection as SqlInternalConnection);
if (null != internalConnection && !_isFromAPI)
{
SqlClientEventSource.Log.TryAdvancedTraceEvent("SqlTransaction.Zombie | ADV | Object Id {0} 2005 deferred zombie", ObjectID);
}
else
{
_internalTransaction = null; // pre-2005 zombification
}
}

////////////////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
////////////////////////////////////////////////////////////////////////////////////////

private void ZombieCheck()
{
// If this transaction has been completed, throw exception since it is unusable.
if (IsZombied)
{
if (Is2005PartialZombie)
{
_internalTransaction = null; // 2005 zombification
}

throw ADP.TransactionZombied(this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlTransaction.Common.cs">
<Link>Microsoft\Data\SqlClient\SqlTransaction.Common.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs">
<Link>Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs</Link>
</Compile>
Expand Down
Loading