I have a Spring service method that performs two database operations on the same table. One with Hibernate Session Factory and the other with plain Spring JDBC. However, I want both to be bound into a single transaction. With the code below, it looks like 2 different transactions are created and the first transaction locks the table up for the second transaction.
How do I make sure both operations are under a single transaction. ?
My Service Class
@Service
public class MyService{
@Autowired
IMyDao mydao;
@Transactional
public void myMethod(){
...
mydao.save(entity); //This uses hibernate
...
mydao.saveBulk(List<Entity> entities>; //This uses spring jdbc for performance reasons.
}
...
}
MyDAOImpl.class
public class MyDAOImpl extends SimpleJDBCSupport implements IMyDao{
private SessionFactory sessionFactory;
@Autowired
public MyDAOImpl (
@Qualifier("sessionFactory") final SessionFactory sessionFactory,
@Qualifier("dataSource") final DataSource dataSource) {
this.sessionFactory = sessionFactory;
this.setDataSource(dataSource);
}
public void save(Entity entity){
sessionFactory.getCurrentSession().createQuery("insert into tableA... " ).executeUpdate() ;
}
public void saveBulk(List<Entity> entites){
...// prepare batch insert data.
this.getJdbcTemplate().batchUpdate("insert into tableA... " , batchPreparedStmtSetter)
}
}
Spring Configuration
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean">
<property name="xaDataSourceClassName" value="${advdb.jdbc.dataSourceClassName}" />
<property name="xaProperties" ref="dataSourceProperties" />
<property name="uniqueResourceName" value="${advdb.jdbc.uniqueResourceName}" />
<property name="testQuery" value="${advdb.jdbc.testQuery}" />
<property name="minPoolSize" value="${advdb.jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${advdb.jdbc.maxPoolSize}" />
<property name="maxIdleTime" value="${advdb.jdbc.maxIdleTime}" />
<property name="borrowConnectionTimeout" value="${advdb.jdbc.borrowConnectionTimeout}" />
<property name="reapTimeout" value="${advdb.jdbc.reapTimeout}" />
<property name="maintenanceInterval" value="${advdb.jdbc.maintenanceInterval}" />
</bean>
<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource- ref="dataSource">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
...
...
</bean>
The transaction boundary is governed by the connection that is used. As long as you are using the same connection (could be same Hibernate Session instance, same JPA entity manager instance or same Spring JdbcTemplate), the tables that are exclusively locked in this connection will not be available for others until the transaction on the current connection is either committed or rolled back.
In your case, since you are using different connections (through
HibernateSessionandSimpleJdbcTemplate) for normal save and bulk save, they cannot be part of the same transaction. The two actions have to be serialized, one after the other.