I tried unit test my database code with an in-memory database but somehow this does not work:
I prepared a snippet to show this:
import org.springframework.jdbc.core.JdbcTemplate;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;
import java.io.IOException;
import java.sql.SQLException;
public class TestMain {
public static void main(String[] args) throws SQLException, IOException {
//When this variable is set to a filename
//like "test.db" everything works
String file = ":memory:";
SQLiteConfig config = new SQLiteConfig();
SQLiteDataSource dataSource = new SQLiteDataSource(config);
dataSource.setUrl("jdbc:sqlite:" + file);
JdbcTemplate temp = new JdbcTemplate(dataSource);
temp.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)");
temp.update("INSERT INTO test (id) VALUES (1), (2)");
temp.query("SELECT * FROM test", (rs, rowNum) -> {
System.out.println(rs);
return null;
});
}
}
This code throws the following exception:
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [INSERT INTO test (id) VALUES (1), (2)]; SQL state [null]; error code [0]; no such table: test; nested exception is java.sql.SQLException: no such table: test
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:415)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:534)
at ch.tiim.sco.TestMain.main(TestMain.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.sql.SQLException: no such table: test
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:523)
at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:520)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
... 7 more
But only when the database is set to in memory mode. Is this a bug in spring? It can't be a bug in SQLite or in the xerial SQLite driver because when I use JDBC directly everything works.
Every pointer and hint is appreciated, thank you.
Aucun commentaire:
Enregistrer un commentaire