@@ -37,8 +37,16 @@ UPDATE HistoryItems SET
3737 ShortenedURL = @ShortenedURL,
3838 Hidden = @Hidden
3939 WHERE Id = @Id;" ;
40- using var connection = new SqliteConnection ( Connection . ConnectionString ) ;
41- connection . Open ( ) ;
40+ SqliteConnection connection = Connection ;
41+ bool isMemoryDb = Connection . ConnectionString . Contains ( ":memory:" , StringComparison . OrdinalIgnoreCase ) ||
42+ Connection . ConnectionString . Contains ( "Mode=Memory" , StringComparison . OrdinalIgnoreCase ) ;
43+ if ( ! isMemoryDb )
44+ {
45+ // For each Save operation, we create a new connection for concurrency. SQLite handles the connections.
46+ var concurrentConnection = new SqliteConnection ( Connection . ConnectionString ) ;
47+ concurrentConnection . Open ( ) ;
48+ connection = concurrentConnection ;
49+ }
4250
4351 using var transaction = connection . BeginTransaction ( ) ;
4452 try
@@ -58,6 +66,10 @@ UPDATE HistoryItems SET
5866 transaction . Rollback ( ) ;
5967 return false ;
6068 }
69+ finally
70+ {
71+ if ( ! isMemoryDb ) connection . Close ( ) ;
72+ }
6173 }
6274
6375 [ DapperAot ]
@@ -97,9 +109,16 @@ UPDATE HistoryItems
97109 [ DapperAot ]
98110 protected override bool Append ( string ? filePath , IEnumerable < HistoryItem > historyItems )
99111 {
100- // For each Append operation, we create a new connection for concurrency. SQLite handles the connections.
101- using var connection = new SqliteConnection ( Connection . ConnectionString ) ;
102- connection . Open ( ) ;
112+ SqliteConnection connection = Connection ;
113+ bool isMemoryDb = Connection . ConnectionString . Contains ( ":memory:" , StringComparison . OrdinalIgnoreCase ) ||
114+ Connection . ConnectionString . Contains ( "Mode=Memory" , StringComparison . OrdinalIgnoreCase ) ;
115+ if ( ! isMemoryDb )
116+ {
117+ // For each Append operation, we create a new connection for concurrency. SQLite handles the connections.
118+ var concurrentConnection = new SqliteConnection ( Connection . ConnectionString ) ;
119+ concurrentConnection . Open ( ) ;
120+ connection = concurrentConnection ;
121+ }
103122 using var tx = connection . BeginTransaction ( ) ;
104123 try
105124 {
@@ -194,6 +213,14 @@ INSERT INTO Tags
194213 tx . Rollback ( ) ;
195214 return false ;
196215 }
216+ finally
217+ {
218+ if ( ! isMemoryDb )
219+ {
220+ connection ? . Dispose ( ) ;
221+ }
222+
223+ }
197224 }
198225
199226}
0 commit comments