These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: getCurrentSession and Jboss/JTA
PostPosted: Tue Dec 04, 2007 12:50 pm 
Newbie

Joined: Wed Jun 20, 2007 8:22 pm
Posts: 2
I am trying to have Jboss (4.2.2) and hibernate work together with JTA. I've poured through the hibernate and jboss wikis/forums/ref docs and I've seen this exception mentioned in some threads, but never resolved. I feel like I am just missing a simple setting or step. Any help would be appreciated. I deploy my hibernate classes via a .har file.

I have 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:
Quote:
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.
Code:
<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 with jboss):
Code:
<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:
Code:
<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:
Quote:
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 2:18 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

I'm also using JBoss integrated with Hibernate. The manner I'm using the session opening and transaction start/commit/rollback is delegated to an EJB Session Stateless. So, the session is open and closed and transaction is committed by the method on the EJB. In order to have that:
1. You don't need to have an hibernate.cfg.xml xml but define an hibernate-service.xml to your har (this is the setup for the hibernate MBean). i.e.
Code:
<server>
  <mbean code="org.jboss.hibernate.jmx.Hibernate" name="Company:Service=Persistence">

    <!-- #========================================================# -->
    <!-- #= Bind the Hibernate service to JNDI                   =# -->
    <!-- #========================================================# -->
    <attribute name="SessionFactoryName">java:/ch/persistence/hibernate/SessionFactory</attribute>
   
    <!-- #========================================================# -->
    <!-- #= Datasource setting                                   =# -->
    <!-- #========================================================# -->
    <attribute name="Dialect">@database.dialect@</attribute>                    <!-- hibernate.dialect                       -->
    <attribute name="DatasourceName">@database.datasource@</attribute>          <!-- hibernate.connection.datasource         -->
    <attribute name="Username">@database.user.name@</attribute>                 <!-- hibernate.connection.username           -->
    <attribute name="Password">@database.user.password@</attribute>             <!-- hibernate.connection.password           -->

    <!-- #========================================================# -->
    <!-- #= JDBC Settings. For now, set nothing, only when       =# -->
    <!-- #========================================================# -->      
    <attribute name="JdbcBatchSize"></attribute>                                         <!-- hibernate.jdbc.batch_size               -->
    <attribute name="JdbcFetchSize"></attribute>                                         <!-- hibernate.jdbc.fetch_size               -->
    <attribute name="JdbcScrollableResultSetEnabled"></attribute>                       <!-- hibernate.jdbc.use_scrollable_resultset --> 

    <!-- #========================================================# -->
    <!-- #= JDBC Settings. For now, set nothing, only when       =# -->
    <!-- #========================================================# -->      
    <attribute name="MaxFetchDepth"></attribute>                                         <!-- hibernate.max_fetch_depth               -->
   
    <!-- #========================================================# -->
    <!-- #= Caching setup. Nothing for now                       =# -->
    <!-- #========================================================# -->      
    <attribute name="QueryCacheEnabled">false</attribute>                                <!-- hibernate.cache.use_query_cache         -->
    <attribute name="CacheProviderClass"></attribute>                                    <!-- hibernate.cache.provider_class          -->
    <attribute name="CacheRegionPrefix"></attribute>                                     <!-- hibernate.cache.region_prefix           -->

    <!-- #========================================================# -->
    <!-- #= Caching setup. Just specify the class implementation =# -->
    <!-- #========================================================# -->      
    <attribute name="ShowSqlEnabled">false</attribute>                                   <!-- hibernate.show_sql                      -->
    <attribute name="StatGenerationEnabled">false</attribute>                            <!-- hibernate.generate_statistics           -->
  </mbean>
</server>

2. Then, I create an EJB Stateless:
Code:
@Stateless
public class AccountServiceBean implements AccountService {
  /**
   * {@inheritDoc}
   */
  public List<Account> findAll() {
    return DaoFactory.DEFAULT.getAccountDao().findAll();
  }

When you connect to the EJB and call the find all method, hibernate session is opened and transaction started.
3. The hibernateutil java file is then simple as this:
Code:
  private static final String HIBERNATE_SESSION_FACTORY = "java:/ch/persistence/hibernate/SessionFactory";
  private static final Logger LOG                       = Logger.getLogger(HibernateUtil.class);

  public static Session getSession() {
    try {
      InitialContext context = new InitialContext();
      return ((SessionFactory) context.lookup(HIBERNATE_SESSION_FACTORY)).getCurrentSession();
    } catch (HibernateException he) {
      throw new RuntimeException("Configuration problem : " + he.getMessage(), he);
    } catch (NamingException ne) {
      throw new RuntimeException("Naming problem : " + ne.getMessage(), ne);
    }
  }

4. You can find more documentation here: http://docs.jboss.org/jbossas/jboss4guide/r5/html/ch13.html#ch13.mbean.sect
Read the all chapter....

Hope this will put you on track.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.