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

Check a system property before EntityManager injection outside work unit happens #997

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 @@ -36,6 +36,7 @@
/** @author Dhanji R. Prasanna (dhanji@gmail.com) */
@Singleton
class JpaPersistService implements Provider<EntityManager>, UnitOfWork, PersistService {
private static final String SYSTEM_PROPERTY_PROHIBIT_EM_INJECTION_OUTSIDE_WORK_UNIT = "com.google.inject.persist.jpa.prohibitEntityManagerInjectionOutsideWorkUnit";
private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();

private final String persistenceUnitName;
Expand All @@ -51,6 +52,10 @@ public JpaPersistService(
@Override
public EntityManager get() {
if (!isWorking()) {
Preconditions.checkState(!isEntityManagerInjectionOutsideWorkUnitProhibited(),
"Requested EntityManager outside work unit. "
+ "Try calling UnitOfWork.begin() first, or use a PersistFilter if you "
+ "are inside a servlet environment.");
begin();
}

Expand All @@ -64,6 +69,10 @@ public EntityManager get() {
return em;
}

private static boolean isEntityManagerInjectionOutsideWorkUnitProhibited() {
return Boolean.valueOf(System.getProperty(SYSTEM_PROPERTY_PROHIBIT_EM_INJECTION_OUTSIDE_WORK_UNIT));
}

public boolean isWorking() {
return entityManager.get() != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class JpaPersistServiceTest extends TestCase {

private static final String PERSISTENCE_UNIT_NAME = "test_persistence_unit_name";
private static final Properties PERSISTENCE_PROPERTIES = new Properties();
private static final String SYSTEM_PROPERTY_PROHIBIT_EM_INJECTION_OUTSIDE_WORK_UNIT = "com.google.inject.persist.jpa.prohibitEntityManagerInjectionOutsideWorkUnit";

private final JpaPersistService sut =
new JpaPersistService(PERSISTENCE_UNIT_NAME, PERSISTENCE_PROPERTIES);
Expand Down Expand Up @@ -60,5 +61,29 @@ public void test_givenErrorOnEntityManagerClose_whenEndIsCalled_thenEntityManage
}
}

public void test_givenEntityManagerInjectionOutsideWorkUnitProhibited_whenEntityManagerAboutToInject_thenExceptionIsThrown() {
System.setProperty(SYSTEM_PROPERTY_PROHIBIT_EM_INJECTION_OUTSIDE_WORK_UNIT, "true");
sut.start(factory);

try {
sut.get();
fail("Exception expected");
}
catch (IllegalStateException expected) {
// nop
}
}

public void test_givenEntityManagerInjectionOutsideWorkUnitNotProhibited_whenEntityManagerAboutToInject_thenInjectionSucceeds() {
sut.start(factory);

assertNotNull(sut.get());
}

@Override
public void tearDown() throws Exception {
System.clearProperty(SYSTEM_PROPERTY_PROHIBIT_EM_INJECTION_OUTSIDE_WORK_UNIT);
}

private static class SimulatedException extends RuntimeException {}
}