我正在使用Spring JDBC模板来查询SQL Server数据库。我有一个计划任务配置为每周执行一次,下面是实现:
@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驱动程序)如果在配置的时间内空闲,则关闭连接。看看'query'方法的实现,似乎它没有创建新的连接。在这种情况下,我是否需要使用其他方法(例如execute
)?
答案 0 :(得分:1)
在dispatcher-servlet.xml中添加以下行:
i = j = m = 1
或者,如果使用注释,请添加:
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo use</string>
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use</string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) microphone use</string>
<key>NSLocationUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) always uses location </string>
<key>NSCalendarsUsageDescription</key>
<string>$(PRODUCT_NAME) calendar events</string>
<key>NSRemindersUsageDescription</key>
<string>$(PRODUCT_NAME) reminder use</string>
<key>NSContactsUsageDescription</key>
<string>$(PRODUCT_NAME) contact use</string>
<key>NSMotionUsageDescription</key>
<string>$(PRODUCT_NAME) motion use</string>
<key>NSHealthUpdateUsageDescription</key>
<string>$(PRODUCT_NAME) heath update use</string>
<key>NSHealthShareUsageDescription</key>
<string>$(PRODUCT_NAME) heath share use</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>$(PRODUCT_NAME) Bluetooth Peripheral use</string>
<key>NSAppleMusicUsageDescription</key>
<string>$(PRODUCT_NAME) media library use</string>
<key>NSSiriUsageDescription</key>
<string>$(PRODUCT_NAME) siri use</string>
<key>NSHomeKitUsageDescription</key>
<string>$(PRODUCT_NAME) home kit use</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>$(PRODUCT_NAME) speech use</string>
<key>NSVideoSubscriberAccountUsageDescription</key>
<string>$(PRODUCT_NAME) tvProvider use</string>
或者,您可以在资源中添加server.xml(如果是tomcat)<context:mbean-export registration="ignoreExisting" />
:
@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)
答案 1 :(得分:0)
关闭连接的不是Spring,而是关闭它的数据库,如果在一段时间内处于非活动状态。或者可能是由于网络故障。
任何连接池都可以在将连接提供给请求它的数据源之前测试连接。通常,它是通过测试查询完成的。
因此,请检查连接池是否正确配置。