Spring 数据 @transactional 不会在 SQL Server 和运行时异常之后回滚 [英] Spring data @transactional not rolling back with SQL Server and after runtimeexception

查看:85
本文介绍了Spring 数据 @transactional 不会在 SQL Server 和运行时异常之后回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经启用我的 spring 应用程序来使用事务并相应地注释我的服务方法,但是当抛出 RuntimeException 时,对我的数据库的更改仍然存在.

I've enabled my spring application to use transactions and annotated my service method accordingly but the changes to my DB persist when a RuntimeException is thrown.

我的 Spring 配置如下所示:

My Spring configuration looks like this:

<!-- Data Source. -->   
<jee:jndi-lookup id="dataSource" jndi-name="java:/jdbc/BeheermoduleDS"/>

<!-- JPA Entity Manager. -->    
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/jpa/BeheermoduleDS"/>

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

我的 jboss 配置文件中的数据源配置如下所示:

My datasource configuration in my jboss' configuration file looks like this:

<datasource jta="true" jndi-name="java:/jdbc/BeheermoduleDS" pool-name="BeheermoduleDS" enabled="true" use-java-context="true" use-ccm="true">
    <connection-url>jdbc:sqlserver://localhost:1433;databaseName=Gebruikers;</connection-url>
    <driver>sqljdbc</driver>
    <security>
        <user-name>jboss</user-name>
        <password>*****</password>
    </security>
</datasource>

我的服务方法如下所示:

My Service method looks like this:

@Transactional
public void authorise(Gebruiker user) {

    user.setStatus(GebruikerStatus.Actief.name());
    gebruikerRepo.save(user);
    if (true) {
        throw new RuntimeException("Exception happened just like that");
    }

    // does more stuff here that is never reached
}

我的存储库扩展了一个 spring 数据存储库,如下所示:

My repository extends a spring data repository and looks like this:

public interface GebruikerRepository extends PagingAndSortingRepository<Gebruiker, Long>, QueryDslPredicateExecutor<Gebruiker> {

}

事务由控制器抛出并捕获,控制器只向用户显示发生异常的消息.当我检查我的 SQL Server 数据库时,对用户状态所做的更改已提交.

The transaction is thrown and caught by a controller which just shows a message to the user that an exception occurred. When I check my SQL Server DB, the change made to the user status have been commited.

他们不是应该用 RuntimeException 回滚吗?

Weren't they supposed to have been rolled back with the RuntimeException?

在为 org.springframework.transaction.interceptor 打开调试后,我看到没有为我的服务方法启动任何事务,但它们是为一堆 JpaRepository 方法启动的.

After turning debug on for org.springframework.transaction.interceptor I saw that no transactions are being started for my service method, but they are for a bunch of JpaRepository methods.

此外,这就是我的persistence.xml 的样子:

Also, this is how my persistence.xml looks like:

<persistence-unit name="BeheermodulePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/jdbc/BeheermoduleDS</non-jta-data-source> 

推荐答案

从您描述的症状来看,您正在扫描相同的课程两次.您可能在 ContextLoaderListenerDispatcherServlet 的配置中具有相同的 .

Judging from the symptoms you describe you are scanning for the same classes twice. You probably have the same <context:component-scan /> in both the configuration of the ContextLoaderListener and DispatcherServlet.

您希望 ContextLoaderListener 扫描除 @Controller 之外的所有内容,而 DispatcherServlet 只扫描 @Controllers.导致这样的事情.

You want the ContextLoaderListener to scan for everything but @Controller and the DispatcherServlet only for @Controllers. Leading to something like this.

对于ContextLoaderListener

<!-- Load everything except @Controllers -->
<context:component-scan base-package="com.myapp">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

对于DispatcherServlet

<!-- Load everything except @Controllers -->
<context:component-scan base-package="com.myapp" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

另请参阅@Service 被构造两次以获取另一个示例和更广泛的解释.

See also @Service are constructed twice for another sample and broader explanation.

这篇关于Spring 数据 @transactional 不会在 SQL Server 和运行时异常之后回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆