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

    Howto Get SessionFactory from JNDI

    Stephen Byrne Newbie

      I have the following in my components.xml:

      <persistence:managed-hibernate-session name="myDatabase" auto-create="true"

      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
          .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
          Guillaume Jeudy Master

          try java:/myDatabase

          Source code in


          seems to hint at that.

             public void create(Component component)
                this.componentName = component.getName();
                if (persistenceUnitJndiName==null)
                   persistenceUnitJndiName = "java:/" + 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" />