我正在开发一个具有 JSF、Spring 和 Hibernate 框架的应用程序。我正在尝试使用以下方法测试 Spring 的事务。
@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;
}
}
上述方法假设回滚第一个查询所做的更新,但这不会发生。
在应用程序上下文中,我有以下设置
<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"/>
谁能告诉我我做错了什么?