@@ -11,19 +11,36 @@ public sealed class SQLiteDbContext: Disposable, IDbContext
1111 {
1212 private readonly SQLiteDbTransaction _transaction ;
1313 private readonly Lifetime _lifetime = new ( ) ;
14+ private readonly bool _checkAcquired ;
1415
1516 /// <summary>
1617 /// Initializes a new instance of the <see cref="SQLiteDbContext"/> class with the specified SQLite database connection.
1718 /// </summary>
1819 /// <param name="connection">The SQLite database connection.</param>
1920 /// <exception cref="ArgumentNullException">Thrown when the provided connection is null.</exception>
2021 public SQLiteDbContext ( SQLiteDbConnection connection )
22+ : this ( connection , true ) // Default behavior is to check acquisition state
23+ {
24+ }
25+
26+ /// <summary>
27+ /// Initializes a new instance of the <see cref="SQLiteDbContext"/> class with the specified SQLite database connection
28+ /// and a flag indicating whether to check if the connection is acquired.
29+ /// </summary>
30+ /// <param name="connection">The SQLite database connection.</param>
31+ /// <param name="checkAcquired">Whether to check if the connection is acquired to prevent "database is locked" exception.</param>
32+ /// <exception cref="ArgumentNullException">Thrown when the provided connection is null.</exception>
33+ public SQLiteDbContext ( SQLiteDbConnection connection , bool checkAcquired )
2134 {
2235 try
2336 {
2437 _lifetime . Add ( ( ) => GC . SuppressFinalize ( this ) ) ;
2538 Connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
26- CheckAcquired ( ) ;
39+ _checkAcquired = checkAcquired ;
40+ if ( checkAcquired )
41+ {
42+ CheckAcquired ( ) ;
43+ }
2744 Debug . Assert ( ! connection . InTransaction , $ "{ nameof ( connection ) } in transaction.") ;
2845 Throw . InvalidOperationExceptionIf ( connection . InTransaction , $ "{ nameof ( connection ) } in transaction.") ;
2946 Connection . Open ( out var originalState ) ;
@@ -70,7 +87,11 @@ private void CheckAcquired()
7087 /// </summary>
7188 public void Commit ( )
7289 {
73- CheckAcquired ( ) ;
90+ CheckDisposed ( ) ;
91+ if ( _checkAcquired )
92+ {
93+ CheckAcquired ( ) ;
94+ }
7495 _transaction . Commit ( ) ;
7596 }
7697
@@ -79,7 +100,10 @@ public void Commit()
79100 /// </summary>
80101 protected override void OnDispose ( )
81102 {
82- CheckAcquired ( ) ;
103+ if ( _checkAcquired )
104+ {
105+ CheckAcquired ( ) ;
106+ }
83107 _lifetime . Dispose ( ) ;
84108 base . OnDispose ( ) ;
85109 }
@@ -89,7 +113,11 @@ protected override void OnDispose()
89113 /// </summary>
90114 public void Rollback ( )
91115 {
92- CheckAcquired ( ) ;
116+ CheckDisposed ( ) ;
117+ if ( _checkAcquired )
118+ {
119+ CheckAcquired ( ) ;
120+ }
93121 _transaction . Rollback ( ) ;
94122 }
95123
0 commit comments