3 Replies Latest reply on Mar 4, 2006 3:27 AM by al_kiev

    Error accesing Hibernate current session and UserModule

    xmaniac

      I need to access the UserModule and RoleModule. I have no problem getting them from a portlet using:

      <service>
       <service-name>UserModule</service-name>
       <service-class>org.jboss.portal.core.modules.UserModule</service-class>
       <service-ref>:service=Module,type=User</service-ref>
      </service>
      

      UserModule userModule = (UserModule) context.getAttribute("UserModule");
      

      Now I have to get a reference from a spring bean to handle some initialization code(same WAR as the portlets). I tried:
      userModule = (UserModule) new InitialContext().lookup(ModuleConstants.USERMODULE_JNDINAME);
      

      It works (well, it gets a reference) but whenever I try to use any function I get:
      2006-02-14 18:26:25,809 ERROR [org.jboss.portal.core.impl.role.RoleModuleImpl] Cannot count roles
      org.hibernate.HibernateException: Unable to locate current JTA transaction
       at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:61)
       at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:604)
       at org.jboss.portal.core.impl.role.RoleModuleImpl.getCurrentSession(RoleModuleImpl.java:382)
       at org.jboss.portal.core.impl.role.RoleModuleImpl.getRolesCount(RoleModuleImpl.java:293)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at ........
      

      I'm using JBOSS Portal 2.4Alpha (latest version from CVS). Everything in the jmx-console looks fine. Any ideas?

        • 1. Re: Error accesing Hibernate current session and UserModule

          I'm not accessing UserModule but I got the same exception in a portlet when I tried to use my own SessionFactory, so my solution might apply to your case. Try to edit jboss-portlet.xml to add a "transaction" element :

          <portlet-app>
           <portlet>
           <portlet-name>MyPortlet</portlet-name>
           <transaction>
           <trans-attribute>Required</trans-attribute>
           </transaction>
           </portlet>
          </portlet-app>
          


          • 2. Re: Error accesing Hibernate current session and UserModule
            xmaniac

            That works great with portlets. As a matter of fact, I was using it already.

            However my problem arises when accesing the portal context from a Spring bean (not referenced by a portlet) or a servlet.

            • 3. Re: Error accesing Hibernate current session and UserModule

              Had the same problem in the code that executed outside of standard TransactionInterceptor....
              the solution is to wrap your calls to transactions using the following technique:

              TransactionManager tm;
              
              
              try {
               tm = TransactionManagerProvider.JBOSS_PROVIDER.getTransactionManager();
              } catch (Exception e) {
               throw new RuntimeException(e);
              }
              
              Transaction oldTx = null;
              try {
               oldTx = Transactions.applyBefore(Transactions.TYPE_REQUIRED, tm);
              
              .... do something ....
              
              } catch (TransactionException e) {
               throw new RuntimeException(e);
              } finally {
               try {
               Transactions.applyAfter(Transactions.TYPE_REQUIRED, tm, oldTx);
               } catch (TransactionException e) {
               log.error("", e);
               }
              }