Skip to content

Commit

Permalink
# To fix ebean / play injection race bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
splendidbits committed Mar 25, 2018
1 parent fa2defd commit 3e179b6
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 26 deletions.
12 changes: 12 additions & 0 deletions app/annotations/CommuteEbeanServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package annotations;

import com.google.inject.BindingAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@BindingAnnotation
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.CONSTRUCTOR }) @Retention(RetentionPolicy.RUNTIME)
public @interface CommuteEbeanServer {}
3 changes: 2 additions & 1 deletion app/controllers/SignupController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controllers;

import annotations.CommuteEbeanServer;
import com.google.inject.Inject;
import enums.pushservices.PlatformType;
import helpers.ValidationHelper;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class SignupController extends Controller {
private FormFactory mFormFactory;

@Inject
public SignupController(EbeanServer ebeanServer, FormFactory formFactory) {
public SignupController(@CommuteEbeanServer EbeanServer ebeanServer, FormFactory formFactory) {
mEbeanServer = ebeanServer;
mFormFactory = formFactory;
}
Expand Down
3 changes: 2 additions & 1 deletion app/dao/AccountDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dao;

import annotations.CommuteEbeanServer;
import enums.pushservices.PlatformType;
import io.ebean.EbeanServer;
import io.ebean.FetchConfig;
Expand All @@ -21,7 +22,7 @@
public class AccountDao extends BaseDao {

@Inject
public AccountDao(EbeanServer ebeanServer) {
public AccountDao(@CommuteEbeanServer EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
3 changes: 2 additions & 1 deletion app/dao/AgencyDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.FetchConfig;
import io.ebean.OrderBy;
Expand All @@ -22,7 +23,7 @@
public class AgencyDao extends BaseDao {

@Inject
public AgencyDao(EbeanServer ebeanServer) {
public AgencyDao(@CommuteEbeanServer EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
3 changes: 2 additions & 1 deletion app/dao/BaseDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.config.ServerConfig;
import io.ebeaninternal.api.SpiEbeanServer;
Expand All @@ -14,7 +15,7 @@
class BaseDao {
EbeanServer mEbeanServer;

BaseDao(EbeanServer ebeanServer) {
BaseDao(@CommuteEbeanServer EbeanServer ebeanServer) {
mEbeanServer = ebeanServer;
}

Expand Down
3 changes: 2 additions & 1 deletion app/dao/DeviceDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.OrderBy;
import models.devices.Device;
Expand All @@ -19,7 +20,7 @@
public class DeviceDao extends BaseDao {

@Inject
public DeviceDao(EbeanServer ebeanServer) {
public DeviceDao(@CommuteEbeanServer EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
2 changes: 2 additions & 0 deletions app/injection/modules/DatabaseModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package injection.modules;

import annotations.CommuteEbeanServer;
import com.google.inject.AbstractModule;
import injection.providers.CommuteEbeanServerProvider;
import io.ebean.EbeanServer;
Expand All @@ -12,6 +13,7 @@ public class DatabaseModule extends AbstractModule {
@Override
protected void configure() {
bind(EbeanServer.class)
.annotatedWith(CommuteEbeanServer.class)
.toProvider(CommuteEbeanServerProvider.class)
.asEagerSingleton();
}
Expand Down
3 changes: 1 addition & 2 deletions app/injection/modules/SchedulersModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class SchedulersModule extends AbstractModule implements AkkaGuiceSupport
protected void configure() {
bindActor(AgencyUpdateActor.class, AgencyUpdateActor.ACTOR_NAME);

bind(CommuteSchedulers.class)
.asEagerSingleton();
bind(CommuteSchedulers.class).asEagerSingleton();
}
}
25 changes: 17 additions & 8 deletions app/injection/providers/CommuteEbeanServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory;
import io.ebean.config.ServerConfig;
Expand All @@ -17,6 +18,10 @@
import models.alerts.Route;
import models.devices.Device;
import models.devices.Subscription;
import play.Environment;
import play.db.DBApi;
import play.db.ebean.EbeanConfig;
import play.inject.ApplicationLifecycle;

import java.util.ArrayList;
import java.util.Map;
Expand All @@ -28,10 +33,17 @@
* Copyright 4/2/16 Splendid Bits.
*/
public class CommuteEbeanServerProvider implements Provider<EbeanServer> {
private final EbeanServer mEbeanServer;

private final Config config;

@Inject
public CommuteEbeanServerProvider(Config config) {
public CommuteEbeanServerProvider(Config config, ServerConfig serverConfig, EbeanConfig ebeanConfig,
ApplicationLifecycle lifecycle, Environment environment, DBApi dbApi) {
this.config = config;
}

@Override
public EbeanServer get() {
if (config == null || config.isEmpty()) {
throw new RuntimeException("No Play Framework configuration found.");
}
Expand Down Expand Up @@ -72,11 +84,8 @@ public CommuteEbeanServerProvider(Config config) {
serverConfig.setDdlGenerate(false);
serverConfig.setUpdateChangesOnly(false);

mEbeanServer = EbeanServerFactory.create(serverConfig);
}

@Override
public EbeanServer get() {
return mEbeanServer;
EbeanServer ebeanServer = EbeanServerFactory.create(serverConfig);
Ebean.register(ebeanServer, true);
return ebeanServer;
}
}
3 changes: 2 additions & 1 deletion app/main/LifecycleListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main;

import annotations.CommuteEbeanServer;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.ebean.EbeanServer;
Expand All @@ -11,7 +12,7 @@
public class LifecycleListener {

@Inject
public LifecycleListener(EbeanServer ebeanServer, ApplicationLifecycle applicationLifecycle) {
public LifecycleListener(@CommuteEbeanServer EbeanServer ebeanServer, ApplicationLifecycle applicationLifecycle) {
applicationLifecycle.addStopHook(() -> CompletableFuture.runAsync(() -> {
ebeanServer.shutdown(true, false);
}));
Expand Down
12 changes: 2 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,13 @@ libraryDependencies ++= Seq(
guice,
"com.splendidbits" % "play-pushservices" % "1.1.2",
"com.typesafe.play" %% "play-ahc-ws-standalone" % "1.0.1",
"com.typesafe.play" %% "play-ws-standalone-json" % "1.0.1",
"com.typesafe.play" %% "play-ws-standalone-xml" % "1.0.1",
"com.typesafe.play" %% "play-json" % "2.6.1",
"io.ebean" % "ebean-agent" % "10.1.6",
"org.postgresql" % "postgresql" % "42.2.2",
"org.jetbrains" % "annotations" % "13.0",
"org.jsoup" % "jsoup" % "1.10.1",
"com.google.code.gson" % "gson" % "2.8.2",
"junit" % "junit" % "4.12" % Test,
"org.mockito" % "mockito-all" % "2.0.2-beta" % Test
"org.jsoup" % "jsoup" % "1.10.1",
"junit" % "junit" % "4.12" % Test
)

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v", "-q")
//resolvers += ("Local Maven Repository" at "/Users/daniel/.ivy2/cache")

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
Expand Down
5 changes: 5 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ play.ws.ssl {
}
}

# To fix ebean / play injection race bug.
# https://github.com/playframework/playframework/issues/7017
# https://github.com/playframework/play-ebean/issues/51
//play.modules.disabled += "play.db.ebean.EbeanModule"

# !!! WARNING !!
# EBeanORM Configuration
# Postgresql Database Engine Configuration
Expand Down

0 comments on commit 3e179b6

Please sign in to comment.