Hy guys,
我想运行一个spring批处理作业然后对结果进行一些测试,但是我需要使用hibernate延迟加载。所以我需要一个打开的事务来保持会话的活跃。
但是我得到了这个异常:IllegalTransactionStateException:找到了预绑定的JDBC连接!
我可以编写方法来直接获取我想要测试的关联对象,但我不想只为测试编写方法并且放弃hibernate的优点。
有办法做到这一点吗?我试图在jobRepositoy中将validateStateTransaction设置为false,但它不起作用(事实上它只适用于内存元数据存储库,如MapJobRepository)。
我正在寻找几天,但没有任何效果。
你能帮帮我吗?感谢我的代码在这里:
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="databaseType">
<value>POSTGRES</value>
</property>
<property name="dataSource" ref="databasePool" />
<property name="transactionManager" ref="transactionManager" />
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
<property name="tablePrefix" value="#{ schema }.BATCH_" />
<property name="validateTransactionState" value="false" />
</bean>
和
@Test
@Transactional
public void test() {
/*
* ===================RUN JOB======================
*/
JobParameters jobParameters = new JobParametersBuilder();
int returnCode = runJob("job_name", jobParameters);
assertEquals("returnCode must be 0.", 0, returnCode );
/*
* ===============END JOB=============================
*/
/*
* ===============TEST ON DATA==========================
*/
ObjectToTest obj = objectDao.findById("1");
assertNotNull( obj.getSomeCollection().get(1));
/*
* =================END TEST==================
*/
}
以下是例外:
org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! JpaTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single JpaTransactionManager for all transactions on a single DataSource, no matter whether JPA or JDBC access.
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:359)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:438)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:261)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy113.getLastJobExecution(Unknown Source)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:98)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:362)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590)
at fr.insee.harmonica.commun.batch.CommandLineJobRunnerTest.runJob(CommandLineJobRunnerTest.java:143)
at fr.insee.harmonica.commun.batch.CommandLineJobRunnerTest.execute(CommandLineJobRunnerTest.java:420)
我尝试将测试部分放在另一个可以使用@Transactional进行分配的方法中,但后来我得到了一个LazyInitializationException ......
编辑:
我使用org.springframework.orm.jpa.JpaTransactionManager实现事务管理器。
这是我的弹簧配置:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
我已经搜索了手动打开交易的方式,但我还没有找到它
答案 0 :(得分:0)
好的,我找到了一个解决方案(最后没有任何帮助)。
要执行此操作,您必须在作业结束后手动打开交易。
你可以这样做:
@Autowired
protected PlatformTransactionManager transactionManager;
@Test
public void test() {
/*
* ===================RUN JOB======================
*/
JobParameters jobParameters = new JobParametersBuilder();
int returnCode = runJob("job_name", jobParameters);
assertEquals("returnCode must be 0.", 0, returnCode );
/*
* ===============END JOB=============================
*/
/*
* ===============TEST ON DATA==========================
*/
(new TransactionTemplate(transactionManager)).execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
ObjectToTest obj = objectDao.findById("1");
assertNotNull( obj.getSomeCollection().get(1));
}
});
/*
* =================END TEST==================
*/
}