@@ -14,9 +14,8 @@ public sealed class SQLiteDbTransaction : Disposable, IDbTransaction
1414 private const int ROLLBACK = 2 ;
1515 #endregion
1616
17- private SQLiteDbConnection ? _connection ;
18- private SQLiteTransaction ? _transaction ;
19- private Action ? _onDispose ;
17+ private readonly SQLiteDbConnection _connection ;
18+ private readonly SQLiteTransaction _transaction ;
2019
2120 private int _state = NONE ;
2221
@@ -29,13 +28,12 @@ public sealed class SQLiteDbTransaction : Disposable, IDbTransaction
2928 /// </summary>
3029 /// <param name="connection">The SQLite database connection.</param>
3130 /// <param name="transaction">The underlying SQLite transaction.</param>
32- /// <param name="onDispose">An action to perform upon disposal.</param>
3331 /// <exception cref="ArgumentNullException">Thrown when the provided connection or transaction is null.</exception>
34- internal SQLiteDbTransaction ( SQLiteDbConnection connection , SQLiteTransaction transaction , Action ? onDispose )
32+ internal SQLiteDbTransaction ( SQLiteDbConnection connection , SQLiteTransaction transaction )
3533 {
3634 _connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
3735 _transaction = transaction ?? throw new ArgumentNullException ( nameof ( transaction ) ) ;
38- _onDispose = onDispose ;
36+ Interlocked . Increment ( ref _connection . TransactionCount ) ;
3937 Debug . Assert ( connection . DefaultTimeout == 30 ) ;
4038 }
4139
@@ -44,12 +42,12 @@ internal SQLiteDbTransaction(SQLiteDbConnection connection, SQLiteTransaction tr
4442 /// <summary>
4543 /// Gets the database connection associated with this transaction.
4644 /// </summary>
47- public IDbConnection Connection => _transaction ! . Connection ;
45+ public IDbConnection Connection => _transaction . Connection ;
4846
4947 /// <summary>
5048 /// Gets the isolation level of this transaction.
5149 /// </summary>
52- public IsolationLevel IsolationLevel => _transaction ! . IsolationLevel ;
50+ public IsolationLevel IsolationLevel => _transaction . IsolationLevel ;
5351
5452 #endregion
5553
@@ -79,32 +77,30 @@ public void Commit()
7977 {
8078 CheckDisposed ( ) ;
8179 var originalState = Interlocked . CompareExchange ( ref _state , COMMIT , NONE ) ;
82- if ( originalState != NONE )
83- {
84- Debug . Assert ( false , $ "Trying commit with state: { originalState } ") ;
85- return ;
86- }
87- _transaction ! . Commit ( ) ;
80+ Debug . Assert ( originalState == NONE , $ "Trying commit with state: { originalState } ") ;
81+ Throw . InvalidOperationExceptionIf ( originalState != NONE , $ "Trying commit with state: { originalState } ") ;
82+ _transaction . Commit ( ) ;
8883 }
8984
9085 /// <summary>
9186 /// Disposes the resources used by this transaction.
9287 /// </summary>
9388 protected override void OnDispose ( )
9489 {
95- DisposeAndNull ( ref _transaction ) ;
96- if ( _onDispose != null )
90+ try
9791 {
98- _onDispose ( ) ;
99- _onDispose = null ;
92+ _transaction . Dispose ( ) ;
10093 }
94+ finally
95+ {
96+ Interlocked . Decrement ( ref _connection . TransactionCount ) ;
10197#if DEBUG
102- var elapsed = _watch . Elapsed ;
103- var timeout = _connection ? . DefaultTimeout ;
104- Debug . Assert ( elapsed . TotalSeconds < timeout , $ "Transaction elapsed: { elapsed } , Expected: { timeout } sec") ;
98+ var elapsed = _watch . Elapsed ;
99+ var timeout = _connection . DefaultTimeout ;
100+ Debug . Assert ( elapsed . TotalSeconds < timeout , $ "Transaction elapsed: { elapsed } , Expected: { timeout } sec") ;
105101#endif
106- _connection = null ;
107- base . OnDispose ( ) ;
102+ base . OnDispose ( ) ;
103+ }
108104 }
109105
110106 /// <summary>
@@ -114,15 +110,11 @@ public void Rollback()
114110 {
115111 CheckDisposed ( ) ;
116112 var originalState = Interlocked . CompareExchange ( ref _state , ROLLBACK , NONE ) ;
117- if ( originalState != NONE )
118- {
119- Debug . Assert ( false , $ "Trying rollback with state: { originalState } ") ;
120- return ;
121- }
122- _transaction ! . Rollback ( ) ;
113+ Debug . Assert ( originalState == NONE , $ "Trying rollback with state: { originalState } ") ;
114+ Throw . InvalidOperationExceptionIf ( originalState != NONE , $ "Trying rollback with state: { originalState } ") ;
115+ _transaction . Rollback ( ) ;
123116 }
124117
125118 #endregion
126-
127119 }
128120}
0 commit comments