title: Transaction Management in Spring 3.x and Hibernate 4
body: <p>Hi I have faced a wall while I was trying to do transaction management using Spring 3.x and Hibernate 4.<br/>
  I've searched on the Internet but there were ways to do this in Hibernate 3, not so many in Hibernate 4.<br/>
  I'm quite confused and I'm not sure if this setting is working.<br/><br/>
  Things I'm using...<br/>
  Spring 3.x<br/>
  Hibernate 4<br/>
  <br/> 
  I've read these<br/>
  <a href="http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative" rel="nofollow noreferrer">http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative</a><br/>
  <a href="https://stackoverflow.com/questions/4138663/spring-hibernate-and-declarative-transaction-implementation-there-is-no-active">spring, hibernate and declarative transaction implementation: there is no active transaction</a><br/>
  and some more...<br/>
  <br/>
  <b>Follow up</b><br/>
  I've managed to run my classes successfully. I've added <code>proxy-target-class="true"</code> to<br/></p>
  
  <pre><code>&lt;aop:config&gt;
  </code></pre>
  
  <p><br/>
  But I'm not sure if this is running in transaction. It says <code>true</code> when I use <br/></p>
  
  <pre><code>TransactionSynchronizationManager.isActualTransactionActive()
  </code></pre>
  
  <p>but when I intentionally throw an error inside the save method, it would not rollback.<br/></p>
  
  <p><b>EDIT</b><br/>
  As suggested by NimChimpsky, I tried invoking the same method through Spring MVC(dispatcher servlet) and it says <br/></p>
  
  <pre><code>    Cannot convert value of type 
  [$Proxy19 implementing kr.co.sgis.services.web.cooingbee.Crudable,org.springframework.aop.SpringProxy,
  org.springframework.aop.framework.Advised] to required type
  [test.TXTestDAO] for property 'dao': 
  no matching editors or conversion strategy found
  </code></pre>
  
  <p><br/>
  I hope I'm getting closer.
  <br/>
  <br/>
  <b>EDIT</b><br/>
  log4j says this.<br/></p>
  
  <pre><code>INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.1.7.Final}
  INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
  INFO : com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -&gt; 3, acquireRetryAttempts -&gt; 30, acquireRetryDelay -&gt; 1000, autoCommitOnClose -&gt; false, automaticTestTable -&gt; null, breakAfterAcquireFailure -&gt; false, checkoutTimeout -&gt; 0, connectionCustomizerClassName -&gt; null, connectionTesterClassName -&gt; com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -&gt; 1bqomxn8s1l3dkxojzz69x|e4865ce, debugUnreturnedConnectionStackTraces -&gt; false, description -&gt; null, driverClass -&gt; com.mysql.jdbc.Driver, factoryClassLocation -&gt; null, forceIgnoreUnresolvedTransactions -&gt; false, identityToken -&gt; 1bqomxn8s1l3dkxojzz69x|e4865ce, idleConnectionTestPeriod -&gt; 0, initialPoolSize -&gt; 3, jdbcUrl -&gt; jdbc:mysql://211.115.111.229:3306/yoursmart, maxAdministrativeTaskTime -&gt; 0, maxConnectionAge -&gt; 0, maxIdleTime -&gt; 0, maxIdleTimeExcessConnections -&gt; 0, maxPoolSize -&gt; 50, maxStatements -&gt; 0, maxStatementsPerConnection -&gt; 0, minPoolSize -&gt; 3, numHelperThreads -&gt; 3, preferredTestQuery -&gt; null, properties -&gt; {user=******, password=******}, propertyCycle -&gt; 0, statementCacheNumDeferredCloseThreads -&gt; 0, testConnectionOnCheckin -&gt; false, testConnectionOnCheckout -&gt; false, unreturnedConnectionTimeout -&gt; 0, userOverrides -&gt; {}, usesTraditionalReflectiveProxies -&gt; false ]
  INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
  INFO : org.hibernate.engine.jdbc.internal.LobCreatorBuilder - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
  INFO : org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
  INFO : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
  </code></pre>
  
  <p><br/>
  It does say <code>' Configured SessionFactory: null'</code> and <code>'TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)'</code> but 
  how does c3p0 connection pool initialise without errors?<br/><br/>
  My spring application context looks like this.<br/></p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;&lt;ref bean="dataSource"/&gt;&lt;/property&gt;
      &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
      &lt;property name="driverClass" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="jdbcUrl" value="****"&gt;&lt;/property&gt;
      &lt;property name="user" value="****"&gt;&lt;/property&gt;
      &lt;property name="password" value="****"&gt;&lt;/property&gt;
      &lt;property name="initialPoolSize"&gt;&lt;value&gt;3&lt;/value&gt;&lt;/property&gt;
      &lt;property name="minPoolSize"&gt;&lt;value&gt;3&lt;/value&gt;&lt;/property&gt;
      &lt;property name="maxPoolSize"&gt;&lt;value&gt;50&lt;/value&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
    &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  
  &lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;
      &lt;tx:attributes&gt;
      &lt;tx:method name="*" propagation="REQUIRED"/&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  </code></pre>
  
  <p>
        
        
      </p>
  
  <pre><code>&lt;bean id="txDAO" class="test.TXTestDAO"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  &lt;bean id="testTxService" class="test.TXTestService"&gt;
          &lt;property name="dao" ref="txDAO"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><br/>
  hibernate.cfg.xml<br/></p>
  
  <pre><code>         &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
           &lt;property name="connection.url"&gt;****&lt;/property&gt;
           &lt;property name="connection.username"&gt;****&lt;/property&gt;
           &lt;property name="connection.password"&gt;****&lt;/property&gt;
           &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
           &lt;property name="connection.pool_size"&gt;5&lt;/property&gt;
           &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
            &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
            &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
            &lt;property name="hibernate.c3p0.timeout"&gt;300&lt;/property&gt;
            &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
            &lt;property name="hibernate.c3p0.idle_test_period"&gt;3000&lt;/property&gt;
  </code></pre>
  
  <p><br/>
  <br/>
  TXTestDAO.java<br/>
  My DAO does not implement HibernateDAOSupport. Do I have to implement it ?<br/></p>
  
  <pre><code>public int save(CooingbeeFetchable bean) {
          // TODO Auto-generated method stub
          try{
  
              sessionFactory.getCurrentSession().getTransaction().begin();
              //Using sessionFactory is low level
              sessionFactory.getCurrentSession().save(bean);
  
              sessionFactory.getCurrentSession().getTransaction().commit();
              System.out.println("committed");
  
          }catch(Exception e){
              e.printStackTrace();
          }
          return 0;
      }
  </code></pre>
  
  <p><br/>My test class<br/> I just used FileSystemResource for testing.<br/></p>
  
  <pre><code>public class Tester {       
  public static void main(String[] args){
  
              FileSystemResource fSysSrc = new FileSystemResource("C:\\SGI_Project\\eclipseWorkspace\\SPSWeb\\WebContent\\WEB-INF\\SpringDispatcher-servlet.xml");
              BeanFactory xmlBeanFactory = new XmlBeanFactory(fSysSrc);
              TXTestService txDAO= (TXTestService) xmlBeanFactory.getBean("testTxService");
  
              TXTestBean bean = new TXTestBean();
  
              bean.setName("bodybody");
              txDAO.save(bean);
  
          }
      }
  </code></pre>
  
  <p><br/></p>
  
  <pre><code>public class TXTestService implements Crudable{
  
      private TXTestDAO dao;
  
      public void setDao(TXTestDAO nDAO){
          dao = nDAO;
      }
  public int save(CooingbeeFetchable bean) {
          // TODO Auto-generated method stub
          return dao.save(bean);
      }
  }
  </code></pre>
  
  <p>So far, I get No session found for current thread exception.<br/>
  Also I'm trying not to use annotation.<br/>
  I feel so lost @.@ ! <br/> Can you point me to the right direction ?<br/></p>