https://kotlinlang.org logo
#exposed
Title
# exposed
s

Sourabh Rawat

01/16/2022, 3:04 PM
Why does this fail?
Copy code
@Test
    fun dbTest2() {
        Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
        transaction {
            SchemaUtils.create(Blogs)
        }
        transaction {
            Blog.all().also { println(it.toList()) }
            Blog.new {
                title = "asd"
                rawContent = "foo"
            }
            Blog.all().also { println(it.toList()) }
        }
    }
with
Table "BLOGS" not found (this database is empty)
even though I can see that create table call executed before
Copy code
2022-01-16 20:32:57.273 [Test worker] DEBUG Exposed - CREATE TABLE IF NOT EXISTS BLOGS (ID INT AUTO_INCREMENT PRIMARY KEY, TITLE VARCHAR(100) NOT NULL, RAW_CONTENT TEXT NOT NULL, CREATE_DATE_TIME DATETIME(9) NOT NULL, UPDATE_DATE_TIME DATETIME(9) NOT NULL)
2022-01-16 20:32:57.289 [Test worker] DEBUG Exposed - SELECT BLOGS.ID, BLOGS.TITLE, BLOGS.RAW_CONTENT, BLOGS.CREATE_DATE_TIME, BLOGS.UPDATE_DATE_TIME FROM BLOGS
solved 1
Found the problem. By default, an in-memory database in H2 is discarded after the connection closes. So modifying db connection URL to
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;
works
6 Views