Skip to content

Commit

Permalink
Move to Shared - SqlTransaction (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaur-Parminder authored Jun 9, 2022
1 parent 3082ae5 commit 966fc6e
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 334 deletions.
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
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 @@ -323,6 +323,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

0 comments on commit 966fc6e

Please sign in to comment.