2015-04-29 135 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

相關問題