title: Why doesn't my (spring)HibernateTransactionManager work in wicket?
body: <p>I tried to shorten this to what I think is relevant, I hope it's sufficient and not overwhelming. Please help!</p>
  
  <p>I'm converting a small <em>wicket+databinder+hibernate</em> web application to use <em>wicket+spring+hibernate</em>. I have a DAO service class, with a hibernate SessionFactory injected by Spring. I am able to do read-only operations using the session factory (autocommit is on by default). What I want to do is use the HibernateTransactionManager and the @Transactional annotation to do a transactional operation.</p>
  
  <p>I define a DAO service implementation, which uses an injected SessionFactory in a method marked @Transactional:</p>
  
  <pre><code>public class DAO implements IDAO {
   @SpringBean
   private SessionFactory sessionFactory;
  
   public DAO() {
    super();
   }
  
   @Transactional
   public Object execute(SessionUnit sessionUnit) {
    Session sess = sessionFactory.getCurrentSession();
    Object result;
    result = sessionUnit.run(sess);
    sess.flush();
    return result;
   }
  
   public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
   }
  
   @Transactional
   public boolean isObjectPersistent(Object object) {
    return sessionFactory.getCurrentSession().contains(object);
   }
  }
  </code></pre>
  
  <p>When I try to call <code>isObjectPersistent()</code>, I get a hibernate exception because no one has called session.beginTransaction():</p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: contains is not valid without active transaction
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
   at $Proxy38.contains(Unknown Source)
   at com.gorkwobbler.shadowrun.karma.db.hibernate.DAO.isObjectPersistent(DAO.java:35)
  (reflection stuff omitted...)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
  (reflection stuff omitted...)
   at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:416)
   at org.apache.wicket.proxy.$Proxy36.isObjectPersistent(Unknown Source)
  </code></pre>
  
  <p>I also notice from the full stack trace that the OpenSessionInViewFilter is being invoked, I'm not sure if that's relevant. Let me know if you need the rest of the stack trace.</p>
  
  <p>If I create a custom WebRequestCycle subclass, which begins a transaction, I can get past this. This seems to me to undermine the purpose of @Transactional, and my implementation of it also turned out to be problematic.</p>
  
  <p>Here is my applicationContext.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Reference: http://wicketinaction.com/2009/06/wicketspringhibernate-configuration/ --&gt;
  &lt;beans default-autowire="autodetect"
      xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;
  
      &lt;!-- bean definitions --&gt;
      &lt;bean id="wicketApplication" class="com.gorkwobbler.shadowrun.karma.view.wicket.core.WicketApplication" /&gt;
  
      &lt;bean id="placeholderConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="ignoreUnresolvablePlaceholders" value="false" /&gt;
          &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt;
          &lt;property name="ignoreResourceNotFound" value="false" /&gt;
          &lt;property name="locations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:/application.properties&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName"&gt;
              &lt;value&gt;${jdbc.driver}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;${jdbc.url}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="username"&gt;
              &lt;value&gt;${jdbc.username}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;value&gt;${jdbc.password}&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
      &lt;!-- setup transaction manager  --&gt;
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- hibernate session factory --&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:/hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.gorkwobbler.shadowrun.karma.domain&lt;/value&gt;
                  &lt;value&gt;com.gorkwobbler.shadowrun.karma.domain.*&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dao"
          class="com.gorkwobbler.shadowrun.karma.db.hibernate.DAO"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Don't know what this is for, but it was in the sample config I started from --&gt; 
      &lt;!-- &lt;context:component-scan base-package="com.gorkwobbler.shadowrun.karma" /&gt;  --&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>How can I get my DAO to begin a transaction, commit at the end of that method, or rollback on error? I want to use the most minimal/standard configuration possible; I prefer annotations over XML if given the choice.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I revised the applicationContext above to remove the AOP configuration stuff, which wasn't working, anyway.</p>
  
  <p>Using the debugger, I determined that the SessionImpl stored in the TransactionInterceptor's session holder map is not the same session as the SessionImpl that is retrieved in the DAO method when I call sessionFactory.getCurrentSession(). Can anyone explain why this is? What am I doing wrong? The magic is not working. =(</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I also notice the following message in my console during startup:</p>
  
  <pre><code>WARN  - stractEhcacheRegionFactory - No TransactionManagerLookup found in Hibernate config, XA Caches will be participating in the two-phase commit!
  </code></pre>