Skip to content

Can't create JDBC shared memory DB #4018

@silverhammermba

Description

@silverhammermba

SQLDelight Version

1.5.4

Application Operating System

Android

Describe the Bug

JDBC supports sqlite's in-memory DB with shared cache, you can see this in their tests where connections are made with URIs such as

  • jdbc:sqlite:file:memdb1?mode=memory&cache=shared
  • jdbc:sqlite:file::memory:?cache=shared

But sqldelight's JdbcSqliteDriver does not support such URIs. I can only get in-memory DBs to work using JdbcSqliteDriver.IN_MEMORY which is hard-coded to jdbc:sqlite:.

For example, I want to use this in my tests so that I can test separate classes accessing a common DB in memory as if it exists on disk:

var dbUrl = "jdbc:sqlite:file:inMemTestDb?mode=memory&cache=shared"

val driver1 = JdbcSqliteDriver(dbUrl)
MyDB.Schema.create(driver1) // crash is here
val db1 = MyDB(driver1)

// opens a separate connection to same DB as db1 without creating anything in the filesystem
val driver2 = JdbcSqliteDriver(dbUrl)
MyDB.Schema.create(driver2)
val db2 = MyDB(driver2)

This results in the stacktrace below.

The code only runs if I use JdbcSqliteDriver.IN_MEMORY, but that gives incorrect behavior because then db1 and db2 are completely separate in-memory databases.

Stacktrace

[SQLITE_ERROR] SQL error or missing database (no such table: main.hockeyPlayer)
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: main.hockeyPlayer)
	at app//org.sqlite.core.DB.newSQLException(DB.java:1012)
	at app//org.sqlite.core.DB.newSQLException(DB.java:1024)
	at app//org.sqlite.core.DB.throwex(DB.java:989)
	at app//org.sqlite.core.NativeDB.prepare_utf8(Native Method)
	at app//org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
	at app//org.sqlite.core.DB.prepare(DB.java:257)
	at app//org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:45)
	at app//org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
	at app//org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:25)
	at app//org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
	at app//org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
	at app//org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
	at app//com.squareup.sqldelight.sqlite.driver.JdbcDriver.execute(JdbcDriver.kt:109)
	at app//com.squareup.sqldelight.db.SqlDriver$DefaultImpls.execute$default(SqlDriver.kt:52)
	at app//com.foo.bar.MyDBImpl$Schema.create(MyDBImpl.kt:40)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions