Spring 4 + Hibernate 5.1 Transaction auto committed after call another DAO query
<p>I have a problem with transaction auto committed after call another dao native query.</p>

<p>Both service and dao signed as @Transactional. </p>

<p>What am I doing wrong here?</p>

<p>Spring 4.2.x</p>

<p>Hibernate 5.1.0</p>

<p>Atomikos 3.9.3</p>

<p>This is my setup:</p>

<pre><code>&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;

&lt;bean id="jtaPlatformAdapter" class="com.xxx.JtaPlatformAdapter"&gt;
    &lt;property name="jtaTransactionManager" ref="transactionManager" /&gt;
&lt;/bean&gt;

&lt;bean class="com.atomikos.icatch.jta.UserTransactionManager" destroy-method="close" id="atomikosTransactionManager" init-method="init"&gt;
    &lt;property name="forceShutdown" value="true" /&gt;
    &lt;property name="startupTransactionService" value="true" /&gt;
&lt;/bean&gt;

&lt;bean class="com.atomikos.icatch.jta.UserTransactionImp" id="atomikosUserTransaction" /&gt;

&lt;bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager"&gt;
    &lt;property name="transactionManager" ref="atomikosTransactionManager" /&gt;
    &lt;property name="userTransaction" ref="atomikosUserTransaction" /&gt;
&lt;/bean&gt;


&lt;bean id="datasouce" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"&gt;
    ...
&lt;/bean&gt;

&lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="JPAVendorAdapter"&gt;
    ...
&lt;/bean&gt;

&lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf" depends-on="transactionManager,jtaPlatformAdapter"&gt;
    &lt;property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /&gt;
    &lt;property name="packagesToScan" value="com.xxx.server"/&gt;
    &lt;property name="dataSource" ref="datasouce" /&gt;
    &lt;property name="persistenceUnitName" value="pun" /&gt;
    &lt;property name="jpaVendorAdapter" ref="JPAVendorAdapter" /&gt;
    &lt;property name="jpaPropertyMap"&gt;
        &lt;map&gt;
            &lt;entry key="hibernate.connection.release_mode" value="on_close" /&gt;
            &lt;entry key="hibernate.transaction.jta.platform" value="com.xxx.server.JtaPlatformAdapter" /&gt;
        &lt;/map&gt;
    &lt;/property&gt;
&lt;/bean&gt;
</code></pre>

<p>persistence.xml</p>

<pre><code>&lt;persistence-unit name="pun" transaction-type="JTA"&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
</code></pre>

<p>service</p>

<pre><code>@Transactional
@Scope("prototype")
public synchronized void save(EntityObj model) throws Exception {
    model.setX(30);
    model.setY(40);

    EntityObj oldModel = entityObjDAO.findById(model.getId());
    // after call findById, the model had been commit to DB
    ...
    ...
    ...

    entityObjDAO.store(model); // this will call entityManager.merge(model)
    entityObjDAO.flush();
}
</code></pre>

<p>DAO</p>

<pre><code>@Transactional
public EntityObj findById(String id) {
    EntityObj model = null;
    String sql = "select id,x,y from EntityObj where id = :id"; // this is a native sql query

    Query query = this.entityManager.createNativeQuery(sql);
    query.setParameter("id", id);
    Object[] rs = (Object[]) query.getSingleResult();
    if (rs != null) {
      model = new EntityObj();
      model.setId(id);
      model.setX(rs[1] == null ? null : (Integer) rs[1]);
      model.setY(rs[2] == null ? null : (Integer) rs[2]);
    }

  return model;
}
</code></pre>

<p>thanks!</p>