Spring + @Transactional: can't rollback in case of error

1.5k Views Asked by At

I am trying to implement the following: I need to add two different entities in same same transaction to database. I have different DAO classes and Service classes for each entity.

public class InvoicesDAO  {
    @Autowired
    protected SessionFactory sessionFactory;

    public void save(Invoice object) {
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);

        session.persist(object);
    }
}

public class RequestsDAO {
    @Autowired
    protected SessionFactory sessionFactory;

    public void save(Request object) {
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);

        session.persist(object);
    }
}

public class InvoicesService {
    @Autowired
    private InvoicesDAO invoicesDAO;

    @Autowired
    private RequestsDAO requestsDAO;

    @Transactional
    public void add(Invoice object) throws HibernateException {
        invoicesDAO.save(object);
    }

    @Transactional
    public void updateAndGenerate(Invoice object1, Request object2) throws HibernateException {
        invoicesDAO.save(object1);
        requestsDAO.save(object2);
    }
}

The config:

<tx:annotation-driven transaction-manager="transactionManager" />
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:/hibernate.properties" />
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${hibernate.connection.driver_class}" />
        <property name="url" value="${hibernate.connection.url}" />
        <property name="username" value="${hibernate.connection.username}" />
        <property name="password" value="${hibernate.connection.password}" />
    </bean>      
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.ejl.butler.object.data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
           </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

<context:annotation-config />
    <context:component-scan base-package="com.service" />
    <bean id="invoicesDao" class="com.dao.InvoicesDAO" />
<bean id="requestsDao" class="com.dao.RequestsDAO" />

Controller:

//***
/**
     * Invoices access service
     */
    @Autowired
    private InvoicesService invoicesService;

        // objects creation
        invoicesService.updateAndGenerate(invoice, request);
//***

So when I am trying to call updateAndGenerate method and pass there invalid values for object2 - it fails without rolling back the object1. How can I fix it? Thank you

2

There are 2 best solutions below

0
On BEST ANSWER

Finally I figured out where the problem was so I will answer my own question...

According to Declarative transactions (@Transactional) doesn't work with @Repository in Spring and https://stackoverflow.com/a/3250959/705869 the order of the base-package items inside context:component-scan directive is very important. In additional, you should put only really necessary packages.

I had some duplicates inside this directive so the application context was initialized before database context. And that's why transactions were disabled inside services!

So check twice for base-package packages inside context:component-scan and remove unnecessary ones.

0
On

I dont think it is got to do with Proxies. You dont need a proxy object here. Generally you need a proxy object for instances such for a login service etc where you need a proxy object for the singleton bean definition. But, the only way it can not rollback is if your propogation level on the Transaction isnt correct.

If you use a Trasaction.REQUIRES_NEW then the dao.save wouldnt rollback and it wouldnt tie back to the outer transaction and hence wouldnt rollback.