Add a BatchConfigurer so the transaction manager can adapt to JPA
Autoconfiguration ordering has to be adjusted so that a DataSource is available before an EntityManagerFactory is ever needed. Previously the autoconfigs were accidentally loaded in the right order, but after the change to BatchAutoConfiguration the order has to be explicit. Fixes gh-189pull/127/merge
parent
e48ddaeb99
commit
c71322a0b2
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.batch;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Component
|
||||
public class BasicBatchConfigurer implements BatchConfigurer {
|
||||
|
||||
private DataSource dataSource;
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
private JobRepository jobRepository;
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
public BasicBatchConfigurer(DataSource dataSource) {
|
||||
this(dataSource, null);
|
||||
}
|
||||
|
||||
public BasicBatchConfigurer(DataSource dataSource,
|
||||
EntityManagerFactory entityManagerFactory) {
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobRepository getJobRepository() {
|
||||
return this.jobRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformTransactionManager getTransactionManager() {
|
||||
return this.transactionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobLauncher getJobLauncher() {
|
||||
return this.jobLauncher;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() throws Exception {
|
||||
this.transactionManager = createTransactionManager();
|
||||
this.jobRepository = createJobRepository();
|
||||
this.jobLauncher = createJobLauncher();
|
||||
}
|
||||
|
||||
private JobLauncher createJobLauncher() throws Exception {
|
||||
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
|
||||
jobLauncher.setJobRepository(getJobRepository());
|
||||
jobLauncher.afterPropertiesSet();
|
||||
return jobLauncher;
|
||||
}
|
||||
|
||||
protected JobRepository createJobRepository() throws Exception {
|
||||
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
|
||||
factory.setDataSource(this.dataSource);
|
||||
factory.setTransactionManager(getTransactionManager());
|
||||
factory.afterPropertiesSet();
|
||||
return (JobRepository) factory.getObject();
|
||||
}
|
||||
|
||||
protected PlatformTransactionManager createTransactionManager() {
|
||||
if (this.entityManagerFactory != null) {
|
||||
return new JpaTransactionManager(this.entityManagerFactory);
|
||||
}
|
||||
return new DataSourceTransactionManager(this.dataSource);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue