SQLite Forum

how to use in-memory with shared cache in mybatis
Login

how to use in-memory with shared cache in mybatis

(1) By live to learn (live_to_learn) on 2020-04-16 07:29:52

Hello team,

I've been recently trying to use in-memory with shared in mybatis. I've read the doc, and tried below configurations in my db.properties which would be read by mybatis initialization. However, it created a file name 

`:memory:?cache=shared`

instead. Apparently, it's not using in-memory db, is it?


Here is my configuration snippet

```
db.env=localdb

db.driver=org.sqlite.JDBC

db.url=jdbc:sqlite::memory:?cache=shared

db.username=

db.password=
```

Any suggestions would be appreciated!

Thanks,
Arthur

(2) By Gunter Hick (gunter_hick) on 2020-04-16 10:37:44 in reply to 1 [link]

See https://sqlite.org/inmemorydb.html

You need to specify the URI pathame "file::memory:?cache=shared" and hope it gets to the SQLite API without interference from mybatis or JDBC and provided that the release of the SQLite library used was compiled with URI support enabled.

(3) By live to learn (live_to_learn) on 2020-04-16 11:08:05 in reply to 2 [link]

Thanks Gunter. Here is the working one according to your suggestion.

```java
db.env=localdb
db.driver=org.sqlite.JDBC
db.url=jdbc:sqlite:file::memory:?cache=shared
db.username=
db.password=
```

And to double confirm, i checked the implementation of SQLite library used
```java
<dependency>
                <groupId>org.xerial</groupId>
                <artifactId>sqlite-jdbc</artifactId>
                <version>3.28.0</version>
            </dependency>
```
Here is the snippet. For my case, it seems the URI would be passed through to sqlite3_open function (or i guess so)

```java
public final synchronized void open(String file, int openFlags) throws SQLException {
        this._open(file, openFlags);
        this.closed.set(false);
        if (this.fileName.startsWith("file:") && !this.fileName.contains("cache=")) {
            this.shared_cache(this.config.isEnabledSharedCache());
        }

        this.enable_load_extension(this.config.isEnabledLoadExtension());
        this.busy_timeout(this.config.getBusyTimeout());
    }
```