Skip to content

Commit cf2d378

Browse files
committedFeb 27, 2013
Retrieving a ProgramWorkflow by name in a the ModuleActivator started
method throws an error - TRUNK-3739(cherry picked from commit 2529954)
1 parent 8965539 commit cf2d378

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed
 

‎api/src/main/java/org/openmrs/api/context/Context.java

+21
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,27 @@ public static void closeSession() {
737737
getContextDAO().closeSession();
738738
}
739739

740+
/**
741+
* Used to define a unit of work which does not require clearing out the currently authenticated user.
742+
* Remember to call closeSessionWithCurrentUser in a, preferably, finally block after this work.
743+
*
744+
* @since 1.10
745+
*/
746+
public static void openSessionWithCurrentUser() {
747+
getContextDAO().openSession();
748+
}
749+
750+
/**
751+
* Used when the a unit of work which started with a call for openSessionWithCurrentUser has finished.
752+
* This should be in a, preferably, finally block.
753+
*
754+
* @since 1.10
755+
*/
756+
public static void closeSessionWithCurrentUser() {
757+
getContextDAO().closeSession();
758+
;
759+
}
760+
740761
/**
741762
* Clears cached changes made so far during this unit of work without writing them to the
742763
* database. If you call this method, and later call closeSession() or flushSession() your

‎api/src/main/java/org/openmrs/module/ModuleUtil.java

+28-17
Original file line numberDiff line numberDiff line change
@@ -793,26 +793,37 @@ public static AbstractRefreshableApplicationContext refreshApplicationContext(Ab
793793
if (log.isDebugEnabled())
794794
log.debug("Reloading advice for all started modules: " + ModuleFactory.getStartedModules().size());
795795

796-
for (Module module : ModuleFactory.getStartedModules()) {
797-
ModuleFactory.loadAdvice(module);
798-
try {
799-
ModuleFactory.passDaemonToken(module);
800-
801-
if (module.getModuleActivator() != null) {
802-
module.getModuleActivator().contextRefreshed();
803-
//if it is system start up, call the started method for all started modules
804-
if (isOpenmrsStartup)
805-
module.getModuleActivator().started();
806-
//if refreshing the context after a user started or uploaded a new module
807-
else if (!isOpenmrsStartup && module.equals(startedModule))
808-
module.getModuleActivator().started();
796+
try {
797+
//The call backs in this block may need lazy loading of objects
798+
//which will fail because we use an OpenSessionInViewFilter whose opened session
799+
//was closed when the application context was refreshed as above.
800+
//So we need to open another session now. TRUNK-3739
801+
Context.openSessionWithCurrentUser();
802+
803+
for (Module module : ModuleFactory.getStartedModules()) {
804+
ModuleFactory.loadAdvice(module);
805+
try {
806+
ModuleFactory.passDaemonToken(module);
807+
808+
if (module.getModuleActivator() != null) {
809+
module.getModuleActivator().contextRefreshed();
810+
//if it is system start up, call the started method for all started modules
811+
if (isOpenmrsStartup)
812+
module.getModuleActivator().started();
813+
//if refreshing the context after a user started or uploaded a new module
814+
else if (!isOpenmrsStartup && module.equals(startedModule))
815+
module.getModuleActivator().started();
816+
}
817+
818+
}
819+
catch (Throwable t) {
820+
log.warn("Unable to invoke method on the module's activator ", t);
809821
}
810-
811-
}
812-
catch (Throwable t) {
813-
log.warn("Unable to invoke method on the module's activator ", t);
814822
}
815823
}
824+
finally {
825+
Context.closeSessionWithCurrentUser();
826+
}
816827

817828
return ctx;
818829
}

0 commit comments

Comments
 (0)
Please sign in to comment.