2015-04-29 33 views
0

当第二个更新语句失败时(使用非RuntimeException),My @Transactional注释不会回滚第一个插入。该异常在updateNumeroVotos中启动,但Spring不会回滚保存操作的插入。 有什么想法?Spring @Transactional注释不会回滚

我有这些文件:

IdeaService.java代码:

@Service 
public class IdeasServiceImpl implements IdeasService { 

@Autowired 
all daos code 

/** 
* Metodo que inserta un voto de un usuario 
*/ 
@Override 
@Transactional(propagation = Propagation.REQUIRED) 
public void incorporarVoto(String token, Integer id) throws Exception { 
    IdeaVotoVO ideaVoto = new IdeaVotoVO(); 

    ideaVoto.setUsuario(usuariosService.getDatosTokenUsuario(token).getLoginCiudadano()); 
    ideaVoto.setIdIdea(id); 
    ideaVoto.setVoto(ConstantesModel.IDEA_VOTO_POSITIVO); 

    if (validarVoto(ideaVoto)) { 
     ideaVotoDAO.save(ideaVoto);   
     ideaDatosDao.updateNumeroVotos(new Timestamp(Generic.getFechaActual()), id); 
    } 

} 

applicationContext.xml

<mvc:annotation-driven /> 
<mvc:default-servlet-handler /> 

<context:component-scan base-package="example.code.xxxx.*" /> 

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 

    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
    <property name="url" value="jdbc:oracle:thin:@ora11g:1521:xxxxxx" /> 
    <property name="username" value="xxxxxx" /> 
    <property name="password" value="yyyyy" /> 
    <property name="validationQuery" value="SELECT SYSDATE FROM DUAL" /> 
    <property name="maxIdle" value="3" /> 
    <property name="poolPreparedStatements" value="true" /> 
    <property name="maxOpenPreparedStatements" value="100" /> 
    <property name="maxWaitMillis" value="10000" /> 
    <property name="maxTotal" value="20" /> 
</bean> 

<bean id="sessionFactoryCiud" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">validate</prop> 
     </props> 
    </property> 
    <property name="mappingResources"> 
     <list> 
      <<--Here de entity's--> 
     </list> 
    </property> 
</bean> 



<cache:annotation-driven /> 
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
    <property name="cacheManager" ref="ehcache" /> 
</bean> 

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
    p:config-location="classpath:ehcache.xml" /> 


<bean id="TransactionManagerCiud" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactoryCiud" /> 
    <qualifier value="ciudada" /> 
</bean> 


<tx:annotation-driven transaction-manager="TransactionManagerCiud" proxy-target-class="true"/> 



<bean id="ideasService" 
    class="example.code.xxxx.service.ideas.impl.IdeasServiceImpl"> 
</bean> 



<bean id="IdeaVotoDAO" 
    class="example.code.xxxx.model.ideas.impl.IdeaVotoDAOImpl"> 
    <property name="sessionFactory" ref="sessionFactoryCiudadania" /> 
</bean> 

<bean id="IdeaDatosDAO" 
    class="example.code.xxxx.model.ideas.impl.IdeaDatosDAOImpl"> 
    <property name="sessionFactory" ref="sessionFactoryCiud" /> 
</bean> 
</beans> 

我尝试添加该线,但它不工作

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

有' ideaVotoDAO'any' @ Transactional'注解? – xerx593

+0

不,他们没有?是否需要? – MickeyBG

+0

默认情况下,Spring只回滚'RuntimeException's。 (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back)你的方法抛出什么样的'Exception'?这也可能是在配置中未启用“@ EnableTransactionManagement”的原因(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#tx-decl-explained )。 –

回答

3

然后,请添加:

rollbackFor = Exception.class 

您的@Transactional注释。


编辑:

如果你不想编辑每个@Transactional annotatinos的,请看看this interesting approach

从一开始,一个好的设计将是,如果(所有)服务会抛出(定制类型的)RuntimeException,当发生可回滚事件时。 (但这似乎不是修改的所有注释更复杂。)

+0

是的,我试过这个,它的工作原理,但是对于所有的@transational方法来说,这是一个“一般方法”,并且不要写这个anottation? – MickeyBG

+0

我做了一个编辑,但是Gábor击中了它! ;) – xerx593

+0

我试过了,但不工作.....我用这个 – MickeyBG

1

您可以为事务管理这样提供tx:advice S:

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

(如documentation解释)

+0

我试过了,但不工作.....我用这个 – MickeyBG

0

什么是“ sessionFactoryCiudadania“用于”ideaVotoDao“,为什么它与您的事务管理器使用的SessionFactory不同?注释只会从它创建的会话中回滚内容....如果一个DAO在内部使用不同的会话,它将基于自己的规则提交(这可能意味着基本的自动提交模式。)

+0

完成了原始信息,我纠正了这一点。我错误地修补了这个问题的代码。 ¿在这一刻都可以吗? – MickeyBG

相关问题