-->
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.  [ 3 posts ] 
Author Message
 Post subject: ThreadLocalSessionContext getCurrentSession JTA error
PostPosted: Thu Apr 20, 2006 2:05 am 
Newbie

Joined: Thu Dec 01, 2005 11:27 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.1.3
Jboss 4.0.3SP1

Mapping documents:
Code:
<server>
    <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Lawcodes">
        <attribute name="DatasourceName">java:/LawcodesDS</attribute>
        <attribute name="Dialect">org.hibernate.dialect.PostgreSQLDialect</attribute>
        <attribute name="SessionFactoryName">java:/hibernate/Lawcodes</attribute>
        <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
        <attribute name="ShowSqlEnabled">true</attribute>
      <property name="hibernate.cache.use_second_level_cache">true</property>
      <property name="hibernate.jdbc.use_scrollable_resultset">true</property>
      <property name="hibernate.cglib.use_reflection_optimizer">true</property>    
      <property name="hibernate.jdbc.batch_size">50</property>       
      <property name="current_session_context_class">thread</property> 
      <property name="hibernate.transaction.auto_close_session">false</property>
      <property name="hibernate.transaction.flush_before_completion">false</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>
        <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>         
    </mbean>
</server>



Code between sessionFactory.openSession() and session.close():

Taken from the Caveatemptor Examples
Code:
public class HibernateThreadFilter implements Filter {

    private static Log log = LogFactory.getLog(HibernateThreadFilter.class);

    private SessionFactory sf;

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {
       
       try {
            log.debug("Starting a database transaction");
            sf.getCurrentSession().beginTransaction();

            // Call the next filter (continue request processing)
            chain.doFilter(request, response);

            // Commit and cleanup
            log.debug("Committing the database transaction");
            sf.getCurrentSession().getTransaction().commit();

        } catch (StaleObjectStateException staleEx) {
            log.error("This interceptor does not implement optimistic concurrency control!");
            log.error("Your application will not work until you add compensation actions!");
            // Rollback, close everything, possibly compensate for any permanent changes
            // during the conversation, and finally restart business conversation. Maybe
            // give the user of the application a chance to merge some of his work with
            // fresh data... what you do here depends on your applications design.
            throw staleEx;
        } catch (Throwable ex) {
            // Rollback only
            ex.printStackTrace();
            try {
                if (sf.getCurrentSession().getTransaction().isActive()) {
                    log.debug("Trying to rollback database transaction after exception");
                    sf.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable rbEx) {
                log.error("Could not rollback transaction after exception!", rbEx);
            }

            // Let others handle it... maybe another interceptor for exceptions?
            throw new ServletException(ex);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("Initializing filter, obtaining Hibernate SessionFactory from HibernateUtil");
        sf = HibernateUtil.getSessionFactory();
    }

    public void destroy() {}

}



Full stack trace of any exception that occurs:


Code:

15:43:24,697 INFO  [STDOUT] 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:508)
   at judcom.lawcodes.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:71)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
   at java.lang.Thread.run(Thread.java:595)
15:43:24,698 ERROR [HibernateThreadFilter] Could not rollback transaction after exception!
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:508)
   at judcom.lawcodes.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:92)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
   at java.lang.Thread.run(Thread.java:595)
15:43:24,699 ERROR [[lawcodes]] Servlet.service() for servlet lawcodes threw exception
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:508)
   at judcom.lawcodes.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:71)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
   at java.lang.Thread.run(Thread.java:595)




Trail of datasource loading:
Though this may be of some use
Code:
15:42:45,255 INFO  [NamingHelper] JNDI InitialContext properties:{}
15:42:45,255 INFO  [DatasourceConnectionProvider] Using datasource: java:/LawcodesUserDS
15:42:45,255 INFO  [SettingsFactory] RDBMS: PostgreSQL, version: 7.4.8
15:42:45,255 INFO  [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 7.4.7 JDBC3 with SSL (build 215)
15:42:45,256 INFO  [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
15:42:45,256 INFO  [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
15:42:45,256 INFO  [NamingHelper] JNDI InitialContext properties:{}
15:42:45,256 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
15:42:45,256 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
15:42:45,256 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
15:42:45,256 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
15:42:45,257 INFO  [SettingsFactory] Automatic flush during beforeCompletion(): enabled
15:42:45,257 INFO  [SettingsFactory] Automatic session close at end of transaction: enabled
15:42:45,257 INFO  [SettingsFactory] JDBC batch size: 15
15:42:45,257 INFO  [SettingsFactory] JDBC batch updates for versioned data: disabled
15:42:45,257 INFO  [SettingsFactory] Scrollable result sets: enabled
15:42:45,257 INFO  [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
15:42:45,257 INFO  [SettingsFactory] Connection release mode: after_statement
15:42:45,257 INFO  [SettingsFactory] Default batch fetch size: 1
15:42:45,257 INFO  [SettingsFactory] Generate SQL with comments: disabled
15:42:45,257 INFO  [SettingsFactory] Order SQL updates by primary key: disabled
15:42:45,257 INFO  [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
15:42:45,257 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
15:42:45,257 INFO  [SettingsFactory] Query language substitutions: {}
15:42:45,257 INFO  [SettingsFactory] Second-level cache: enabled
15:42:45,257 INFO  [SettingsFactory] Query cache: disabled
15:42:45,257 INFO  [SettingsFactory] Cache provider: org.hibernate.cache.HashtableCacheProvider
15:42:45,257 INFO  [SettingsFactory] Optimize cache for minimal puts: disabled
15:42:45,257 INFO  [SettingsFactory] Structured second-level cache entries: disabled
15:42:45,257 INFO  [SettingsFactory] Echoing all SQL to stdout
15:42:45,258 INFO  [SettingsFactory] Statistics: disabled
15:42:45,258 INFO  [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
15:42:45,258 INFO  [SettingsFactory] Default entity-mode: pojo
15:42:45,265 INFO  [SessionFactoryImpl] building session factory
15:42:47,017 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
15:42:47,017 INFO  [NamingHelper] JNDI InitialContext properties:{}
15:42:47,033 INFO  [Hibernate] SessionFactory successfully built and bound into JNDI [java:/hibernate/LawcodesUser]




Name and version of the database you are using:
Postgres 7.4


I have been reading the documentation on Hibernate 3.1 way of doing the request-per-session and the request-per-conversation.
http://hibernate.org/42.html
http://hibernate.org/43.html

I have been banging my head against a brick wall and getting the same error.
From reading the documentation about Transaction demarcation with JTA I read this
Quote:
Hibernate can automatically bind the "current" Session to the current JTA transaction.
Believing that the call to sf.getCurrentSession().beginTransaction() would obtain a transaction for the session. It appears it does not do this and I continually get the following error about "Unable to locate current JTA transaction".

I believe I may need to get the transaction and "begin" it myself, is this correct? If so could someone point some light on how it would be done and if not could some point in the right direction?[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:42 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
You define a thread SessionContext, but you actually get a JTASessionContext??

For using JTA, write Filter similar to this interceptor.

Code:
public class TransactionInterceptor implements Interceptor {

    private TransactionManager tm;

    public Object invoke(final Invocation invocation) {
        Transaction oldTx = null;
        try {
            oldTx = Transactions.applyBefore(Transactions.TYPE_REQUIRED, getTransactionManager());
            return invocation.invokeNext();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Transactions.applyAfter(Transactions.TYPE_REQUIRED, getTransactionManager(), oldTx);
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }

    private TransactionManager getTransactionManager() throws NamingException {
        if (tm == null) {
            Context ctx = new InitialContext();
            try {
                tm = (TransactionManager)ctx.lookup("java:/TransactionManager");
            } finally {
                ctx.close();
            }
        }
        return tm;
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 6:59 pm 
Newbie

Joined: Thu Dec 01, 2005 11:27 pm
Posts: 4
Thanks for the help.

Quote:
You define a thread SessionContext, but you actually get a JTASessionContext??


Yeah I though that was a little wierd, I keep changing my hibernate-service.xml file, but changes to :
transaction.factory_class
current_session_context_class
hibernate.cache.use_second_level_cache
and a few others have no effect when my .har file is deployed.

I am a little confused with the TransactionFilter. I am actually trying to do session-per-conversation for my application.

From the looks of the document that transactionfilter can only be used for session-per-request, is that correct?
Or can I use the transactionfilter with a modifed HibernateThreadFilter to accomplish session-per-converstaion?
If not any suggestions how I can do session-per-request?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.