Skip to content

Commit c67e525

Browse files
committed
feat: move transaction close events after lock
1 parent 7b134a5 commit c67e525

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

src/TagBites.DB/DB/DbLinkContext.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,8 @@ private void SystemTransactionCompleted(object sender, TransactionEventArgs e)
783783
}
784784
finally
785785
{
786-
CloseTransaction(0);
786+
var closeEvents = CloseTransaction(0);
787+
closeEvents();
787788
}
788789
}
789790
finally
@@ -870,7 +871,7 @@ internal void MarkTransaction(bool rollback, bool rollbackCalled = false)
870871
}
871872
}
872873
}
873-
internal void CloseTransaction(int level)
874+
internal Action CloseTransaction(int level)
874875
{
875876
Exception ex = null;
876877
Action closeTransactionEvent = null;
@@ -933,32 +934,35 @@ internal void CloseTransaction(int level)
933934
_transactionContext.Status = DbLinkTransactionStatus.None;
934935
_transactionContext.ForceRelease();
935936
_transactionContext = null;
937+
}
938+
}
936939

937-
// Transaction Closed
938-
try
939-
{
940-
closeTransactionEvent?.Invoke();
941-
}
942-
catch (Exception ex2)
943-
{
944-
ex = ToAggregateException("Exception occurred while executing TransactionClose event.", ex, ex2);
945-
}
940+
return () =>
941+
{
942+
// Transaction Closed
943+
try
944+
{
945+
closeTransactionEvent?.Invoke();
946+
}
947+
catch (Exception ex2)
948+
{
949+
ex = ToAggregateException("Exception occurred while executing TransactionClose event.", ex, ex2);
950+
}
946951

947-
// Transaction Context Closed
948-
try
949-
{
950-
closeTransactionContextEvent?.Invoke();
951-
}
952-
catch (Exception ex2)
953-
{
954-
ex = ToAggregateException("Exception occurred while executing TransactionContextClose event.", ex, ex2);
955-
}
952+
// Transaction Context Closed
953+
try
954+
{
955+
closeTransactionContextEvent?.Invoke();
956+
}
957+
catch (Exception ex2)
958+
{
959+
ex = ToAggregateException("Exception occurred while executing TransactionContextClose event.", ex, ex2);
956960
}
957961

958962
// Throw exception
959963
if (ex != null)
960964
throw ex;
961-
}
965+
};
962966
}
963967

964968
private T ExecuteInner<T>(IQuerySource source, ExecuteQueryWithRowCountDelegate<T> action)

src/TagBites.DB/DB/DbLinkTransaction.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private void CloseTransaction(bool rollback)
5757
public void Dispose()
5858
{
5959
GC.SuppressFinalize(this);
60+
Action closeEvents = null;
6061

6162
lock (_locker)
6263
{
@@ -77,7 +78,7 @@ public void Dispose()
7778
}
7879
finally
7980
{
80-
_context.CloseTransaction(_nestingLevel);
81+
closeEvents = _context.CloseTransaction(_nestingLevel);
8182
}
8283
}
8384
finally
@@ -86,6 +87,8 @@ public void Dispose()
8687
}
8788
}
8889
}
90+
91+
closeEvents?.Invoke();
8992
}
9093
}
9194
}

src/TagBites.DB/DB/DbLinkTransactionWithScope.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.Transactions;
44
using TagBites.Utils;
@@ -77,7 +77,8 @@ public void Dispose()
7777
if (m_transactionScope != null)
7878
try
7979
{
80-
m_context.CloseTransaction(m_nestingLevel);
80+
var closeEvents = m_context.CloseTransaction(m_nestingLevel);
81+
closeEvents();
8182
}
8283
finally
8384
{

0 commit comments

Comments
 (0)