JDBC 模板是否缓存连接?

Does JDBC template cache connection?

我有一个 java-spring 网络应用程序,它将从用户上传的 SQLite 数据库读取、写入和删除信息。我正在使用 JDBCtemplate 来设置连接、查询数据库和更新信息。

我在测试期间观察到一种行为:

每次用户上传一个新的 SQLite 数据库文件(它将与旧数据库文件同名,放在同一目录),如果他们不上传 reboot/restart tomcat , jdbcquery 会报告 db was corrupted 异常。

对我来说,这看起来像是 JDBCtemplate 以某种方式缓存了连接并试图恢复与旧数据库的连接?

如果是这样,您是否知道在不重新启动应用程序的情况下刷新连接?

final SingleConnectionDataSource dataSource = new singleConnectionDataSource();
try {
    Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
        throw new applicationException(MessageTemplateNames.GENERAL_UNKNOWN_ERROR, e);
}

createDirectoryForDbIfNotExists();
dataSource.setUrl(String.format("%s%s", JDBC.PREFIX, getDbFileLocation()));
dataSource.setAutoCommit(true);

JDBCTemplate 不处理 connection.It 从为其设置的数据源获取连接。

来自 SingleConnectionDataSource

的参考文档

Implementation of SmartDataSource that wraps a single JDBC Connection which is not closed after use. .....

This is primarily intended for testing. For example, it enables easy testing outside an application server, for code that expects to work on a DataSource. In contrast to DriverManagerDataSource, it reuses the same Connection all the time, avoiding excessive creation of physical Connections.

A DriverManagerDataSource 将满足您在不重启的情况下获得新连接的要求

Simple implementation of the standard JDBC DataSource interface, configuring the plain old JDBC DriverManager via bean properties, and returning a new Connection from every getConnection call.

更新

我的回答可能无法解决您原来的问题,只会回答新连接的问题,无需重启。请仔细阅读@Thilo 的评论。