@@ -793,26 +793,37 @@ public static AbstractRefreshableApplicationContext refreshApplicationContext(Ab
793
793
if (log .isDebugEnabled ())
794
794
log .debug ("Reloading advice for all started modules: " + ModuleFactory .getStartedModules ().size ());
795
795
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 );
809
821
}
810
-
811
- }
812
- catch (Throwable t ) {
813
- log .warn ("Unable to invoke method on the module's activator " , t );
814
822
}
815
823
}
824
+ finally {
825
+ Context .closeSessionWithCurrentUser ();
826
+ }
816
827
817
828
return ctx ;
818
829
}
0 commit comments