4 Replies Latest reply on Oct 2, 2008 10:56 AM by tomek szzzaaos

    EntityManager in Destroy method

    tomek szzzaaos Newbie

      Hi, Could you please tell me what's wrong with my code, I'm getting entityManager = null in method below?
      Or simply in @PreDestroy/@Destroy method it is always null?




      @Name("org.jboss.seam.security.identity")
      @Scope(value=ScopeType.SESSION)
      @Install(precedence = Install.APPLICATION)
      @BypassInterceptors
      @Startup
      public class User extends RuleBasedIdentity{
      
              ...
      
              @PreDestroy
              public void preDestroy() {
                      log.info("Predestroy: " + getUsername());
                      Contact contact = ((Contact)Component.getInstance(Contact.class));              
                      contact.setOnline(false);
                      if(entityManager!=null) {
                              entityManager.merge(contact);
                              entityManager.flush();
                      }else {
                              System.err.println("------------ entityManager = null -------------");
                      }                       
              }
      
              ...
      
      


        • 1. Re: EntityManager in Destroy method
          System Administrator Expert

          Are you injecting entityManager instance?  are you using @PersistenceContext or @In (SMPC)?


          If you are using SMPC, check components.xml to make sure the instance name matches exactly what you have in components.xml:



          <persistence:managed-persistence-context name="entityManager"
                                                         auto-create="true"
                                                              persistence-unit-jndi-name="java:/foobarFactory"/>



          So in this case, in your SFSB:


          @In  //inject the SHIMS persistence unit/context (SMPC)
          private EntityManager entityManager;  



          • 2. Re: EntityManager in Destroy method
            Ben Catherall Newbie

            How are you accessing entityManager?


            You have @BypassInterceptors - so you'd need to use Component.getInstance(...)

            • 3. Re: EntityManager in Destroy method
              tomek szzzaaos Newbie

              Thanks for help I forgot to delete @BypassInterceptors, so now it looks like:


              @Name("org.jboss.seam.security.identity")
              @Scope(value=ScopeType.SESSION)
              @Install(precedence = Install.APPLICATION)
              @Startup
              public class User extends RuleBasedIdentity {
              
                      @In
                   private EntityManager entityManager;
              
              
                   @PreDestroy
                   public void preDestroy() {
                        log.info("Predestroy: " + getUsername());
                        Contact contact = ((Contact)Component.getInstance(Contact.class));          
                        contact.setOnline(false);
                        
                              //why I have to have this here???
                        FullTextHibernateSessionProxy ep = ((FullTextHibernateSessionProxy)entityManager.getDelegate());
                        ep.merge(contact);
                        ep.flush();               
                        
                   }



              but if I do:


              entityManager.merge(contact);
              entityManager.flush();
              



              [Component] Exception calling JavaBean @PreDestroy method: org.jboss.seam.security.identity
              javax.persistence.TransactionRequiredException: no transaction is in progress
                   at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
                   at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:90)
                   at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.flush(FullTextEntityManagerImpl.java:102)
                   at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:90)
                   at com.ylem.model.notstoreable.User.preDestroy(User.java:85)
              



              Btw Im using SFSB, defined as below in componets.xml


                   <component name="org.jboss.seam.ui.EntityConverter">
                        <property name="entityManager">#{entityManager}</property>
                   </component>
              
                   <persistence:managed-persistence-context name="entityManager"
                        auto-create="true"
                        persistence-unit-jndi-name="java:/webshopEntityManagerFactory" />



              Thats odd for me, any help much appreciated!

              • 4. Re: EntityManager in Destroy method
                tomek szzzaaos Newbie

                Solved by @Transactional annotation:


                        @Transactional
                     @PreDestroy
                     public void preDestroy() {
                          if (getUsername() != null) {
                               log.info("Logged out: " + getUsername());
                               Contact contact = ((Contact) Component.getInstance(Contact.class));
                               if (contact != null) {
                                    contact.setOnline(false);
                
                                    entityManager.merge(contact);
                                    entityManager.flush();
                               }
                          }
                     
                          
                     }