title: How to deal with Background Threads in a Hibernate/Spring Application
body: <p>I am asking me how I should deal with background threads performing database-involving tasks in my Hibernate/Spring web application.</p>
  
  <p>Currently I am using the following interceptor, so I can annotate my thread run methods with @OpenSession and a session should be opened. This should also work for RMI requests for example or any other method that is called without a session being opened. However, I am not sure if the code is correct, I face the problem that sometimes the sessions are just not closed and kept open forever.</p>
  
  <pre><code>@Around("@annotation(openSession)")
      public Object processAround(ProceedingJoinPoint pjp, OpenSession openSession) throws Throwable {
  
          boolean boundResource = false;
          Session session = null;
  
          // Bind the session to the thread, if not already done
          if(TransactionSynchronizationManager.getResource(sessionFactory) == null) {
              log.debug("Opening Hibernate Session in method "+pjp.getSignature());
  
              session = SessionFactoryUtils.getSession(sessionFactory, true);
              TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
              boundResource = true;
          }
  
          // Invoke the annotated method
          Object ret;
          try {
              ret = pjp.proceed();
          }
          catch(Throwable t) {
              // Rethrows the Exception but makes sure the session will be closed
              SessionFactoryUtils.closeSession(session);
              log.debug("Closing Hibernate Session in method (Exception thrown) "+pjp.getSignature());
              throw t;
          }
  
          // If a resourc was bound by this method call, unbind it.
          if(boundResource) {
              //SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
              session.flush();
              SessionFactoryUtils.closeSession(session);
  
              log.debug("Closing Hibernate Session in method "+pjp.getSignature());
          }
  
          return ret;
      }
  </code></pre>