0 Replies Latest reply on Dec 3, 2007 5:29 PM by stjanda

    hibernate getCurrentSession() with JTA

    stjanda Newbie

      I am trying to have Jboss (4.2.2) and hibernate work together with JTA. I deploy my hibernate classes via a .har file. I've seen this issue mentioned in some threads, but never resolved. I feel like I am just missing a simple setting or something. Any help would be appreciated.

      I have great success using the following:
      ctx = new InitialContext();
      factory = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
      Session hSess = factory.openSession();
      Transaction tx = hSess.beginTransaction();

      But I cannot get the following to work:
      Session hSess = factory.getCurrentSession();

      The error thrown is:
      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:544) at test.TestServlet.processRequest(TestServlet.java:57) at test.TestServlet.doGet(TestServlet.java:129) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157) at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:393) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446) at java.lang.Thread.run(Thread.java:595)

      Obvously no transaction is obtained from the TransactionManager, but why? I thought that was the point of the method call to deal with opening the session for you.

      I have the following properties setup.

      <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
       <depends>jboss:service=Naming</depends>
       <depends>jboss:service=TransactionManager</depends>
       <attribute name="DatasourceName">java:/MyDS</attribute>
       <attribute name="Dialect">org.hibernate.dialect.Oracle9Dialect</attribute>
       <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
       <attribute name="Password">xxx</attribute>
       <attribute name="Username">xxx</attribute>
       <attribute name="CacheProviderClass">org.hibernate.cache.NoCacheProvider</attribute>
       <attribute name="QueryCacheEnabled">false</attribute>
       <attribute name="MinimalPutsEnabled">false</attribute>
       <attribute name="MaxFetchDepth">3</attribute>
       <attribute name="ShowSqlEnabled">true</attribute>
       </mbean>
      
      hibernate.cfg.xml (i don't think this is actually needed):
      <hibernate-configuration>
       <session-factory>
       <property name="current_session_context_class">jta</property>
       <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
       </session-factory>
      </hibernate-configuration>
      
      datasource config:
      <xa-datasource>
       <jndi-name>MyDS</jndi-name>
       <track-connection-by-tx/>
       <isSameRM-override-value>false</isSameRM-override-value>
       <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
       <xa-datasource-property name="URL">jdbc:oracle:oci8:@db</xa-datasource-property>
       <!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
       <!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->
      
       <!-- Oracles XA datasource cannot reuse a connection outside a transaction once enlisted in a global transaction and vice-versa -->
       <no-tx-separate-pools/>
      
       <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
       <metadata>
       <type-mapping>Oracle9i</type-mapping>
       </metadata>
       </xa-datasource>
      

      Hibernate log after server starts reads ok by my interpretation:
      2007-12-03 16:44:41,580 INFO [org.hibernate.cfg.Environment] Hibernate 3.2.4.sp1
      2007-12-03 16:44:41,595 INFO [org.hibernate.cfg.Environment] hibernate.properties not found
      2007-12-03 16:44:41,595 INFO [org.hibernate.cfg.Environment] Bytecode provider name : javassist
      2007-12-03 16:44:41,611 INFO [org.hibernate.cfg.Environment] using JDK 1.4 java.sql.Timestamp handling
      2007-12-03 16:44:41,705 INFO [org.hibernate.cfg.Configuration] Searching for mapping documents in jar: my.har
      2007-12-03 16:44:41,720 INFO [org.hibernate.cfg.Configuration] Found mapping document in jar: beans/tn3270_inactivity.hbm.xml
      2007-12-03 16:44:41,751 DEBUG [org.hibernate.util.DTDEntityResolver] trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
      2007-12-03 16:44:41,751 DEBUG [org.hibernate.util.DTDEntityResolver] recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
      2007-12-03 16:44:41,767 DEBUG [org.hibernate.util.DTDEntityResolver] located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
      2007-12-03 16:44:42,017 INFO [org.hibernate.cfg.HbmBinder] Mapping class: beans.InactiveDeviceBean -> tn3270.inactivity
      2007-12-03 16:44:42,205 INFO [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
      2007-12-03 16:44:42,220 INFO [org.hibernate.connection.DatasourceConnectionProvider] Using datasource: java:/MyDS
      2007-12-03 16:44:42,611 DEBUG [org.hibernate.cfg.SettingsFactory] could not get database version from JDBC metadata
      2007-12-03 16:44:42,611 INFO [org.hibernate.cfg.SettingsFactory] RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
      With the Partitioning, OLAP and Oracle Data Mining options
      JServer Release 9.2.0.1.0 - Production
      2007-12-03 16:44:42,611 INFO [org.hibernate.cfg.SettingsFactory] JDBC driver: Oracle JDBC driver, version: 9.2.0.1.0
      2007-12-03 16:44:42,642 INFO [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.Oracle9Dialect
      2007-12-03 16:44:42,642 WARN [org.hibernate.dialect.Oracle9Dialect] The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
      2007-12-03 16:44:42,642 INFO [org.hibernate.transaction.TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
      2007-12-03 16:44:42,642 INFO [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
      2007-12-03 16:44:42,642 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
      2007-12-03 16:44:42,658 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
      2007-12-03 16:44:42,658 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
      2007-12-03 16:44:42,658 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Automatic flush during beforeCompletion(): enabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Automatic session close at end of transaction: enabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] JDBC batch size: 15
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] JDBC batch updates for versioned data: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Scrollable result sets: enabled
      2007-12-03 16:44:42,658 DEBUG [org.hibernate.cfg.SettingsFactory] Wrap result sets: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] JDBC3 getGeneratedKeys(): disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Connection release mode: after_statement
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Maximum outer join fetch depth: 3
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Default batch fetch size: 1
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Generate SQL with comments: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Order SQL updates by primary key: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Order SQL inserts for batching: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
      2007-12-03 16:44:42,658 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Query language substitutions: {}
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] JPA-QL strict compliance: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Second-level cache: enabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Query cache: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Cache provider: org.hibernate.cache.NoCacheProvider
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Optimize cache for minimal puts: disabled
      2007-12-03 16:44:42,658 INFO [org.hibernate.cfg.SettingsFactory] Structured second-level cache entries: disabled
      2007-12-03 16:44:42,673 INFO [org.hibernate.cfg.SettingsFactory] Echoing all SQL to stdout
      2007-12-03 16:44:42,673 INFO [org.hibernate.cfg.SettingsFactory] Statistics: disabled
      2007-12-03 16:44:42,673 INFO [org.hibernate.cfg.SettingsFactory] Deleted entity synthetic identifier rollback: disabled
      2007-12-03 16:44:42,673 INFO [org.hibernate.cfg.SettingsFactory] Default entity-mode: pojo
      2007-12-03 16:44:42,673 INFO [org.hibernate.cfg.SettingsFactory] Named query checking : enabled
      2007-12-03 16:44:42,720 INFO [org.hibernate.impl.SessionFactoryImpl] building session factory
      2007-12-03 16:44:42,720 DEBUG [org.hibernate.impl.SessionFactoryImpl] Session factory constructed with filter configurations : {}
      2007-12-03 16:44:42,720 DEBUG [org.hibernate.impl.SessionFactoryImpl] instantiating session factory with properties: {hibernate.cache.use_minimal_puts=false, hibernate.transaction.flush_before_completion=true, hibernate.transaction.auto_close_session=true, hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider, hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup, hibernate.dialect=org.hibernate.dialect.Oracle9Dialect, hibernate.cache.use_query_cache=false, hibernate.max_fetch_depth=3, hibernate.connection.agressive_release=true, hibernate.connection.username=xxx, hibernate.connection.datasource=java:/MyDS, hibernate.show_sql=true, hibernate.connection.password=antelope, hibernate.connection.release_mode=after_statement, hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory}
      2007-12-03 16:44:43,064 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] initializing class SessionFactoryObjectFactory
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryObjectFactory] registered: 8a1f66ba16a1f7d70116a1f7d9380000 (unnamed)
      2007-12-03 16:44:43,080 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryImpl] instantiated session factory
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryImpl] obtaining JTA TransactionManager
      2007-12-03 16:44:43,080 INFO [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryImpl] Checking 0 named HQL queries
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryImpl] Checking 0 named SQL queries
      2007-12-03 16:44:43,080 DEBUG [org.hibernate.impl.SessionFactoryImpl] Returning a Reference to the SessionFactory

      Any ideas?