Skip to content
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

Fix PersistenceException in production #302

Merged
merged 2 commits into from
Jun 6, 2022
Merged
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
2 changes: 1 addition & 1 deletion docs/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ lazy val docs = project
resolvers ++= DefaultOptions.resolvers(snapshot = true),
libraryDependencies += component("play-java-forms"),
libraryDependencies += component("play-test") % Test,
libraryDependencies += "com.h2database" % "h2" % "1.4.200" % Test,
libraryDependencies += "com.h2database" % "h2" % "2.1.212" % Test,
PlayDocsKeys.javaManualSourceDirectories := (baseDirectory.value / "manual" / "working" / "javaGuide" ** "code").get,
// No resource directories shuts the ebean agent up about java sources in the classes directory
Test / unmanagedResourceDirectories := Nil,
Expand Down
16 changes: 8 additions & 8 deletions play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import com.typesafe.config.ConfigException;
import io.ebean.config.DatabaseConfig;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.scanners.Scanners;
import org.reflections.scanners.TypeElementsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
Expand Down Expand Up @@ -75,7 +74,7 @@ public EbeanConfig parse() {

Map<String, DatabaseConfig> serverConfigs = new HashMap<>();

for (Map.Entry<String, List<String>> entry: ebeanConfig.getDatasourceModels().entrySet()) {
for (Map.Entry<String, List<String>> entry : ebeanConfig.getDatasourceModels().entrySet()) {
String key = entry.getKey();

DatabaseConfig serverConfig = new DatabaseConfig();
Expand All @@ -100,7 +99,7 @@ public EbeanConfig parse() {
private void setServerConfigDataSource(String key, DatabaseConfig serverConfig) {
try {
serverConfig.setDataSource(new WrappingDatasource(dbApi.getDatabase(key).getDataSource()));
} catch(Exception e) {
} catch (Exception e) {
throw new ConfigException.BadValue(
"ebean." + key,
e.getMessage(),
Expand All @@ -110,7 +109,7 @@ private void setServerConfigDataSource(String key, DatabaseConfig serverConfig)
}

private void addModelClassesToServerConfig(String key, DatabaseConfig serverConfig, Set<String> classes) {
for (String clazz: classes) {
for (String clazz : classes) {
try {
serverConfig.addClass(Class.forName(clazz, true, environment.classLoader()));
} catch (Exception e) {
Expand All @@ -128,7 +127,7 @@ private Set<String> getModelClasses(Map.Entry<String, List<String>> entry) {
entry.getValue().forEach(load -> {
load = load.trim();
if (load.endsWith(".*")) {
classes.addAll(Classpath.getTypes(environment, load.substring(0, load.length()-2)));
classes.addAll(Classpath.getTypes(environment, load.substring(0, load.length() - 2)));
} else {
classes.add(load);
}
Expand Down Expand Up @@ -216,6 +215,7 @@ private static class Classpath {
static Set<String> getTypes(Environment env, String packageName) {
return getReflections(env, packageName).getStore().getOrDefault(TypeElementsScanner.class.getSimpleName(), Collections.emptyMap()).keySet();
}

private static Reflections getReflections(Environment env, String packageName) {
// This is not supposed to happen very often, but just when starting the application.
// So it should be okay to not have a cache.
Expand All @@ -232,8 +232,8 @@ private static Reflections getReflections(Environment env, String packageName) {
private static ConfigurationBuilder getReflectionsConfiguration(String packageName, ClassLoader classLoader) {
return new ConfigurationBuilder()
.addUrls(ClasspathHelper.forPackage(packageName, classLoader))
.filterInputsBy(new FilterBuilder().includePackage((packageName + ".").replace(".","\\.") + ".*"))
.setScanners(new TypeElementsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
.filterInputsBy(new FilterBuilder().includePackage(packageName))
.setScanners(new TypeElementsScanner(), Scanners.TypesAnnotated, Scanners.SubTypes);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeAnnotationsScanner and SubTypesScanner are deprecated and use TypesAnnotated and SubTypes in constructors respectively

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ object PlayEbean extends AutoPlugin {

override def trigger = noTrigger

override def projectSettings = inConfig(Compile)(scopedSettings) ++ unscopedSettings
override def projectSettings: Seq[Def.Setting[_]] = inConfig(Compile)(scopedSettings) ++ unscopedSettings

def scopedSettings =
def scopedSettings: Seq[Def.Setting[_]] =
Seq(
playEbeanModels := configuredEbeanModels.value,
manipulateBytecode := ebeanEnhance.value
)

def unscopedSettings =
def unscopedSettings: Seq[Def.Setting[_]] =
Seq(
playEbeanDebugLevel := -1,
playEbeanAgentArgs := Map("debug" -> playEbeanDebugLevel.value.toString),
Expand Down Expand Up @@ -76,15 +76,12 @@ object PlayEbean extends AutoPlugin {
val originalContextClassLoader = Thread.currentThread.getContextClassLoader

try {

val classpath = deps.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL

val classLoader = new java.net.URLClassLoader(classpath, null)
val classpath = deps.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL
val classLoader = new URLClassLoader(classpath, null)

Thread.currentThread.setContextClassLoader(classLoader)

val transformer = new Transformer(classLoader, agentArgsString)

val transformer = new Transformer(classLoader, agentArgsString)
val fileTransform = new OfflineFileTransform(transformer, classLoader, classes.getAbsolutePath)

try {
Expand Down
8 changes: 4 additions & 4 deletions sbt-play-ebean/src/sbt-test/sbt-ebean/enhancement/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ lazy val root = project

scalaVersion := "2.12.15"

sourceDirectory in Test := baseDirectory.value / "tests"
Test / sourceDirectory := baseDirectory.value / "tests"

scalaSource in Test := baseDirectory.value / "tests"
Test / scalaSource := baseDirectory.value / "tests"

javaSource in Test := baseDirectory.value / "tests"
Test / javaSource := baseDirectory.value / "tests"

resolvers ++= DefaultOptions.resolvers(snapshot = true)

libraryDependencies += "com.h2database" % "h2" % "1.4.200"
libraryDependencies += "com.h2database" % "h2" % "2.1.212"
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package models;

import org.junit.*;

import static org.junit.Assert.*;

import play.test.*;
import play.Application;

import static play.test.Helpers.*;

public class TestTask extends WithApplication {
Expand All @@ -15,12 +18,12 @@ protected Application provideApplication() {
@Test
public void saveAndFind() {
Task task = new Task();
task.id = 10l;
task.id = 10L;
task.name = "Hello";
task.done = false;
task.save();

Task saved = Task.find.byId(10l);
Task saved = Task.find.byId(10L);
assertEquals("Hello", saved.name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ scalaVersion := "2.12.15"

resolvers ++= DefaultOptions.resolvers(snapshot = true)

libraryDependencies += "com.h2database" % "h2" % "1.4.200"
libraryDependencies += "com.h2database" % "h2" % "2.1.212"