Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Exception handling required in Spring Transaction?

I am having a doubt in exception handling with a Transaction. To state clearly my problem I would like to show my configuration:

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

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

<bean id="transactionInterceptor" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributeSource">
        <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
    </property>
</bean>

<bean id="baseService" abstract="true">
    <property name="daoProvider" ref="daoProvider" />
</bean>

<bean id="customerService" parent="transactionInterceptor">
    <property name="target">
        <bean class="com.edfx.adb.service.CustomerService" parent="baseService" />
    </property>
</bean>

<bean id="daoProvider" class="com.edfx.adb.dao.provider.DaoProvider">   
    <property name="customerDao" ref="customerDao" />
</bean>

<bean id="customerDao" class="com.edfx.adb.dao.CustomerDao">
    <constructor-arg value="#{T(com.edfx.adb.persist.entity.Customer)}" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

The active transaction class is:

@Transactional
public class CustomerService extends BaseService implements ICustomerService {

    @Transactional(readOnly = true)
    public Customer getCustomerById(String id) {
        return getDaoProvider().getCustomerDao().getCustomerById(id);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
    public void addNewCustomer(CustomerDTO customerDTO) {
        Customer customer = new Customer();

        customer.setCustomerId(customerDTO.getCustomerId());
        customer.setCustomerName(customerDTO.getCustomerName());
        customer.setActive(customerDTO.isActive());

        getDaoProvider().getCustomerDao().save(customer);
    }
}

My doubts lies in the method addNewCustomer. I have set rollbackFor = { Throwable.class }.

How does it work?

Also do I need to explicitly handle exception like:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
public boolean addNewCustomer(CustomerDTO customerDTO) {
    Customer customer = new Customer();

    customer.setCustomerId(customerDTO.getCustomerId());
    customer.setCustomerName(customerDTO.getCustomerName());
    customer.setActive(customerDTO.isActive());

    try {
        getDaoProvider().getCustomerDao().save(customer);
    } catch (Throwable throwable) {
        return false;
    }

    return true;
}

Forcefully I have created an exception by deleting a column from the customer table, BUT that exception wasn't catch in the try-catch block, rather I can catch that exception from the managed bean where I have invoked the addNewCustomer method.

like image 729
Tapas Bose Avatar asked Dec 14 '12 18:12

Tapas Bose


1 Answers

This is an excerpt from Spring docs

In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

You set rollbackFor = Throwable.class, now Spring will rollback for any Exception / Error. By default, whether we like it or not, Spring will rollback only for RuintimeException, and commit otherwise

like image 192
Evgeniy Dorofeev Avatar answered Oct 03 '22 17:10

Evgeniy Dorofeev