Connection closed error while executing query with Spring JDBC Template
<p>I am using Spring JDBC template to query SQL Server db. I have a scheduled task configured to execute once every week, below is the implementation:</p>

<pre><code>@Autowired
private JdbcTemplate jdbcTemplate;

public void importData(){

    try{
        logger.debug("Importing Data");
        jdbcTemplate.query(...) // Fails
     catch(DataAccessException e){
        //Log the error
     }
}
</code></pre>

<p>I am getting the below exception when the task is executed (i.e. once every week):</p>

<pre><code>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.&lt;init&gt;(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)
</code></pre>

<p>Below is the driver class:</p>

<pre><code>spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
</code></pre>

<p>I guess Spring (or jdbc driver) closes the connection if it's idle for configured amount of time. Looking at the implementation of 'query' method, it seems it does not create a new connection. In this case, do I need to use a different method (e.g. <code>execute</code>)?</p>