title: java.lang.IllegalStateException: No value for key [org.hibernate.impl.SessionFactoryImpl] with AOP
body: <p>I'm having an error each time I update an entity using Hibernate.
  Here's my story:</p>
  
  <p>I have a service with an annotation @Transactional and is calling a DAO.update method.</p>
  
  <pre><code>@Service
  @Transactional
  public class FooServiceImpl implements FooService {
      @Inject
      private FooDAO fooDAO;
  
      @Override
      public Foo update(Foo foo) {
         return fooDAO.update(foo);
      }
  
      @Override
      public Foo find(int fooId) {
         return fooDAO.findById(fooId);
      }
  }
  </code></pre>
  
  <p>I have an AOP which catch the DAO.update in order to make some operations.</p>
  
  <pre><code>@Aspect
  @Order(2)
  @Component
  public class FooTackerAspect {
     @Inject
     private FooService fooService;
  
     @Inject
     private SessionFactoryManager sessionFactoryManager;
  
     @Pointcut("execution(* com.foo.bar.FooService.update(..))")
     public void methodToTrack() {}
  
     @Around("methodToTrack")
     public Object track(final ProceedingJoinPoint joinPoint) throws Throwable {
        int fooId = fetchIdFromJoinPoint(joinPoint);
        Foo foo = fooService.find(fooId);
  
        // Do some operations
  
        // I close the session in order to avoid the exception
        // org.hibernate.NonUniqueObjectException: a different object with
        // the same identifier value was already associated with the session
        sessionFactoryManager.getSessionFactory(DataSource.FOO)
            .getCurrentSession().close();
  
        Object retVal = joinPoint.proceed();
        return retVal;
     }
  }
  </code></pre>
  
  <p>However, after updating the entity (fooDAO.update(foo)), I get the following error:</p>
  
  <pre><code>2056953 [http-bio-8080-exec-4] ERROR o.s.t.s.TransactionSynchronizationUtils - TransactionSynchronization.beforeCompletion threw exception 
  java.lang.IllegalStateException: No value for key [org.hibernate.impl.SessionFactoryImpl@762d6271] bound to thread [http-bio-8080-exec-4]
      at org.springframework.transaction.support.TransactionSynchronizationManager.unbindResource(TransactionSynchronizationManager.java:209) ~[spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCompletion(SpringSessionSynchronization.java:193) ~[spring-orm-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCompletion(TransactionSynchronizationUtils.java:106) ~[spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCompletion(AbstractPlatformTransactionManager.java:938) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:739) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-3.2.4.RELEASE.jar:3.2.4.RELEASE]
      at com.sun.proxy.$Proxy112.update(Unknown Source) [$Proxy112.class:na]
  </code></pre>
  
  <p>This error is not blocking (the update still goes through). But I want to correct this error.
  Do you have any idea how I can do that?</p>
  
  <p>I think the error comes from the fact I closed the session. But I have to close it in order to update the entity.</p>