Skip to content

Commit b0c1759

Browse files
authored
Merge pull request #798 from jooby-project/792
Hot-reloading doesnt work if there are multiple ebean datasources fix…
2 parents 48b4ec2 + 0b56c12 commit b0c1759

File tree

3 files changed

+184
-60
lines changed

3 files changed

+184
-60
lines changed

jooby-ebean/src/main/java/org/jooby/ebean/Ebeanby.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
*/
1919
package org.jooby.ebean;
2020

21-
import java.util.Arrays;
2221
import java.util.HashSet;
22+
import java.util.List;
2323
import java.util.Properties;
2424
import java.util.Set;
25+
import java.util.concurrent.atomic.AtomicReference;
2526
import java.util.function.Consumer;
2627

2728
import org.jooby.Env;
@@ -30,6 +31,7 @@
3031
import org.jooby.internal.ebean.EbeanManaged;
3132
import org.jooby.jdbc.Jdbc;
3233

34+
import com.google.common.collect.ImmutableList;
3335
import com.google.inject.Binder;
3436
import com.google.inject.Key;
3537
import com.typesafe.config.Config;
@@ -38,6 +40,7 @@
3840
import io.ebean.EbeanServer;
3941
import io.ebean.config.ContainerConfig;
4042
import io.ebean.config.ServerConfig;
43+
import javaslang.control.Try.CheckedRunnable;
4144

4245
/**
4346
* <h1>ebean module</h1>
@@ -151,7 +154,8 @@
151154
*/
152155
public class Ebeanby extends Jdbc {
153156

154-
private Set<String> packages = new HashSet<>();
157+
/** package for test */
158+
static final AtomicReference<Set<String>> PKG = new AtomicReference<>(new HashSet<>());
155159

156160
static {
157161
// Turn off ebean shutdown hook:
@@ -173,33 +177,13 @@ public Ebeanby(final String name) {
173177
public Ebeanby() {
174178
}
175179

176-
/**
177-
* <p>
178-
* Add one ore more packages. Packages are used by the agent enhancement (if present) and to
179-
* search for entities via class path search when classes have not been explicitly specified.
180-
* </p>
181-
*
182-
* @param packages Packages to enhancement and search for.
183-
* @return This module.
184-
*/
185-
public Ebeanby packages(final String... packages) {
186-
Arrays.stream(packages).forEach(this.packages::add);
187-
return this;
188-
}
189-
190180
@Override
191181
public void configure(final Env env, final Config conf, final Binder binder) {
192182
configure(env, conf, binder, (name, ds) -> {
193183
ServerConfig config = new ServerConfig();
194184

195-
this.packages.add(conf.getString("application.ns"));
196-
197-
EbeanEnhancer.newEnhancer().run(packages);
198-
199185
config.setName(name);
200186

201-
packages.forEach(config::addPackage);
202-
203187
Config cprops = conf.getConfig("ebean");
204188
if (conf.hasPath("ebean." + name)) {
205189
cprops = conf.getConfig("ebean." + name)
@@ -220,6 +204,13 @@ public void configure(final Env env, final Config conf, final Binder binder) {
220204

221205
callback(config, conf);
222206

207+
List<String> packages = config.getPackages();
208+
if (packages == null || packages.size() == 0) {
209+
packages = ImmutableList.of(conf.getString("application.ns"));
210+
}
211+
config.setPackages(packages);
212+
PKG.get().addAll(packages);
213+
223214
EbeanManaged server = new EbeanManaged(conf, config);
224215
env.onStart(server::start);
225216
env.onStop(server::stop);
@@ -232,6 +223,9 @@ public void configure(final Env env, final Config conf, final Binder binder) {
232223
}
233224
keys.generate(EbeanServer.class, name, provider);
234225
});
226+
227+
// Run enhancer once per JVM
228+
env.onStart(runEnhancer());
235229
}
236230

237231
@Override
@@ -248,4 +242,19 @@ private Properties props(final Config config) {
248242
});
249243
return props;
250244
}
245+
246+
/**
247+
* package for test.
248+
*
249+
* @return An enhancer task. It runs once per classloader instance.
250+
*/
251+
static CheckedRunnable runEnhancer() {
252+
return () -> {
253+
Set<String> packages = PKG.getAndSet(null);
254+
if (packages != null) {
255+
EbeanEnhancer.newEnhancer().run(packages);
256+
}
257+
};
258+
}
259+
251260
}

jooby-ebean/src/main/resources/org/jooby/ebean/ebean.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ ebean.debug.sql=true
88
ebean.debug.lazyload=false
99

1010
ebean.disableClasspathSearch = true
11-
ebean.search.packages = ${application.ns}
1211

1312
# -------------------------------------------------------------
1413
# Transaction Logging

0 commit comments

Comments
 (0)