为什么第一次(也是第一次!)访问Spring Data存储库查找器总是回滚我的EJB事务?

时间:2017-08-16 14:30:36

标签: java-ee spring-data ejb-3.0

我的申请中有一个奇怪的现象。

我在本地无状态EJB中,并希望调用另一个本地无状态EJB,这会导致以下异常:

javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted

当我研究这个问题时,我发现通常原因是在尝试调用第二个(内部)EJB之前,第一个EJB内部代码中的某个地方存在运行时异常。

显然,即使捕获并处理了该运行时异常,它的存在也足以将事务标记为回滚。到目前为止这是可以理解的。

事情是我不知道相关代码中的任何运行时异常。但是我能够找到导致这种情况的单行代码,并且可以访问Spring Data存储库查找程序,如下所示:

@Inject
CompanyRepository companyRepo;

Company company = companyRepo.findByName(inputVO.getCompanyName());

我有几个存储库,我调用哪一个并不重要,它们都会导致这种影响。

但是,仅在重新部署应用程序后的第一次调用期间。之后一切正常,直到我重新部署应用程序或重新启动Payara服务器。

哦,顺便说一下,对finder-method的调用总是返回一个有效的结果,并且没有"可见"即使在第一次通话时也是例外。

我假设Spring数据代码中有一些运行时异常被处理并处理,因此对我来说是不可见的。也许某种"懒惰的初始化"什么东西只有在异常表明它之前没有被初始化?我不知道......这只是一种预感。

无论如何,我很想知道是否有人知道如何解决这个问题? (在@PostConstruct中进行虚拟查找器调用似乎有点"不优雅")

或许有人甚至知道这个的来源以及如何完全避免它?

供参考我将在下面包含我的applicationContext.xml(但请记住,我没有在Spring容器中运行,我在Payara服务器中。我只使用Spring Data库来自Spring Framework):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/jdbc 
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <jpa:repositories base-package="de.otto.cccs.customerscoring.entities" />

    <tx:jta-transaction-manager />
    <tx:annotation-driven />

    <context:component-scan base-package="de.otto.cccs" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/COR99TSDatasource" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="default" />
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform"
                    value="org.eclipse.persistence.platform.database.OraclePlatform" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

我还没有真正找到解决方案,但由于它与另一个问题(CDI: Injecting single instance works, but injecting Instance<> does not. Why?)有关,并且该问题的解决方案不是使用EJB而是使用纯Java类,我的问题随着回滚的EJB事务也消失了......因为我不再使用EJB了。

所以这不是一个理想的答案,我意识到这一点,但这就是我现在所做的事情。