title: Running out of heap space when using hibernate in a Java EE application
body: <p>I am currently developing a Glassfish application. To allow the business EJBs to communicate with the MySQL back-end there is the following structure:</p>
  
  <p>An interface project<br>
  A domain project<br>
  A JPA/ORM project (Hibernate)  </p>
  
  <p>The interface project contains interfaces of the models and an IDomainFacadeBean. The domain project contains implementations of these interfaces which are mapped to the JPA classes. The JPA classes are generated by eclipse. The business EJB retrieve the DomainFacadeBean by a JNDI lookup.</p>
  
  <p>In order to make this work I had to set the loading of the autogenerated JPA classes to EAGER. This worked well on my test database, but when I try this on the production database the application crashes and the log shows a long exception that ends with: <code>Caused by: java.lang.OutOfMemoryError: Java heap space]]</code> (full exception at the bottom)</p>
  
  <p>I have found the following possible solutions so far and perhaps there is another solution.</p>
  
  <ul>
  <li>Disable hibernate logging (<a href="https://stackoverflow.com/questions/8487221/hibernate-java-lang-outofmemoryerror-java-heap-space">link</a>) - I don't think this is the problem because the stacktrace in the linked answer doesn't resemble the my application throws.</li>
  <li>Increase the JVM memory size - My colleague did already try this and set the memory size to 2GB. The same problem seemed to occur, which should not be, since the database is only about 200MB in size.</li>
  <li>Use LAZY instead of EAGER loading (<a href="http://howtodoinjava.com/hibernate/lazy-loading-in-hibernate/" rel="nofollow noreferrer">link</a>)- I think this is the solution, although I don't know how to make this work in the architecture I am currently working with. When I first tried this, hibernate threw <code>LazyInitializationException</code> and I had no data to work with. I know the key to avoiding this is to keep the session active while using the object, but that would mean that the business EJBs are responsible for data storage; something I would like to avoid.</li>
  </ul>
  
  <p>I would really appreciate it if someone could show me some pointers on what it is that is causing these exceptions.</p>
  
  <p>Full exception:  </p>
  
  <pre><code>javax.ejb.EJBException
      at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
      at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionNoTx(EJBContainerTransactionManager.java:638)
      at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionBeanMgTx(EJBContainerTransactionManager.java:597)
      at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:467)
      at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475)
      at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009)
      at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
      at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
      at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
      at com.sun.proxy.$Proxy326.getAllWlcs(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:239)
      at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
      at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:226)
      at nl.XXX.interfaces.domain.__IDomainFacadeBean_Remote_DynamicStub.getAllWlcs(nl/XXX/interfaces/domain/__IDomainFacadeBean_Remote_DynamicStub.java)
      at nl.XXX.interfaces.domain._IDomainFacadeBean_Wrapper.getAllWlcs(nl/XXX/interfaces/domain/_IDomainFacadeBean_Wrapper.java)
      at nl.XXX.operations.OperationsTimerBean.refreshProductionStatus(OperationsTimerBean.java:87)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
      at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
      at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
      at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
      at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
      at sun.reflect.GeneratedMethodAccessor123.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundTimeout(SystemInterceptorProxy.java:145)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
      at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
      at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
      at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:3993)
      at com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1199)
      at com.sun.ejb.containers.EJBTimerService.access$000(EJBTimerService.java:89)
      at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:1919)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to release JDBC Connection
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
      at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1407)
      at org.hibernate.Query.getResultList(Query.java:427)
      at nl.XXX.orm.repositories.Repository.getAll(Repository.java:119)
      at nl.XXX.orm.beans.OrmFacadeBean.getAllWlcs(OrmFacadeBean.java:68)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
      at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
      at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
      at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
      at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
      at sun.reflect.GeneratedMethodAccessor123.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
      at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
      at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
      at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
      ... 49 more
  Caused by: org.hibernate.exception.JDBCConnectionException: Unable to release JDBC Connection
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
      at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection(LogicalConnectionManagedImpl.java:192)
      at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.afterTransaction(LogicalConnectionManagedImpl.java:155)
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.afterTransaction(JdbcCoordinatorImpl.java:282)
      at org.hibernate.internal.SessionImpl.afterOperation(SessionImpl.java:540)
      at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1478)
      at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1426)
      at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1398)
      ... 81 more
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
      at com.mysql.jdbc.Util.getInstance(Util.java:408)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
      at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1246)
      at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1241)
      at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4801)
      at org.hibernate.engine.jdbc.connections.internal.PooledConnections.add(PooledConnections.java:68)
      at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.closeConnection(DriverManagerConnectionProviderImpl.java:195)
      at org.hibernate.internal.NonContextualJdbcConnectionAccess.releaseConnection(NonContextualJdbcConnectionAccess.java:46)
      at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection(LogicalConnectionManagedImpl.java:189)
      ... 87 more
  Caused by: java.lang.OutOfMemoryError: Java heap space
  ]]
  </code></pre>