spring3.x整合hibernate4.x事务不回滚问题

_Jerusalem_ 发布于 2012/07/15 22:01
阅读 2K+
收藏 0

Service定义如下:

@Repository("us")
public class UserService {
 private UserDAO userDAO;
 private LogDAO logDAO;
 public LogDAO getLogDAO() {
  return logDAO;
 }
 @Resource
 public void setLogDAO(LogDAO logDAO) {
  this.logDAO = logDAO;
 }
 public UserDAO getUserDAO() {
  return userDAO;
 }
 @Resource
 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }
 @Transactional(rollbackFor=RuntimeException.class,value="txManager")
 public void save(User u){
    Log log = new Log();

    userDAO.save(u);
  log.setMsg("a user save...");
  logDAO.save(log);   //为了测试回滚,在此方法内抛出RuntimeException异常

     }
}

xml配置如下:

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

 <context:component-scan base-package="hzm.*" />


 <context:property-placeholder location="jdbc.properties" />

 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>

 <bean id="mySessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="myDataSource" />
  <property name="annotatedClasses">
   <list>
    <value>hzm.domain.User</value>
    <value>hzm.domain.Log</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
   </props>
  </property>
 </bean>

 <bean id="txManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <!-- (this dependency is defined somewhere else) -->
  <property name="sessionFactory" ref="mySessionFactory" />
 </bean>


 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="*" rollback-for="-Exception"/>
  </tx:attributes>
 </tx:advice>

 

为什么就是不回滚呢,求指点

加载中
0
h
hom_

数据库什么类型的?

0
s
sphsyv

http://git.oschina.net/sphsyv/sypro

可以参考这个

返回顶部
顶部