0

2011 年 12 月 12 日更新

该应用程序正在使用可以在此处找到的通用 DAO 类。

基于重新阅读关于使用@Repository 的spring 手册,看来我需要将其添加到我的GenericDAO 实现中,以便spring 处理异常翻译。

我将 @Repository 添加到 GenericDAO 但结果没有改变。父对象仍被持久化到数据库中。


使用 Spring 2.5.6、JBoss 5.1 及其休眠版本、Spring MVC、MySQL

我正在做一些测试以确保在发生异常时我的事务将正确回滚。在下面的代码中,第二个方法:createProfile 将抛出一个 NullPointerException,从而阻止该方法完成。我曾预计交易会被回滚。我发现用户对象被持久化到数据库中。我想要的是,如果第二个操作失败,那么第一个操作也需要回滚。

我做了一些更改,将父方法的传播更改为 REQUIRED_NEW,但没有看到行为发生任何变化。

我已经打开了春季交易的日志记录。

我一直在这里查看不同的帖子,并查看spring 对交易的看法。我似乎在交易行为方面遗漏了一些东西。

我在这里做错了什么。

我有以下定义

    @Transactional(propagation = Propagation.SUPPORTS)
    public class ProfileServiceImpl implements ProfileService
    {

      @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
      public boolean createProfile(UserDTO pUserDTO, ContactInfoDTO pContactInfoDTO)
        {
            boolean retVal = false;

            if(this.createProfile(pUserDTO))
            {
                if(this.addAddressToUser(pContactInfoDTO, pUserDTO.getId()))
                {
                    retVal = true;
                }
            }
            return retVal;
        }

    @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    public boolean createProfile(UserDTO userDTO)
    {
        boolean retVal = false;

        if(this.getProfileQueries().isLoginUnique(userDTO.getLogin(),userDTO.getSiteInfoId()))
        {
            User user = new User();
            BeanUtils.copyProperties(userDTO, user);
            user.setPassword(this.stringDigester.digest(userDTO.getPassword()));
            user.setSiteInfo(this.getSiteInfoDao().read(userDTO.getSiteInfoId()));
            user.setSecurityLevel(SecurityLevel.ROLE_USER);
            user.setStatus(Status.ACTIVE);

            Long pk = this.getUserDao().create(user);
            userDTO.setId(pk);

            retVal = true;
        }
        return retVal;
    }
    @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    public boolean addAddressToUser(ContactInfoDTO pAddress, Long pProfileId)
    {
        boolean retVal = false;

        if(null != pAddress && null != pProfileId)
        {
            ContactInfo contactInfo = new ContactInfo();
            BeanUtils.copyProperties(pAddress, contactInfo);

            User user = this.getUserDao().read(pProfileId);
            contactInfo.setUser(user);
            this.getContactInfoDao().create(contactInfo);

            user.getUserAddress().setProfileAddress(contactInfo);
            user.getUserAddress().setBillingAddress(contactInfo);
            this.getUserDao().update(user);

            retVal = true;

        }
        return retVal;
    }
}

数据配置

      <!-- TRANSACTION MANAGER--> 
      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
      </bean>

      <!-- ANNOTATION DRIVEN TRANSACTIONS -->
      <tx:annotation-driven transaction-manager="transactionManager" />


  <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/MySqlDS"/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="jndiDataSource"/>
    <property name="annotatedClasses">
      <list>
        <value>com.vsg.dataaccess.user.entity.User</value>
        <value>com.vsg.dataaccess.user.entity.ContactInfo</value>
        <value>com.vsg.dataaccess.user.entity.UserAddress</value>
     </list>
    </property>


    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
        <prop key="hibernate.jdbc.batch_size">20</prop>
        <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
        <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_structured_entries">true</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.generate_statistics">true</prop>
        <prop key="org.hibernate.envers.audit_table_suffix">_aud</prop>
        <prop key="org.hibernate.envers.revision_field_name">rev_number</prop>
        <prop key="org.hibernate.envers.revision_type_field_name">rev_type</prop>
        <prop key="org.hibernate.envers.revision_on_collection_change">true</prop>        
      </props>
    </property>
    <property name="entityInterceptor">
      <bean class="vsg.ecotrak.dataaccess.framework.hibernate.interceptor.AuditTrailInterceptor"/>
    </property>
  </bean>
4

2 回答 2

0

确保(在 IDE 中的调试模式下)类 ProfileServiceImpl 的实例被 Spring 的 AOP 代理包装(这意味着 Spring Transaction 配置正确),还请提供您的事务管理器和数据源(等)的 *.xml 配置

于 2011-12-08T20:39:50.757 回答
0

我认为自动提交导致了这个问题。如果您想在第二个查询失败时回滚第一个查询,您可能需要关闭他并手动管理事务。否则第一个查询将被自动提交。想想这个。

于 2011-12-15T10:43:41.487 回答