Announcement Announcement Module
No announcement yet.
Problems with Transactions and New threads Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with Transactions and New threads


    I'm having difficulty with transactions and new threads which throw exceptions relating to class not found for Hibernate's HqlToken so I'm wondering if I'm missing something with transactions and new threads (timers/synchronization objects against transactions).

    I currently have the following scenario:

    ServiceA - @Transactional
    ServiceB - @Transactional

    ServiceA is called by an Session Bean and inherits a weblogic transaction. A synchronization object is registered to this transaction. When the transaction COMMITS, ServiceB is called to do its work.

    ServiceB creates a JDK Timer with a spring managed TimerTask.

    Now the problem I'm having seems to be with ServiceB creating a new Transaction that does not seem to inherit properties for Hibernate classes? (My understanding of this isn't too deep). When the Transaction for ServiceA commits, the synchronization object registered to it kicks off a new thread which kicks off SerivceB. And when The timer is created in ServiceB it seems to be missing these hibernate related properties.

    The timer/timerTask works fine if i call ServiceB directly from ServiceA (therefore serviceB inherits the transaction from serviceA and hence when the Timer is created in serviceB it has the properties or what not to kick off the hibernate job (doing a select statement against the DB)).

    Am i missing something obvious here?


  • #2
    If you kick off a new thread, then the transactional state which is stored in thread local won't be found and hence you'll be starting from scratch.
    Last edited by karldmoore; Aug 27th, 2007, 02:52 PM.


    • #3
      Yeps I don't mind kicking off a new thread with a new transaction but that thread/transaction seem to be missing something as it throws ClassNotFoundException on org.hibernate.hql.ast.HqlToken when I try to run a query. Am I missing something in relation to why this thread doesn't have access to this library even tho it seems like its loaded into the jvm (according to -verbose option)


      • #4
        Hmm, a new thread should not have problems accessing the same classes except some awkward class loader stuff is happening - which is quite likely in a jee environment. Is it allowed to create your own thread after all? Does setting the thread context class loader help?



        • #5
          what would i set the thread context class loader to? Not too familiar with it but i did find info on weblogic + hibernate with weblogic defining its own ANTLR (see but I'm not too sure why the new thread isn't loading the correct hibernate ANTLR jar as this all works in the rest of the system.


          • #6
            Originally posted by jimmy.h View Post
            what would i set the thread context class loader to?
            Hmm, I can only tell you the obvious. It might be highly container-specific though.

            Thread myThread = new Thread();
            I wonder if that's not the default anyway. So no idea if it helps.

            Also repeating the question: Is it allowed to create your own thread after all?



            • #7
              Hmm I'm not too sure what you mean if its allowed to create your own thread?

              A new thread is created and that was expected for the Timer Task and the Synchronization object if that's what you mean?


              • #8
                Originally posted by jimmy.h View Post
                Hmm I'm not too sure what you mean if its allowed to create your own thread?
                I have never worked in such a managed environment of a full-blown j(2)ee server. I only remember reading about most different issues with creating your own threads in such an environment.



                • #9
                  ClassNotFoundException: org.hibernate.hql.ast.HqlToken

                  Hi frnds,

                  i am getting following exception when i tried to execute the example.

                  ClassNotFoundException: org.hibernate.hql.ast.HqlToken

                  here is the DAOImpl java class ,application-hibernte.xml and files.

                  can anyone help regarding this problem...

                  ## Oracle

                  hibernate.dialect org.hibernate.dialect.OracleDialect
                  #hibernate.dialect org.hibernate.dialect.Oracle9Dialect
                  hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
                  hibernate.connection.username username1
                  hibernate.connection.password username1
                  hibernate.connection.url jdbc:oracle:thin:@
                  #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE


                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">


                  <!-- ========================= RESOURCE DEFINITIONS ========================= -->

                  <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
                  <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
                  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
                  <property name="location"><value>/WEB-INF/classes/</value></property>

                  <!-- Local DataSource that works in any environment -->
                  <!-- Note that DriverManagerDataSource does not pool; it is not intended for production -->

                  <bean id="dataSource"
                  class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
                  <property name="driverClassName">
                  <property name="url">
                  <property name="username">
                  <property name="password">

                  <!-- JNDI DataSource for J2EE environments -->

                  <!-- Hibernate SessionFactory -->
                  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
                  <property name="dataSource"><ref local="dataSource"/></property>
                  <property name="mappingResources">
                  <property name="hibernateProperties">
                  <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                  <prop key="hibernate.show_sql">true</prop>

                  <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
                  <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
                  <property name="sessionFactory"><ref local="sessionFactory"/></property>

                  <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

                  Data access object: Hibernate implementation.

                  <bean id="HibernateSpringDaoTarget" class="com.crimsonlogic.dao.SpringHibernateDAOImpl ">
                  <property name="sessionFactory"><ref local="sessionFactory"/></property>

                  - Transactional proxy for Login Application central data access object.
                  - Defines specific transaction attributes with "readOnly" markers,
                  - which is an optimization that is particularly valuable with Hibernate
                  - (to suppress unnecessary flush attempts for read-only operations).
                  - Note that in a real-life app with multiple transaction proxies,
                  - you will probably want to use parent and child bean definitions
                  - as described in the manual, to reduce duplication.

                  <bean id="SpringHibernateDAO" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
                  <property name="transactionManager"><ref local="transactionManager"/></property>
                  <property name="target"><ref local="HibernateSpringDaoTarget"/></property>
                  <property name="transactionAttributes">
                  <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                  <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                  <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                  <prop key="store*">PROPAGATION_REQUIRED</prop>

                  ************************************************** ********

                  ******************DAOImpl class***************************

                  import java.util.*;
                  import org.springframework.dao.DataAccessException;
                  import teDaoSupport;
                  import org.hibernate.criterion.*;
                  import org.hibernate.*;
                  import org.hibernate.cfg.*;
                  import org.hibernate.hql.ast.HqlToken;

                  public class SpringHibernateDAOImpl extends HibernateDaoSupport implements SpringHibernateDAO {

                  public ArrayList getUsersList()throws DataAccessException{
                  ArrayList list = new ArrayList();
                  Session session = null;
                  Configuration cfg = new Configuration().addClass(LogonForm.class);
                  SessionFactory factory = cfg.buildSessionFactory();
                  session =factory.openSession();
                  System.out.println("In DAOImpl Class");
                  List query = session.createQuery("from EMPDETAILS").list();
                  System.out.println("The Query is in DAOImpl "+query);
                  Iterator itr = query.iterator();
                  while (itr.hasNext())
                  System.out.println("Inside while loop in DAOImpl");
                  LogonForm e = (LogonForm);
                  catch(Exception e)

                  { }
                  return list;