1 Reply Latest reply on Aug 13, 2008 3:33 PM by gjeudy

    Howto Get SessionFactory from JNDI

    sbyrne.sb.ezftz.com

      I have the following in my components.xml:


      <persistence:managed-hibernate-session name="myDatabase" auto-create="true"
      session-factory-jndi-name="myDatabase"/>



      In CONVERSATION scoped components I can inject Session myDatabase;
      I would like to, from an APPLICATION scoped component, get a Session.
      I do it like this:


      String jndiName = "/myDatabase";
      SessionFactory factory = (SessionFactory) org.jboss.seam.util.Naming
          .getInitialContext()
          .lookup( jndiName );
      Session myDatabase = factory.openSession();



      Make sure to close the Session when you are done with it.

        • 1. Re: Howto Get SessionFactory from JNDI
          gjeudy

          try java:/myDatabase


          Source code in

          org.jboss.seam.persistence.ManagedPersistenceContext


          seems to hint at that.


          @Create
             public void create(Component component)
             {
                this.componentName = component.getName();
                if (persistenceUnitJndiName==null)
                {
                   persistenceUnitJndiName = "java:/" + componentName;
                }
                
                PersistenceContexts.instance().touch(componentName);      
             }



          If for some reasons this fails you can use hibernate feature to publish sessionfactory in global jndi namespace by adding the following to your persistence.xml.


          <property name="hibernate.session_factory_name" value="java:/mySessionFactory" />