Skip to content

Commit

Permalink
Retrieving a ProgramWorkflow by name in a the ModuleActivator started
Browse files Browse the repository at this point in the history
method throws an error - TRUNK-3739(cherry picked from commit 2529954)
  • Loading branch information
dkayiwa committed Feb 27, 2013
1 parent 8965539 commit cf2d378
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
21 changes: 21 additions & 0 deletions api/src/main/java/org/openmrs/api/context/Context.java
Expand Up @@ -737,6 +737,27 @@ public static void closeSession() {
getContextDAO().closeSession();
}

/**
* Used to define a unit of work which does not require clearing out the currently authenticated user.
* Remember to call closeSessionWithCurrentUser in a, preferably, finally block after this work.
*
* @since 1.10
*/
public static void openSessionWithCurrentUser() {
getContextDAO().openSession();
}

/**
* Used when the a unit of work which started with a call for openSessionWithCurrentUser has finished.
* This should be in a, preferably, finally block.
*
* @since 1.10
*/
public static void closeSessionWithCurrentUser() {
getContextDAO().closeSession();
;
}

/**
* Clears cached changes made so far during this unit of work without writing them to the
* database. If you call this method, and later call closeSession() or flushSession() your
Expand Down
45 changes: 28 additions & 17 deletions api/src/main/java/org/openmrs/module/ModuleUtil.java
Expand Up @@ -793,26 +793,37 @@ public static AbstractRefreshableApplicationContext refreshApplicationContext(Ab
if (log.isDebugEnabled())
log.debug("Reloading advice for all started modules: " + ModuleFactory.getStartedModules().size());

for (Module module : ModuleFactory.getStartedModules()) {
ModuleFactory.loadAdvice(module);
try {
ModuleFactory.passDaemonToken(module);

if (module.getModuleActivator() != null) {
module.getModuleActivator().contextRefreshed();
//if it is system start up, call the started method for all started modules
if (isOpenmrsStartup)
module.getModuleActivator().started();
//if refreshing the context after a user started or uploaded a new module
else if (!isOpenmrsStartup && module.equals(startedModule))
module.getModuleActivator().started();
try {
//The call backs in this block may need lazy loading of objects
//which will fail because we use an OpenSessionInViewFilter whose opened session
//was closed when the application context was refreshed as above.
//So we need to open another session now. TRUNK-3739
Context.openSessionWithCurrentUser();

for (Module module : ModuleFactory.getStartedModules()) {
ModuleFactory.loadAdvice(module);
try {
ModuleFactory.passDaemonToken(module);

if (module.getModuleActivator() != null) {
module.getModuleActivator().contextRefreshed();
//if it is system start up, call the started method for all started modules
if (isOpenmrsStartup)
module.getModuleActivator().started();
//if refreshing the context after a user started or uploaded a new module
else if (!isOpenmrsStartup && module.equals(startedModule))
module.getModuleActivator().started();
}

}
catch (Throwable t) {
log.warn("Unable to invoke method on the module's activator ", t);
}

}
catch (Throwable t) {
log.warn("Unable to invoke method on the module's activator ", t);
}
}
finally {
Context.closeSessionWithCurrentUser();
}

return ctx;
}
Expand Down

0 comments on commit cf2d378

Please sign in to comment.