title: How to configure Spring to make JPA (Hibernate) and JDBC (JdbcTemplate or MyBatis) share the same transaction
body: <p>I have a single dataSource, I use Spring 3.0.3, Hibernate 3.5.1 as JPA provider and I use MyBatis 3.0.2 for some queries and my app runs on Tomcat 6. I have a HibernateDAO and a MyBatisDAO, when I call both from the same method which is annotated with @Transactional it looks like they don't share the same transaction, they get different connections. 
  <br>How can I make them to do?</p>
  
  <p>I've tried getting a connection from DataSourceUtils.getConnection(dataSource) and I get the one which is used by MyBatis which is strange I thought the problem was in MyBatis config and it can't use JpaTransactionManager. Even calling multiple times DataSoruceUtils.getConnection gives the same connection always, which is ok.</p>
  
  <p>After some googling I've tried spring-instrument-tomcat's classloader (although I don't know if tomcat really uses it :))</p>
  
  <p>partial applicationContext</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"&gt;
      &lt;property name="driverClassName" value="${database.driverClassName}"/&gt;
      &lt;property name="url" value="${database.url}"/&gt;
      &lt;property name="username" value="${database.username}"/&gt;
      &lt;property name="password" value="${database.password}"/&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/&gt;
  
  &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation" value="classpath:META-INF/mybatis/mybatis-config.xml" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>partial mybatis config</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;settings&gt;
      &lt;setting name="cacheEnabled" value="false" /&gt;
      &lt;setting name="useGeneratedKeys" value="false" /&gt;
      &lt;setting name="defaultExecutorType" value="REUSE" /&gt;
      &lt;setting name="lazyLoadingEnabled" value="false"/&gt;
  &lt;/settings&gt;
  </code></pre>
  
  <p>partial persistence.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  </code></pre>