How to solve org.hsqldb.HsqlException: statement is closed


Posted by Steven

A while ago, we fixed a really annoying defect in our code. Because we debugged for days without finding the reason for the defect, I want to share how we approached the problem and what the solution was. The breakthrough in this problem came when a colleague of mine reviewed our code and found a solution. So the first thing to take away here is: If you tried anything, ask a coworker to join you in the bug-hunt.

Our application has an HSQL database and uses Spring Data with Hibernate. Really early in the development, we encountered the following exception:

  1. org.springframework.dao.InvalidDataAccessResourceUsageException: could not update: [de.company.project.server.app.model.SomeBusinessClass#591067]; SQL [update SomeBusinessClass set creator=?, creationdate=?, lastChangeDate=?, lastChangeUser=?, OPT_LOCK=?, archiviert=?, name=? where ID=? and OPT_LOCK=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not update: [de.company.project.server.app.model.SomeBusinessClass#591067]
  2.  
  3. at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238)
  4. at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
  5. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
  6. at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
  7. at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
  8. at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
  9. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  10. at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:119)
  11. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  12. at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
  13. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  14. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
  15. at com.sun.proxy.$Proxy73.findAll(Unknown Source)
  16. at de.company.project.server.app.service.somepackage.SomeServiceImpl.someMethod(SomeServiceImpl.java:1012)
  17. at de.company.project.server.app.service.somepackage.SomeServiceImpl.someSaveMethod(SomeServiceImpl.java:440)
  18. at de.company.project.server.app.service.somepackage.SomeServiceImpl.someOtherSaveMethod(SomeServiceImpl.java:387)
  19. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  20. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  21. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  22. at java.lang.reflect.Method.invoke(Method.java:497)
  23. at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
  24. at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
  25. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
  26. at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
  27. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
  28. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  29. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  30. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
  31. at com.sun.proxy.$Proxy75.someOtherSaveMethod(Unknown Source)
  32. at de.company.project.server.app.repositories.BusinessClassTest.getDefaultMarktDto(BusinessClassTest.java:152)
  33. at de.company.project.server.app.repositories.BusinessClassTest.reproduceSQLException(BusinessClassTest.java:68)
  34. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  35. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  36. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  37. at java.lang.reflect.Method.invoke(Method.java:497)
  38. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  39. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  40. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  41. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  42. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  43. at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
  44. at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
  45. at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
  46. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  47. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
  48. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
  49. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  50. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  51. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  52. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  53. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  54. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  55. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  56. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  57. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
  58. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  59. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
  60. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
  61. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
  62. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  63. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  64. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  65. at java.lang.reflect.Method.invoke(Method.java:497)
  66. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  67. Caused by: org.hibernate.exception.SQLGrammarException: could not update: [de.company.project.server.app.model.SomeBusinessClass#591067]
  68. at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
  69. at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
  70. at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
  71. at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3303)
  72. at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
  73. at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
  74. at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:159)
  75. at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
  76. at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
  77. at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
  78. at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:67)
  79. at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1227)
  80. at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1293)
  81. at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
  82. at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
  83. at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
  84. at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
  85. at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:323)
  86. at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:68)
  87. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  88. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  89. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  90. at java.lang.reflect.Method.invoke(Method.java:497)
  91. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:475)
  92. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:460)
  93. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:432)
  94. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  95. at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
  96. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  97. at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
  98. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
  99. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  100. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  101. at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
  102. ... 58 more
  103. Caused by: java.sql.SQLException: statement is closed
  104. at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
  105. at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
  106. at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
  107. at org.hsqldb.jdbc.JDBCStatementBase.checkClosed(Unknown Source)
  108. at org.hsqldb.jdbc.JDBCPreparedStatement.checkSetParameterIndex(Unknown Source)
  109. at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
  110. at org.hsqldb.jdbc.JDBCPreparedStatement.setString(Unknown Source)
  111. at org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:132)
  112. at org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:132)
  113. at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:63)
  114. at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
  115. at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
  116. at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
  117. at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
  118. at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
  119. at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3248)
  120. ... 88 more
  121. Caused by: org.hsqldb.HsqlException: statement is closed
  122. at org.hsqldb.error.Error.error(Unknown Source)
  123. at org.hsqldb.error.Error.error(Unknown Source)
  124. ... 102 more

This exception was not reproducible and occurred from time to time and we didn't pay much attention to it. Further developing our application, we encountered this exception more and more often. Obviously, it was related to the amount of database queries our application produced.

The very first thing we did was to write a JUnit test that recreated this behavior. Sadly, a simple test could not reproduce it every time, so we had to write a for-loop around that test to trigger the exception every time the test ran. But even debugging this test did not give us the clue we were looking for, because when debugging this test, it ran green without any issues. Maybe because of the debugging process, there was enough time to do something which under normal circumstances was unsuccessful.

After having our code reviewed by a coworker, he found an interesting setting in our data source configuration. Can you spot it?

  1. @Bean
  2. BasicDataSource dataSource() {
  3. BasicDataSource ds = new BasicDataSource();
  4. ds.setDriverClassName("org.hsqldb.jdbcDriver");
  5. ds.setUrl("jdbc:hsqldb:file:db/testdb;shutdown=true");
  6. ds.setUsername("SA");
  7. ds.setPassword("");
  8. ds.setInitialSize(1);
  9. ds.setMaxActive(5);
  10. ds.setPoolPreparedStatements(true);
  11. ds.setMaxOpenPreparedStatements(10);
  12. return ds;
  13. }

The solution was to delete line 11. The number of max open statements was too small. Deleting that line makes the method default to -1 which means "no max statements".

Hope you can get around this issue.

TL;DR

Only use BasicDataSource.setMaxOpenPreparedStatements() if you know what it does. :)

Category: 
Share: