0 Replies Latest reply on Sep 23, 2009 9:09 AM by kingdom119

    Strange behavior in session context object

    kingdom119

      As my project has to switch between two database,so I extend EntityManagerFactory.But in my customized class, we faced a very strange behavior. First I'd like to show my code first.




      @Scope(ScopeType.APPLICATION)
      public class DdsEntityManagerFactory extends EntityManagerFactory {
      
           @In
           private CurrentUserInformation currentUserInformation;
      
           private static Map<String, javax.persistence.EntityManagerFactory> factorys;
      
           @Unwrap
           public javax.persistence.EntityManagerFactory getEntityManagerFactory() {
                if (!DdsUtil.isNotEmptyString(currentUserInformation
                          .getSelectedDataBase().getDatabaseUnitName())) {
                     currentUserInformation.getSelectedDataBase().setDatabaseUnitName(
                               super.getPersistenceUnitName());
                }
      
                if (factorys.containsKey(currentUserInformation.getSelectedDataBase()
                          .getDatabaseUnitName())) {
                     return factorys.get(currentUserInformation.getSelectedDataBase()
                               .getDatabaseUnitName());
                }
                return null;
           }
      
           @Create
           public void startup(Component component) throws Exception {
                Map<String, Database> dbs = DatabaseCfgBundleLoader
                          .getDatabasesFromProperties();
                factorys = new HashMap<String, javax.persistence.EntityManagerFactory>();
                for (Database db : dbs.values()) {
                     if (!factorys.containsKey(db.getDatabaseUnitName())) {
                          super.setPersistenceUnitName(db.getDatabaseUnitName());
                          factorys.put(db.getDatabaseUnitName(), super
                                    .createEntityManagerFactory());
                     }
                }
           }...........................



      here is the code of CurrentUserInformation class:



      @Name("currentUserInformation")
      @Scope(ScopeType.SESSION)
      @AutoCreate
      public class CurrentUserInformation extends Identity implements Serializable {
           private static final long serialVersionUID = 1L;
      
           @SuppressWarnings("unused")
           @Logger
           private Log log;
      
           private User currentUser;
      
           private Trial selectedTrial;
      
           private Database selectedDataBase;
      ...........................



      OK, normally,my project will works well.But sometimes, it will throw nullpointer exception at
      if (!DdsUtil.isNotEmptyString(currentUserInformation
                          .getSelectedDataBase().getDatabaseUnitName()))

      or if (factorys.containsKey(currentUserInformation.getSelectedDataBase()
                          .getDatabaseUnitName()))


      But, how will this happen? my currentUserInformation has session context. Why it lose value sometimes.Please help.thanks.