Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 内存中的Spring Sqlite数据库损坏?_Java_Spring_Sqlite_Jdbc_In Memory Database - Fatal编程技术网

Java 内存中的Spring Sqlite数据库损坏?

Java 内存中的Spring Sqlite数据库损坏?,java,spring,sqlite,jdbc,in-memory-database,Java,Spring,Sqlite,Jdbc,In Memory Database,我尝试使用内存中的数据库对我的数据库代码进行单元测试,但不知何故,这不起作用: 我准备了一个片段来说明这一点: import org.springframework.jdbc.core.JdbcTemplate; import org.sqlite.SQLiteConfig; import org.sqlite.SQLiteDataSource; import java.io.IOException; import java.sql.SQLException; public class Te

我尝试使用内存中的数据库对我的数据库代码进行单元测试,但不知何故,这不起作用:

我准备了一个片段来说明这一点:

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;
        });
    }
}
此代码引发以下异常:

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
但仅当数据库设置为内存模式时。 这是春天的虫子吗?它不可能是SQLite或xerial SQLite驱动程序中的错误,因为当我直接使用JDBC时,一切都正常

感谢您的每一个指点和提示

编辑: 实际上,h2数据库也会发生同样的事情。
因此,我的end或spring的使用方式一定是个问题。

显然是在最后一个连接关闭后立即关闭SQLite和H2数据库。在H2中,这可以通过jdbc:H2:mem:test来修复;数据库关闭延迟=-1