Skip to content

Add support for background initialization of EntityManagerFactory #7184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
Expand Down Expand Up @@ -107,11 +108,13 @@ public JpaVendorAdapter jpaVendorAdapter() {
@ConditionalOnMissingBean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
JpaVendorAdapter jpaVendorAdapter,
ObjectProvider<PersistenceUnitManager> persistenceUnitManagerProvider) {
ObjectProvider<PersistenceUnitManager> persistenceUnitManagerProvider,
ObjectProvider<AsyncTaskExecutor> bootstrapExecutorProvider) {
EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
jpaVendorAdapter, this.properties.getProperties(),
persistenceUnitManagerProvider.getIfAvailable(),
determinePersistenceUnitRootLocation());
determinePersistenceUnitRootLocation(),
bootstrapExecutorProvider.getIfUnique());
builder.setCallback(getVendorCallback());
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

Expand All @@ -58,6 +62,7 @@
*
* @author Phillip Webb
* @author Dave Syer
* @author Vedran Pavic
*/
public abstract class AbstractJpaAutoConfigurationTests {

Expand Down Expand Up @@ -196,6 +201,41 @@ public void customPersistenceUnitManager() throws Exception {
.isEqualTo(this.context.getBean(PersistenceUnitManager.class));
}

@Test
public void withTaskExecutor() {
setupTestConfiguration(TestConfigurationWithTaskExecutor.class);
this.context.refresh();
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
AsyncTaskExecutor bootstrapExecutor = (AsyncTaskExecutor) ReflectionTestUtils
.getField(entityManagerFactoryBean, "bootstrapExecutor");
assertThat(bootstrapExecutor).isEqualTo(
this.context.getBean(AsyncTaskExecutor.class));
}

@Test
public void withMultipleTaskExecutors() {
setupTestConfiguration(TestConfigurationWithMultipleTaskExecutors.class);
this.context.refresh();
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
AsyncTaskExecutor bootstrapExecutor = (AsyncTaskExecutor) ReflectionTestUtils
.getField(entityManagerFactoryBean, "bootstrapExecutor");
assertThat(bootstrapExecutor).isNull();
}

@Test
public void withPrimaryTaskExecutor() {
setupTestConfiguration(TestConfigurationWithPrimaryTaskExecutor.class);
this.context.refresh();
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
AsyncTaskExecutor bootstrapExecutor = (AsyncTaskExecutor) ReflectionTestUtils
.getField(entityManagerFactoryBean, "bootstrapExecutor");
assertThat(bootstrapExecutor).isEqualTo(
this.context.getBean("taskExecutorOne", AsyncTaskExecutor.class));
}

protected void setupTestConfiguration() {
setupTestConfiguration(TestConfiguration.class);
}
Expand Down Expand Up @@ -306,6 +346,47 @@ public PersistenceUnitManager persistenceUnitManager() {

}

@Configuration
protected static class TestConfigurationWithTaskExecutor extends TestConfiguration {

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
return new ThreadPoolTaskExecutor();
}

}

@Configuration
protected static class TestConfigurationWithMultipleTaskExecutors extends TestConfiguration {

@Bean
public ThreadPoolTaskExecutor taskExecutorOne() {
return new ThreadPoolTaskExecutor();
}

@Bean
public ThreadPoolTaskExecutor taskExecutorTwo() {
return new ThreadPoolTaskExecutor();
}

}

@Configuration
protected static class TestConfigurationWithPrimaryTaskExecutor extends TestConfiguration {

@Bean
@Primary
public ThreadPoolTaskExecutor taskExecutorOne() {
return new ThreadPoolTaskExecutor();
}

@Bean
public ThreadPoolTaskExecutor taskExecutorTwo() {
return new ThreadPoolTaskExecutor();
}

}

@SuppressWarnings("serial")
static class CustomJpaTransactionManager extends JpaTransactionManager {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.sql.DataSource;

import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
Expand All @@ -41,6 +42,7 @@
* @author Dave Syer
* @author Phillip Webb
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.3.0
*/
public class EntityManagerFactoryBuilder {
Expand All @@ -53,6 +55,8 @@ public class EntityManagerFactoryBuilder {

private final URL persistenceUnitRootLocation;

private final AsyncTaskExecutor bootstrapExecutor;

private EntityManagerFactoryBeanCallback callback;

/**
Expand Down Expand Up @@ -82,10 +86,31 @@ public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager,
URL persistenceUnitRootLocation) {
this(jpaVendorAdapter, jpaProperties, persistenceUnitManager,
persistenceUnitRootLocation, null);
}

/**
* Create a new instance passing in the common pieces that will be shared if multiple
* EntityManagerFactory instances are created.
* @param jpaVendorAdapter a vendor adapter
* @param jpaProperties JPA properties to be passed to the persistence provider.
* @param persistenceUnitManager optional source of persistence unit information (can
* be null)
* @param persistenceUnitRootLocation the persistence unit root location to use as a
* fallback (can be null)
* @param bootstrapExecutor optional asynchronous executor for background
* bootstrapping (can be null)
* @since 1.5.0
*/
public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter,
Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager,
URL persistenceUnitRootLocation, AsyncTaskExecutor bootstrapExecutor) {
this.jpaVendorAdapter = jpaVendorAdapter;
this.persistenceUnitManager = persistenceUnitManager;
this.jpaProperties = new LinkedHashMap<String, Object>(jpaProperties);
this.persistenceUnitRootLocation = persistenceUnitRootLocation;
this.bootstrapExecutor = bootstrapExecutor;
}

public Builder dataSource(DataSource dataSource) {
Expand Down Expand Up @@ -192,7 +217,10 @@ public LocalContainerEntityManagerFactoryBean build() {
}
entityManagerFactoryBean.setJpaVendorAdapter(
EntityManagerFactoryBuilder.this.jpaVendorAdapter);

if (EntityManagerFactoryBuilder.this.bootstrapExecutor != null) {
entityManagerFactoryBean.setBootstrapExecutor(
EntityManagerFactoryBuilder.this.bootstrapExecutor);
}
if (this.jta) {
entityManagerFactoryBean.setJtaDataSource(this.dataSource);
}
Expand Down