11 Replies Latest reply on Nov 25, 2005 5:02 AM by muhviehstarr

    JBoss bug ou EJB bug???

    lsalves

      I've be seding this question for forums and JBoss/EJB3 jira and I have no answer until now. I realy need to know if this problem is a bug ou a normal behavior of EJB.

      I'm using JBoss 4.0.3SP1 + EJB3 RC3 + MyFaces.

      In a JSF managed bean, my application begin a JTA transaction and invoke a stateless session bean that return a entity bean. The entity bean has a lazy collection that throws org.hibernate.LazyInitializationException when I try to get it at managed bean method.

      Follow the managed bean method:

      public String consultar() throws Exception {
       ServiceLocator locator = ServiceLocator.getInstance();
       UserTransaction tx = (UserTransaction) locator.lookup( "java:comp/UserTransaction" );
       tx.begin();
      
       try {
       AplicacaoFacade facade = locator.lookupEJB( AplicacaoFacade.class );
      
       // Get informations for page
       vo = (UsuarioVO) paginaRegistros.getRowData();
       Usuario usuario = facade.getUsuarioPorId( vo.getId() );
      
       // Build organizations collection
       List<Long> organizacoesId = new ArrayList<Long>();
      
       // THROWS LAZY EXCEPTION !!!!!!
       for ( Organizacao organizacao : usuario.getOrganizacoes() ) {
       organizacoesId.add( organizacao.getId() );
       }
      
       tx.commit();
       }
       catch ( Exception e ) {
       tx.rollback();
       }
      
       return Constantes.OUTCOME_SUCESSO;
       }


      The session façade uses dependency injection to get entity manager:

      @Stateless
      public class AplicacaoFacadeBean implements AplicacaoFacade {
      
       @PersistenceContext EntityManager entityManager;
      
       public Usuario getUsuarioPorId( Long usuarioId ) {
       return entityManager.find( Usuario.class, usuarioId );
       }


      Shouldn't entity manager be open until JTA commit ou rollback?

        • 1. Re: JBoss bug ou EJB bug???
          martinganserer

          Hi,

          I know it helps you not really further but in my app I do it in the the same way you do it in your example. But it works in my app. And I must say that I use many lazy associations.
          For getting the stub of my facades I am using the inital context.
          Example:

          Facade facade = (Facade) ctx.lookup(Facade.class.getName());
          

          Maybe you try this?

          I had the same problem with JBoss 4.0.3SP1. Are you really sure that you are using SP1?



          • 2. Re: JBoss bug ou EJB bug???
            lsalves

            The ServiceLocator.lookupEJB( Class ) uses InitialContext like your example.

            I'm sure about JBoss 4.0.3SP1. What version do you use? Did you use the installer package or do some modification/upgrade?

            • 3. Re: JBoss bug ou EJB bug???
              martinganserer

              Hi,

              I use JBoss 4.0.3SP1. I used the installer package and didn't make any mods or something like that.

              Maybe you can try another approach:
              Lookup for the facade outside of the transaction. I am not sure but maybe it helps!

              • 4. Re: JBoss bug ou EJB bug???
                lsalves

                Thaks for your help, but still doesn't work.

                Are you sure you are using lazy collections outside EJB? In a Action Struts or JSF Managed Bean?

                Are EJB's stateless or stateful? Are you using depency injection to get Entity Manager? Is entity manager extended?

                • 5. Re: JBoss bug ou EJB bug???
                  martinganserer

                  Hello,

                  I don't use Action Struts and I don't work with JSF Managed Beans either.
                  I simply work with servlets and JSPs. But I don't think that this really matters. In my application I only use stateless session beans as Data Access Objects and I use dependency injection all the time for my entity manager. Therefore my entity manager doesn't use the extended persistent context.
                  If it doesn't work with JSF or any other framework i would test my app within a simple servlet.

                  • 6. Re: JBoss bug ou EJB bug???
                    lsalves

                    I change my code to a Servlet and still not working. Was just a test... really make no sense work in a Servlet and doesn't work in a JSF Managed Bean.

                    Are there any special configuration in your persistence.xml?
                    Could you show me the content of your persistence.xml properties tag? Specialy hibernate.transaction.factory_class and hibernate.transaction.manager_lookup_class.

                    • 7. Re: JBoss bug ou EJB bug???
                      patrick_ibg

                      I did notice that you looked up UserTransaction from the service locator... I'm not sure that will work.

                      I think maybe the problem is that the transaction you are getting is not the same as the transaction that is used by AplicacaoFacade.

                      If you want to use JSF with container managed transactions, try coding your managed bean as an EJB (look into JBoss SEAM). Or implement AplicacaoFacade as a POJO...

                      • 8. Re: JBoss bug ou EJB bug???
                        martinganserer

                        Hello,

                        I made some test to figure out if lazy loading really works with UserTransactions!

                        And now I can definitly say that it works in my app. The facade I use is a stateless session bean that uses an injected entity manager. In my servlet I use the local interface. So from my point of view it is not a Jboss bug!
                        It might be really the problem that the the transaction you are creating is not the same the entity manager uses. But I am really not sure about this. Taking a look at your code tells me that everything should be OK!

                        There is one thing that we don't have covered yet! The entity beans!!
                        Please create a stateless session bean to test if the lazy loading works there. If not the problem is the entity bean mapping and has nothing to do with the UserTransaction!

                        What do you think about that?

                        • 9. Re: JBoss bug ou EJB bug???
                          lsalves

                          Thanks Martin and Patrick!!!

                          It's working!!! The secret is that Martin is using a Local interface, while I was using a Remote interface for session façade.

                          • 10. Re: JBoss bug ou EJB bug???
                            martinganserer

                            Hello,

                            that's great! I am happy that it works!

                            • 11. Re: JBoss bug ou EJB bug???
                              muhviehstarr

                              Thank you for this useful informations!!