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

  • Problems with Transactions and New threads

    Hi,

    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?

    Thanks

  • #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, 03:52 PM.

    Comment


    • #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)

      Comment


      • #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?

        Joerg

        Comment


        • #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 http://forum.hibernate.org/viewtopic...7b5b1aeccb74ea) 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.

          Comment


          • #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.

            Code:
            Thread myThread = new Thread();
            myThread.setContextClassLoader(Thread.currentThread.getContextClassLoader());
            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?

            Joerg

            Comment


            • #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?

              Comment


              • #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.

                Joerg

                Comment


                • #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 hibernate.properties files.

                  can anyone help regarding this problem...


                  *************hibernate.properties****************
                  ## 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:@12.18.120.158:1251:cgg5
                  #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE
                  **************************************************

                  ***************applicationcontext-hibernate.xml********************

                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

                  <beans>

                  <!-- ========================= 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/hibernate.properties</value></property>
                  </bean>

                  <!-- 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">
                  <value>${hibernate.connection.driver_class}</value>
                  </property>
                  <property name="url">
                  <value>${hibernate.connection.url}</value>
                  </property>
                  <property name="username">
                  <value>${hibernate.connection.username}</value>
                  </property>
                  <property name="password">
                  <value>${hibernate.connection.password}</value>
                  </property>
                  </bean>


                  <!-- 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">
                  <list>
                  <value>com/bean/LogonForm.hbm.xml</value>
                  </list>
                  </property>
                  <property name="hibernateProperties">
                  <props>
                  <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                  <prop key="hibernate.show_sql">true</prop>
                  </props>
                  </property>
                  </bean>


                  <!-- 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>
                  </bean>


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

                  <!--
                  Data access object: Hibernate implementation.
                  -->

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

                  <!--
                  - 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">
                  <props>
                  <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>
                  </props>
                  </property>
                  </bean>
                  </beans>



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


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

                  import java.util.*;
                  import org.springframework.dao.DataAccessException;
                  import org.springframework.orm.hibernate3.support.Hiberna 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;
                  try{
                  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)itr.next();
                  e.setEmpId(e.getEmpId());
                  e.setName(e.getName());
                  e.setDesignation(e.getDesignation());
                  e.setEmail(e.getEmail());
                  e.setJoin_date(e.getJoin_date());
                  e.setBday(e.getBday());
                  e.setPhnExtn(e.getPhnExtn());
                  list.add(e);
                  }
                  }
                  catch(Exception e)
                  {
                  System.out.println(e.getMessage());

                  }
                  finally
                  { }
                  return list;
                  }
                  }

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

                  Comment

                  Working...
                  X