Skip to content

Commit 57111ab

Browse files
committed
Get published DataSource from EntityManager
Update DataSourceInitializedPublisher to always attempt to obtain the published DataSource directly from the EntityManager. In the case where the EntityManager doesn't provide a DataSource, the previous logic is used. Fixes gh-8296
1 parent 055542f commit 57111ab

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,20 +64,33 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
6464
if (bean instanceof JpaProperties) {
6565
this.properties = (JpaProperties) bean;
6666
}
67-
if (bean instanceof EntityManagerFactory && this.dataSource != null
68-
&& isInitializingDatabase()) {
69-
this.applicationContext
70-
.publishEvent(new DataSourceInitializedEvent(this.dataSource));
67+
if (bean instanceof EntityManagerFactory) {
68+
publishEventIfRequired((EntityManagerFactory) bean);
7169
}
7270
return bean;
7371
}
7472

75-
private boolean isInitializingDatabase() {
73+
private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) {
74+
DataSource dataSource = findDataSource(entityManagerFactory);
75+
if (dataSource != null && isInitializingDatabase(dataSource)) {
76+
this.applicationContext
77+
.publishEvent(new DataSourceInitializedEvent(dataSource));
78+
}
79+
}
80+
81+
private DataSource findDataSource(EntityManagerFactory entityManagerFactory) {
82+
Object dataSource = entityManagerFactory.getProperties()
83+
.get("javax.persistence.nonJtaDataSource");
84+
return (dataSource != null && dataSource instanceof DataSource
85+
? (DataSource) dataSource : this.dataSource);
86+
}
87+
88+
private boolean isInitializingDatabase(DataSource dataSource) {
7689
if (this.properties == null) {
7790
return true; // better safe than sorry
7891
}
7992
Map<String, String> hibernate = this.properties
80-
.getHibernateProperties(this.dataSource);
93+
.getHibernateProperties(dataSource);
8194
if (hibernate.containsKey("hibernate.hbm2ddl.auto")) {
8295
return true;
8396
}

0 commit comments

Comments
 (0)