【发布时间】:2016-08-31 08:01:28
【问题描述】:
我正在使用 Spring JDBC 模板来查询 SQL Server db。我有一个计划任务配置为每周执行一次,下面是实现:
@Autowired
private JdbcTemplate jdbcTemplate;
public void importData(){
try{
logger.debug("Importing Data");
jdbcTemplate.query(...) // Fails
catch(DataAccessException e){
//Log the error
}
}
执行任务时(即每周一次),我收到以下异常:
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Invalid state, the Connection object is closed.
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:305)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:329)
at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:97)
at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787)
下面是驱动类:
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
我猜如果 Spring(或 jdbc 驱动程序)在配置的时间内空闲,它会关闭连接。查看“查询”方法的实现,似乎它没有创建新的连接。在这种情况下,我是否需要使用其他方法(例如execute)?
【问题讨论】:
-
你的连接池应该处理这个问题。你有一个连接池,对吧?
-
我正在使用带有 Jetty 的 Spring Boot,因此根据本文档 (docs.spring.io/spring-boot/docs/current/reference/html/…) 它应该使用 Hikari 连接池?
-
我相信只有在你的类路径中包含 Hikari 时。不确定它默认使用什么。
-
对了!我将删除jetty依赖并尝试使用tomcat使tomcat连接池可用,看看是否能解决问题。
-
更换一个 Web 服务器来改变 JDBC 连接池......听起来很明智。
标签: java sql-server spring spring-jdbc