我正在使用Spring Data JPA(SDJ),在我的集成测试中,我遇到了一个奇怪的情况 - 我可能错误配置了一些东西,但我看不出那可能是什么。
简而言之 - 一旦在存储库接口上声明了任何类型的方法,SDJ似乎无法调用Spring的PlatformTransactionManager
实现(即扩展的方法) JpaRepository<T, ID>
)。
我已经把这种行为简化为一个非常平庸的理由 - 在这种情况下,JtaTransactionManager
的setter根本没有调用,但我无法弄清楚原因。
接口是否缺少任何方法声明 - 一切都按原样运行:JtaTransactionManager
setter正常调用并且事务启动,但我真的不喜欢编写不需要的自定义存储库只是为了绕过可能的错误配置/错误(我已经确认实现自定义存储库确实绕过了所提到的行为)。
我很感激您提供给我的任何信息,因为我在这里没有想法。
这是我的相关堆栈:
这个基本界面将正常执行:
package org.test
public interface TestDAO extends JpaRepository<Test, Integer> {}
以下存储库界面将导致以下异常:
package org.test
public interface TestDAO extends JpaRepository<Test, Integer> {
//public static final String TEST = "SELECT t FROM Test t WHERE t.code=:code";
//@Query(TEST)
List<Test> findByCode(String code);}
引发异常(在Spring应用程序上下文引导期间抛出异常,在“配置时”中):
08:23:40.296 | DEBUG | [o.s.d.r.c.AbstractRepositoryConfigDefinitionParser:110] | Triggering auto repository detection
08:23:40.997 | DEBUG | [o.s.d.r.c.AbstractRepositoryConfigDefinitionParser:233] | Registering repository: testDAO - Interface: org.test.TestDAO - Factory: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean, - Custom implementation: null
.....
08:23:41.309 | DEBUG | [o.s.aop.framework.JdkDynamicAopProxy:113] | Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [org.springframework.data.jpa.repository.support.SimpleJpaRepository@6d886d88]
08:23:41.338 | DEBUG | [o.s.d.j.r.query.SimpleJpaQuery:122] | Looking up query for method findByCode
08:23:41.342 | DEBUG | [o.s.d.j.repository.query.NamedQuery:108] | Looking up named query Test.findByCode
08:23:43.451 | TRACE | [openjpa.jdbc.SQL:83] | <t 16515324, conn 2070969200> executing prepstmnt 694298978 SELECT SEQSCHEMA AS SEQUENCE_SCHEMA, SEQNAME AS SEQUENCE_NAME FROM SYSCAT.SEQUENCES
08:23:43.551 | TRACE | [openjpa.jdbc.SQL:83] | <t 16515324, conn 2070969200> [99 ms] spent
08:24:07.248 | INFO | [o.s.b.f.s.DefaultListableBeanFactory:433] | Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6d966d96: defining beans [....]; root of factory hierarchy
08:24:07.253 | INFO | [o.s.o.j.LocalContainerEntityManagerFactoryBean:441] | Closing JPA EntityManagerFactory for persistence unit 'xxx'
08:24:07.307 | ERROR | [o.s.test.context.TestContextManager:324] | Caught exception while allowing TestExecutionListener [....] to prepare test instance [org.test.SomeIntegrationTest@64026402]
java.lang.NullPointerException: null
at org.apache.openjpa.kernel.AbstractBrokerFactory.syncWithManagedTransaction(AbstractBrokerFactory.java:720) ~[openjpa-kernel-2.0.1.jar:2.0.1]
Wrapped by: org.apache.openjpa.persistence.PersistenceException: null
at org.apache.openjpa.kernel.AbstractBrokerFactory.syncWithManagedTransaction(AbstractBrokerFactory.java:752) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:371) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:315) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:231) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:215) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156) ~[openjpa-kernel-2.0.1.jar:2.0.1]
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227) ~[openjpa-persistence-2.0.1.jar:2.0.1]
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154) ~[openjpa-persistence-2.0.1.jar:2.0.1]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.5.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) ~[na:1.5.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.5.0]
at java.lang.reflect.Method.invoke(Method.java:618) ~[na:1.5.0]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376) ~[spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517) ~[spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at $Proxy24.createEntityManager(Unknown Source) ~[na:na]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:234) ~[spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at $Proxy31.createNamedQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.NamedQuery.hasNamedQuery(NamedQuery.java:90) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.NamedQuery.<init>(NamedQuery.java:69) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:111) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:125) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:160) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) ~[spring-data-jpa-1.1.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280) ~[spring-data-commons-core-1.3.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148) ~[spring-data-commons-core-1.3.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125) ~[spring-data-commons-core-1.3.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41) ~[spring-data-commons-core-1.3.0.RELEASE.jar:na]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
当您深入研究炸弹的代码(OpenJPA AbstractBrokerFactory
类)时,OpenJPA需要给定的事务管理器(如下面的OpenJPA属性映射中所定义),但Spring不提供它,因为setter根本没有调用JtaTransactionManager
类(我在调试时验证了这种行为)。
与此相反,如果调用没有任何方法的接口,则会正常调用setter并为OpenJPA提供事务管理器。
这是我的配置 - 您可能会注意到并且可能相关的一件事:我根本没有使用persistence.xml
文件。
数据源
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${dataSource.uniqueName}" />
<property name="xaDataSourceClassName" value="${dataSource.className}" />
<property name="poolSize" value="100" />
<property name="maxPoolSize" value="250" />
<property name="borrowConnectionTimeout" value="5000" />
<property name="xaProperties">
<props>
//snipped for brevity....
</props>
</property>
</bean>
交易管理员
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="3600"/>
</bean>
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true" />
</bean>
实体经理
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="${entityManagerFactory.persistenceUnitName}"/>
<property name="packagesToScan" value="org.test.model" />
<property name="jpaDialect" ref="jpaDialect"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties" ref="openjpaProperties" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.OpenJpaDialect"></bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"></bean>
OpenJPA属性
<util:map id="openjpaProperties">
<!-- Specifies whether JPA is using Managed (global) transactions or Local transactions -->
<entry key="openjpa.TransactionMode" value="managed" />
<entry key="openjpa.ConnectionFactoryMode" value="managed" />
<entry key="openjpa.ConnectionFactory2" value-ref="dataSourceNonXA" />
<!-- Specifies transaction manager to be used -->
<entry key="openjpa.ManagedRuntime" value="invocation(TransactionManagerMethod=com.atomikos.icatch.jta.TransactionManagerImp.getTransactionManager)" />
</util:map>
JPA存储库扫描程序
<jpa:repositories base-package="hr.apisit.b28.intrastat.business.storage.codelists.internal.dao" entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" />
提前致谢
答案 0 :(得分:2)
您需要在启动时初始化atomikos事务管理器。所以,这里唯一需要的是添加:
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" lazy-init="false">
<property name="forceShutdown" value="true" />
</bean>
答案 1 :(得分:0)
你可以试试这个:
<jpa:repositories base- package="hr.apisit.b28.intrastat.business.storage.codelists.internal.dao" entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" depends-on="transactionManager"/>