7 Replies Latest reply on Oct 25, 2004 10:01 AM by Dave Smith

    Jboss 3.2.6 JNDI access from hibernate

    beyarecords Novice

      Hi,
      I have just migrated from Apache/Tomact to Apache/Jboss 3.2.6 and now need to change my class files to take advantage of JNDI. I have declared my jndi:

      [SessionFactoryObjectFactory] Factory name: java:/HibernateFactory

      What I am not to clear about is how to now access a transaction via jndi from java code? This is how my classes currently access a transaction:

      session = HibernateUtil.currentSession();
      transaction = session.beginTransaction();

      but could I now acces a transaction like so?:

      Context ctx = new InitialContext();
      UserTransaction ut = (UserTransaction)ctx.lookup("java:/HibernateFactory");
      ut.begin();
      ut.commit();

      How would I close the session? ut.closeSession()?

      many thanks

        • 1. Re: Jboss 3.2.6 JNDI access from hibernate
          Dave Smith Newbie

          use ..

          org.jboss.hibernate.session.HibernateContext.getSession("java:/hibernate/SessionFactory");
          Or what ever yopu have defined for SessionFactoryName in your hibernate mbean.

          This will return a session. If you are within an ejb your session becomes part of the ejb transaction. When the ejb transaction is commited so will your hibernate session.

          Otherwiise use the session as per normal..


          • 2. Re: Jboss 3.2.6 JNDI access from hibernate
            beyarecords Novice

            Hi rimmeraj,
            I have ammend my code as follow but nothing is happening and I am not getting any errors!:

            User user = null;
            Transaction transaction = null;
            Session session = null;

            try {
            session = org.jboss.hibernate.session.HibernateContext.getSession("java:/HibernateFactory");

            transaction = session.beginTransaction();

            //Get User details
            Query query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
            .setString("username",username)
            .setString("password",password);
            user = (User) query.uniqueResult();

            transaction.commit();
            session.closeSession();

            } catch (HibernateException e) {

            throw new RuntimeException("Exception in Hibernate:: " + e.getMessage(), e);
            }

            What am I doing wrong here?

            many thanks


            Uzo

            • 3. Re: Jboss 3.2.6 JNDI access from hibernate
              Dave Smith Newbie

              what does your hibernate-service.xml services file look like?

              • 4. Re: Jboss 3.2.6 JNDI access from hibernate
                beyarecords Novice

                rimmerraj,
                my hibernate-service.xml is as follows:


                jboss.jca:service=LocalTxCM,name=PostgreSQL

                java:/HibernateFactory
                java:/PostgreSQL
                net.sf.hibernate.dialect.PostgreSQLDialect
                net.sf.ehcache.hibernate.Provider
                update
                x
                y
                25
                true


                Woud it be possible to see the complete code of one of your *.class files between the transaction begin and the clossing the session so as I can gain a greater understanding of how it all fits together, or alternatively if you know of a resource that explains what you have explained to me?


                many thanks


                Andrew

                • 5. Re: Jboss 3.2.6 JNDI access from hibernate
                  Dave Smith Newbie

                  I always use hibernate within an EJB transaction so I don;t have to open/close ect ..


                  Is hibernate starting correctly? When I start my app I see something like this on my console. Maybee your test is throwing a NPE?

                  15:56:59,035 INFO [Environment] Hibernate 2.1.5
                  15:56:59,091 INFO [Environment] hibernate.properties not found
                  15:57:00,388 INFO [Environment] using CGLIB reflection optimizer
                  15:57:00,439 INFO [Configuration] Searching for mapping documents in jar: util-db.jar
                  15:57:00,452 INFO [Configuration] Found mapping documents in jar: com/candata/util/db/EventLog.hbm.xml
                  15:57:01,320 INFO [Binder] Mapping class: com.candata.util.db.EventLog -> event_log
                  15:57:02,099 INFO [Configuration] Found mapping documents in jar: com/candata/util/db/ImportSequence.hbm.xml
                  15:57:02,314 INFO [Binder] Mapping class: com.candata.util.db.ImportSequence -> import_sequences
                  15:57:02,339 INFO [Configuration] Found mapping documents in jar: com/candata/util/db/Notes.hbm.xml
                  15:57:02,535 INFO [Binder] Mapping class: com.candata.util.db.Notes -> notes
                  15:57:02,549 INFO [Configuration] Found mapping documents in jar: com/candata/util/db/SystemProperty.hbm.xml
                  15:57:02,762 INFO [Binder] Mapping class: com.candata.util.db.SystemProperty -> system_properties
                  15:57:02,777 INFO [Configuration] Found mapping documents in jar: com/candata/util/db/UserProperty.hbm.xml
                  15:57:02,970 INFO [Binder] Mapping class: com.candata.util.db.UserProperty -> user_properties
                  15:57:02,985 INFO [Configuration] processing one-to-many association mappings
                  15:57:02,987 INFO [Configuration] processing one-to-one association property references
                  15:57:03,018 INFO [Configuration] processing foreign key constraints
                  15:57:03,221 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
                  15:57:03,278 INFO [SettingsFactory] Use outer join fetching: true
                  15:57:03,335 INFO [NamingHelper] JNDI InitialContext properties:{}
                  15:57:03,355 INFO [DatasourceConnectionProvider] Using datasource: java:/DefaultDS
                  15:57:03,363 INFO [TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
                  15:57:03,389 INFO [NamingHelper] JNDI InitialContext properties:{}
                  15:57:03,397 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                  15:57:03,412 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                  15:57:03,424 INFO [NamingHelper] JNDI InitialContext properties:{}
                  15:57:03,427 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                  15:57:03,446 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                  15:57:03,582 INFO [SettingsFactory] Use scrollable result sets: true
                  15:57:03,598 INFO [SettingsFactory] Use JDBC3 getGeneratedKeys(): false
                  15:57:03,600 INFO [SettingsFactory] Optimize cache for minimal puts: false
                  15:57:03,611 INFO [SettingsFactory] Query language substitutions: {}
                  15:57:03,621 INFO [SettingsFactory] cache provider: net.sf.hibernate.cache.HashtableCacheProvider
                  15:57:03,640 INFO [Configuration] instantiating and configuring caches
                  15:57:04,986 INFO [SessionFactoryImpl] building session factory
                  15:57:08,831 INFO [SessionFactoryObjectFactory] Factory name: java:/hibernate/SessionFactory
                  15:57:08,843 INFO [NamingHelper] JNDI InitialContext properties:{}
                  15:57:08,847 INFO [NamingHelper] Creating subcontext: hibernate
                  15:57:08,870 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/hibernate/SessionFactory
                  15:57:08,902 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
                  15:57:08,909 INFO [NamingHelper] JNDI InitialContext properties:{}

                  • 6. Re: Jboss 3.2.6 JNDI access from hibernate
                    beyarecords Novice

                    Hi rimmeraj,

                    yes Hibernate starts up properly. My log output is:

                    21:24:01,184 INFO [Environment] Hibernate 2.1.5
                    21:24:01,194 INFO [Environment] hibernate.properties not found
                    21:24:01,199 INFO [Environment] using CGLIB reflection optimizer
                    21:24:01,223 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/Artist.hbm.xml
                    21:24:01,408 INFO [Binder] Mapping class: test.Artist -> artisttbl
                    21:24:01,629 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/ArtistImages.hbm.xml
                    21:24:01,659 INFO [Binder] Mapping class: test.ArtistImages -> artistimagestbl
                    21:24:01,682 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/Order.hbm.xml
                    21:24:01,715 INFO [Binder] Mapping class: test.Order -> orderstbl
                    21:24:01,755 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/OrderItem.hbm.xml
                    21:24:01,774 INFO [Binder] Mapping class: test.OrderItem -> orderitemstbl
                    21:24:01,776 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/Stock.hbm.xml
                    21:24:01,823 INFO [Binder] Mapping class: test.Stock -> stocktbl
                    21:24:01,832 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/StockImages.hbm.xml
                    21:24:01,865 INFO [Binder] Mapping class: test.StockImages -> stockimagestbl
                    21:24:01,869 INFO [Configuration] Mapping file: /Library/jboss-3.2.6/server/default/deploy/hibernate.har/test/User.hbm.xml
                    21:24:01,910 INFO [Binder] Mapping class: test.User -> usertbl
                    21:24:01,917 INFO [Binder] Mapping collection: test.User.ArtistChoice -> choicetbl
                    21:24:01,918 INFO [Configuration] processing one-to-many association mappings
                    21:24:01,919 INFO [Binder] Mapping collection: test.Order.OrderItems -> orderitemstbl
                    21:24:01,921 INFO [Configuration] processing one-to-one association property references
                    21:24:01,922 INFO [Configuration] processing foreign key constraints
                    21:24:01,976 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
                    21:24:01,981 INFO [SettingsFactory] Use outer join fetching: true
                    21:24:01,998 INFO [NamingHelper] JNDI InitialContext properties:{}
                    21:24:02,004 INFO [DatasourceConnectionProvider] Using datasource: java:/PostgreSQL
                    21:24:02,009 INFO [TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
                    21:24:02,010 INFO [NamingHelper] JNDI InitialContext properties:{}
                    21:24:02,014 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                    21:24:02,015 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                    21:24:02,016 INFO [NamingHelper] JNDI InitialContext properties:{}
                    21:24:02,017 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                    21:24:02,017 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                    21:24:02,057 INFO [SettingsFactory] Use scrollable result sets: true
                    21:24:02,059 INFO [SettingsFactory] Use JDBC3 getGeneratedKeys(): false
                    21:24:02,060 INFO [SettingsFactory] Optimize cache for minimal puts: false
                    21:24:02,060 INFO [SettingsFactory] echoing all SQL to stdout
                    21:24:02,060 INFO [SettingsFactory] Query language substitutions: {}
                    21:24:02,060 INFO [SettingsFactory] cache provider: net.sf.ehcache.hibernate.Provider
                    21:24:02,077 INFO [Configuration] instantiating and configuring caches
                    21:24:02,137 WARN [Configurator] No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/Library/jboss-3.2.6/server/default/deploy/jbossweb-tomcat50.sar/cocoon.war/WEB-INF/lib/ehcache-0.8.jar!/ehcache-failsafe.xml
                    21:24:02,190 WARN [Plugin] Could not find configuration for test.Order.OrderItems. Configuring using the defaultCache settings.
                    21:24:02,241 WARN [Plugin] Could not find configuration for test.ArtistImages. Configuring using the defaultCache settings.
                    21:24:02,244 WARN [Plugin] Could not find configuration for test.Order. Configuring using the defaultCache settings.
                    21:24:02,539 INFO [SessionFactoryImpl] building session factory
                    21:24:03,069 INFO [ReflectHelper] reflection optimizer disabled for: test.OrderItem, NullPointerException: null
                    21:24:03,194 INFO [SessionFactoryObjectFactory] Factory name: java:/HibernateFactory
                    21:24:03,195 INFO [NamingHelper] JNDI InitialContext properties:{}
                    21:24:03,197 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/HibernateFactory
                    21:24:03,198 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
                    21:24:03,205 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
                    21:24:03,206 INFO [NamingHelper] JNDI InitialContext properties:{}
                    21:24:03,207 INFO [DatasourceConnectionProvider] Using datasource: java:/PostgreSQL
                    21:24:03,207 INFO [SchemaUpdate] Running hbm2ddl schema update
                    21:24:03,208 INFO [SchemaUpdate] fetching database metadata
                    21:24:03,212 INFO [SchemaUpdate] updating schema
                    21:24:03,213 INFO [Configuration] processing one-to-many association mappings
                    21:24:03,213 INFO [Configuration] processing one-to-one association property references
                    21:24:03,213 INFO [Configuration] processing foreign key constraints

                    I have since made some adjustments to my class:

                    User user = null;
                    Transaction transaction = null;
                    //HibernateUtil Util = null;
                    HibernateContext hibContext = new HibernateContext();
                    Session session = hibContext.getSession("java:/HibernateFactory");

                    try {
                    //Util = new HibernateUtil();
                    //session = Util.currentSession();

                    transaction = session.beginTransaction();

                    //Get User details
                    Query query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
                    .setString("username",username)
                    .setString("password",password);
                    user = (User) query.uniqueResult();

                    transaction.commit();
                    //Util.closeSession();

                    } catch (HibernateException e) {

                    throw new RuntimeException("Exception in Hibernate:: " + e.getMessage(), e);
                    }

                    and the output i get in the logs is:

                    2004-10-22 21:28:43,017 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: HibernateFactory
                    2004-10-22 21:28:43,017 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=d1e22501ffc24f5f00ffc24f636d0000
                    2004-10-22 21:28:43,018 DEBUG [net.sf.hibernate.impl.SessionImpl] opened session
                    2004-10-22 21:28:43,134 INFO [org.apache.jk.server.JkCoyoteHandler] Response already commited
                    2004-10-22 21:28:43,155 INFO [org.apache.jk.server.JkCoyoteHandler] Response already commited
                    2004-10-22 21:28:43,185 INFO [org.apache.jk.server.JkCoyoteHandler] Response already commited
                    2004-10-22 21:28:43,748 INFO [org.apache.jk.server.JkCoyoteHandler] Response already commited
                    2004-10-22 21:28:43,783 INFO [org.apache.jk.server.JkCoyoteHandler] Response already commited

                    can you show me how to use hibernate within an EJB transaction? Is it more efficient to do it that way?

                    many thanks


                    Andrew

                    • 7. Re: Jboss 3.2.6 JNDI access from hibernate
                      Dave Smith Newbie

                      After looking at code here the problem is that HibernateFactory returns a factory not a session

                      It sould look like ..


                      InitialContext ctx = new InitialContext();
                      SessionFactory fac = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
                      Session ses = fac.openSession();

                      Do what ever here

                      ses.close();