yetanalytics / lrsql

A SQL-based Learning Record Store
https://www.sqllrs.com
Apache License 2.0
89 stars 18 forks source link

Fix in-memory SQLite bug where DB is deleted upon connection close #350

Closed kelvinqian00 closed 9 months ago

kelvinqian00 commented 9 months ago

When a HikariCP (or any JDBC connection) is closed, the entire database is deleted, as explained in this article:

The thing is, JDBC does not track DB connections by default. Every time you call for (jdbc/*) function, you create a new connection, perform an operation and close it. For such persistent data storages like Postgres or MySQL that’ fine although not effective (in our project, we use HikariCP to have a pool of open connections).

But for in-memory SQLite database, closing a connection to it leads to wiping the data completely out form the RAM. So you need to track the connection in more precision way. You will create a connection by yourself and close it when the work is done.

In HikariCP, a connection is open for the duration of the maxLifetime property, after which it is closed and removed, thus deleting the SQLite in-memory database. This PR fixes it by setting maxLifetime to infinity (denoted by 0) specifically for SQLite in-memory mode.