While running junit using in memory database H2 I was hitting this error. I was testing my spring DAO classes that are using JDBCTemplate. In a single Junit testing, to test various scenario I was calling a single DAO method with different parameters . It start giving the error "The object is already closed" as it finishes testing the DAO method with first parameter.
Then I investigated in JDBCTemplate and found that it closes connection after every execution of query using Finally statement.
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
To solve this issue I override the JDBCTemplate and created my own JDBCTemplate in which I have removed the Finally statement.