T.SET
T.SET

Reputation: 23

Spring 4 + Hibernate 5.1 Transaction auto committed after call another DAO query

I have a problem with transaction auto committed after call another dao native query.

Both service and dao signed as @Transactional.

What am I doing wrong here?

Spring 4.2.x

Hibernate 5.1.0

Atomikos 3.9.3

This is my setup:

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="jtaPlatformAdapter" class="com.xxx.JtaPlatformAdapter">
    <property name="jtaTransactionManager" ref="transactionManager" />
</bean>

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

<bean class="com.atomikos.icatch.jta.UserTransactionImp" id="atomikosUserTransaction" />

<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager" />
    <property name="userTransaction" ref="atomikosUserTransaction" />
</bean>


<bean id="datasouce" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
    ...
</bean>

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="JPAVendorAdapter">
    ...
</bean>

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

persistence.xml

<persistence-unit name="pun" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

service

@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();
}

DAO

@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;
}

thanks!

Upvotes: 2

Views: 1269

Answers (1)

devops
devops

Reputation: 9179

If you are using @Transactional in your code, Spring creates a proxy object for your Dao object (wrapper).

So it looks like this if your application is running:

public EntityObj proxyMethodForFindById(String id) {

    try {
        // 1. start transaction
        startTransaction();

        // 2. execute your code
        return yourDaoObject.findById(id);

    } finally { // [!] PSEUDO CODE: NO EXCEPTION HANDLING

        // commit transaction
        commitTransaction();
    }

}

So what happens in your code?

Your save method is marked also as @Transactional. So if you are changing your object by setting:

model.setX(30);
model.setY(40);

Spring creates two proxies. One for Service and one four your Dao. On the End of the findById-Transaction this changes will be commited. Nested transactions is the keyword.

You should remove @Transaction in your findById-Method or better in the whole Dao object. Service should be transactional, not Dao-layer.

Upvotes: 1

Related Questions