简体   繁体   中英

how to use transaction of spring to rollback the changes when checked exception is caught?

I am working on a application having JSF, Spring and Hibernate framework. I'm trying to test transaction of Spring using following method.

@Override
@Transactional(rollbackFor=UnsupportedOperationException.class, propagation=Propagation.REQUIRED)
public void updateActiveMonth(Long collectionMonthId) throws Exception{
    try{
    Session session = getSessionFactory().getCurrentSession();
    String hql = "update CollectionMonth collectionMonth set active = false";
    Query query = session.createQuery(hql);
    query.executeUpdate();
    if (true) {
        throw new UnsupportedOperationException();
    }
    String hql1 = "update CollectionMonth collectionMonth set collectionMonth.active = true where collectionMonth.id=:collectionMonthId";
    Query query1 = session.createQuery(hql1);
    query1.setLong("collectionMonthId", collectionMonthId);
    query1.executeUpdate();
    }catch(UnsupportedOperationException e){
        throw e;
    }
}

Above method suppose to rollback the update made by first query but this doesn't happen.

In application context I have following settings

<context:annotation-config/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"/>


<!-- PERSISTENCE SUPPORT HERE (Hibernate 4 Mapping In Spring ) -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:configLocation="/WEB-INF/hibernate.cfg.xml" 
      p:packagesToScan="collection.model"/>

    <!-- Transaction Manager is defined -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory" />

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

can anybody tell me what am I doing wrong?

I suppose you forgot to declare your class as spring bean, so the spring container is able to detect and register them. Your method public void updateActiveMonth(Long collectionMonthId) must be part of some kind of service or dao class which is somewhere declared for spring.

There are several ways of doing this like declaring these in xml or by setting the proper annotations using annotation-config and component-scan.

I can recommend reading this great article from beginning to the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM