0 Replies Latest reply on Jun 7, 2013 3:40 AM by r4j

    Seam Managed Transactions + Spring + Quartz

    r4j

      I have a problem with making Seam Managed Transactions to work with Spring / Quartz. Whenever Quartz job is executed on a Spring-only defined Bean (with transactional propagation = REQUIRED) I get IllegalArgumentException from "SeamLifecycleUtils#beginTransactionalSeamCall" because there is no Transactional Synchronization active yet.

       

      I have done a bit of debugging and TransactionSynchronisation is initialized AFTER the "AbstractPlatformTransactionManager#doBegin" method is executed. On the other hand - doBegin requires an access to Session object, so we end up with a chicken-egg problem: Spring wants to get a Session in order to create transaction, Seam needs a transaction to return Session.

       

      My configuration of components is as follows:

       

      Spring:

       

      <bean id="springSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         
      <property name="dataSource" ref="dataSource" />
         
      <property name="namingStrategy">
             
      <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
         
      </property>
         
      <property name="configLocation" value="hibernate.cfg.xml" />
      </bean>

      <bean id="seamSessionFactory" class="org.jboss.seam.ioc.spring.SeamManagedSessionFactoryBean">
         
      <property name="sessionName" value="seamDatabase"/>
      </bean>

      <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       
      <property name="sessionFactory" ref="seamSessionFactory" />
      </bean>

       

      Seam:

       

      <core:init transaction-management-enabled="true" />

      <spring:spring-transaction platform-transaction-manager="#{txManager}"/>

      <persistence:managed-hibernate-session name="seamDatabase"
              
      auto-create="true" session-factory="#{springSessionFactory}" />

       

      Quartz executes a normal Spring bean and it all ends up with:

       

      Caused by: java.lang.IllegalStateException: Seam application context not available and cannot be started.  Seam Managed Persistence Context not available.  Try placing the spring bean call inside of a spring transaction or try making the spring bean a Seam Component using <seam:component/>.
      at org
      .jboss.seam.ioc.spring.SeamLifecycleUtils.beginTransactionalSeamCall(SeamLifecycleUtils.java:44) [jboss-seam-ioc.jar:]
      at org
      .jboss.seam.ioc.spring.SeamManagedSessionFactoryBean$SeamManagedSessionFactoryHandler.getSession(SeamManagedSessionFactoryBean.java:128) [jboss-seam-ioc.jar:]
      at org
      .jboss.seam.ioc.spring.SeamManagedSessionFactoryBean$SeamManagedSessionFactoryHandler.invoke(SeamManagedSessionFactoryBean.java:188) [jboss-seam-ioc.jar:]
      at com
      .sun.proxy.$Proxy85.openSession(Unknown Source)
      at sun
      .reflect.GeneratedMethodAccessor156.invoke(Unknown Source) [:1.7.0_15]
      at sun
      .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.7.0_15]
      at java
      .lang.reflect.Method.invoke(Method.java:601) [:1.7.0_15]
      at org
      .springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:191) [org.springframework.core-3.1.3.RELEASE.jar:]
      at org
      .springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:176) [org.springframework.core-3.1.3.RELEASE.jar:]
      at org
      .springframework.orm.hibernate4.SessionFactoryUtils.openSession(SessionFactoryUtils.java:114) [org.springframework.orm-3.1.3.RELEASE.jar:]
      at org
      .springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:340) [org.springframework.orm-3.1.3.RELEASE.jar:]

       

      Components versions:

      • Seam 2.3
      • Spring 3.1.3
      • Hibernate 4.2.2

       

      Any hints ?

       

      Thanks in advance!