Java Spring@Transactional注释不会回滚
当第二个更新语句失败时,@Transactional注释不会回滚第一次插入(带有非Java Spring@Transactional注释不会回滚,java,spring,hibernate,spring-transactions,Java,Spring,Hibernate,Spring Transactions,当第二个更新语句失败时,@Transactional注释不会回滚第一次插入(带有非运行时异常)。异常在updateNumeroVotos中启动,但Spring不会回滚保存操作的插入。 有什么想法吗 我有以下文件: IdeaService.javacode: @Service public class IdeasServiceImpl implements IdeasService { @Autowired all daos code /** * Metodo que inserta un
运行时异常)。异常在updateNumeroVotos
中启动,但Spring不会回滚保存操作的插入。
有什么想法吗
我有以下文件:
IdeaService.java
code:
@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>
org.hibernate.dialen.oracle10galent
真的
验证
然后,请加上:
rollbackFor = Exception.class
到您的@Transactional
注释
编辑:
如果您不想编辑每个@Transactional
注释,请查看
从一开始,如果(所有)服务在发生可回滚的情况时抛出(自定义类型的)RuntimeException,那么一个好的设计就是。(但这似乎比修改所有注释更复杂。)然后,请添加:
rollbackFor = Exception.class
到您的@Transactional
注释
编辑:
如果您不想编辑每个@Transactional
注释,请查看
从一开始,如果(所有)服务在发生可回滚的情况时抛出(自定义类型的)RuntimeException,那么一个好的设计就是。(但这似乎比修改所有注释更复杂。)您可以为事务管理器提供tx:advice
s,如下所示:
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
(如中所述。)您可以为事务管理器提供如下tx:advice
s:
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
(如中所述)什么是“ideaVotoDao”使用的“sessionFactoryCiudadania”,为什么它与事务经理使用的SessionFactory不同?批注只将回滚它创建的会话中的内容。。。。如果DAO在内部使用不同的会话,它将根据自己的规则(这可能意味着基本的自动提交模式)进行提交。什么是“ideaVotoDao”使用的“sessionFactoryCiudadania”,为什么它与事务管理器使用的SessionFactory不同?批注只将回滚它创建的会话中的内容。。。。如果DAO在内部使用不同的会话,它将根据自己的规则(这可能意味着基本的自动提交模式)提交。有ideaVotoDAO
任何@Transactional
注释吗?没有,它们没有?是否需要它?默认情况下,仅运行时异常s回滚。()您的方法抛出什么类型的异常?这可能也是在配置()中未启用@EnableTransactionManagement
的原因。我需要回滚所有类型的事务,而不仅仅是运行时异常。有什么方法可以在应用程序上下文中声明它吗?请尝试将log4j.logger.org.springframework.transaction.interceptor=trace放在您的log4j.properties中!!!是否有ideaVotoDAO
任何@Transactional
注释?否,它们没有注释?是否需要注释?默认情况下,只有RuntimeException
s才会回滚。()您的方法抛出什么类型的异常?这可能也是在配置()中未启用@EnableTransactionManagement
的原因。我需要回滚所有类型的事务,而不仅仅是运行时异常。有什么方法可以在应用程序上下文中声明它吗?请尝试将log4j.logger.org.springframework.transaction.interceptor=trace放在您的log4j.properties中!!!是的,我试过了,它很管用,但是对于所有的@transational方法来说,这是一种“通用方法”,不要写那个附加语?谢谢。发现这不是默认行为对我来说真是一个很大的惊喜。是的,我尝试了这个方法,它很有效,但对于所有@transational方法来说,这是一种“通用方法”,不要写那个附加?谢谢。当我发现这不是默认行为时,我感到非常惊讶。我纠正了这一点。我把问题的代码打错了。现在一切都好吗?我纠正了。我把问题的代码打错了。现在一切都好吗?