title: Spring : @Transactional @Scheduled method throws TransactionException
body: <p>(Still a bit new to Spring)</p>
  
  <p>I need to have a service method that is at the same time <code>@Scheduled</code> and <code>@Transactional</code>, so that I get to call a DAO in it.</p>
  
  <p>Declarative transactions are enabled, the transaction manager is a <code>org.springframework.orm.hibernate3.HibernateTransactionManager</code> based on a hibernate session factory.</p>
  
  <p>The service class does not implement any interface so a CGLIB proxy is used.</p>
  
  <p>This setup works fine in general (methods called from the web stack i.e. Struts) but this method raises an exception when called by the scheduler.</p>
  
  <p>Here are the relevant bits of code :</p>
  
  <p>The service method (the class is called <code>ClientWakeAndTerminateManager</code>) :</p>
  
  <pre><code>@Scheduled(initialDelay = 5000, fixedRateString = "${rmi.server.threads.clientsScheduleManagement.rate}")
      @Transactional(readOnly = true)
      public void runCheck(){
  
          //Call a read-only DAO method (the DAO is @Autowired as a class field)
  
          //do some stuff with the data loaded from DB
  
      }
  </code></pre>
  
  <p>Relevant parts of my application context :</p>
  
  <pre><code>&lt;!-- switch on the transactional infrastructure --&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/&gt;
  
      &lt;!-- Utility class to execute transactional code where use of annotation is not possible --&gt;
      &lt;bean class="org.springframework.transaction.support.TransactionTemplate" id="txTemplate"&gt;
          &lt;constructor-arg name="transactionManager" ref="transactionManager"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction manager based on Hibernate --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>Exception stack trace :</p>
  
  <pre><code>[ERROR] : Unexpected error occurred in scheduled task.
  org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:661)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
      at ch.unine.sitel.lis.rmi.shared.ClientWakeAndTerminateManager$$EnhancerByCGLIB$$d8be4f34.runCheck(&lt;generated&gt;)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.hibernate.TransactionException: Transaction not successfully started
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:127)
      at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)
      ... 22 more
  </code></pre>
  
  <p>The stack trace seems to tell me that a transactional proxy is indeed used so I don't understand this exception. Help !</p>
  
  <p>EDIT:</p>
  
  <p>I tried to separate the <code>@Transactional</code> and the <code>@Scheduled</code> annotations by :</p>
  
  <ul>
  <li>Create a new class/bean that contaisn the <code>@Scheduled</code> method</li>
  <li>Inject my service to this bean</li>
  <li>Removed the <code>@Scheduled</code> from my original method but left the <code>@Transactional</code></li>
  </ul>
  
  <p>But I still get the same exception. I also tried to put the <code>@Transactional</code> on my DAO method and remove it from my service method : same result.</p>