buildframework/helium/sf/java/jpa/src/com/nokia/helium/jpa/ORMUtil.java
branchhelium-9.0
changeset 618 df88fead2976
parent 587 85df38eb4012
equal deleted inserted replaced
587:85df38eb4012 618:df88fead2976
    36     private static final int READ_CACHE_LIMIT = 30000;
    36     private static final int READ_CACHE_LIMIT = 30000;
    37 
    37 
    38     private static HashMap<String, ORMEntityManager> emMap = 
    38     private static HashMap<String, ORMEntityManager> emMap = 
    39         new HashMap<String, ORMEntityManager>();
    39         new HashMap<String, ORMEntityManager>();
    40 
    40 
       
    41     private static HashMap<String, Integer> emMapCount = 
       
    42         new HashMap<String, Integer>();
       
    43 
       
    44     private static Object mutexObject = new Object();
    41     private ORMUtil() {
    45     private ORMUtil() {
    42     }
    46     }
    43     
    47     
    44     /**
    48     /**
    45      * Initializes the entity manager and begins the transcations.
    49      * Initializes the entity manager and begins the transcations.
    46      * @param urlPath - database path to be connected to.
    50      * @param urlPath - database path to be connected to.
    47      */
    51      */
    48     public static synchronized void initializeORM(String urlPath) {
    52     public static void initializeORM(String urlPath) {
    49         ORMEntityManager manager = emMap.get(urlPath);
    53         synchronized (mutexObject) {
    50         log.debug("initializeORM: urlpath: " + urlPath);
    54             ORMEntityManager manager = emMap.get(urlPath);
    51         if (manager == null) {
    55             log.debug("initializeORM: urlpath: " + urlPath);
    52             try {
    56             if (manager == null) {
    53                 manager = new ORMEntityManager(urlPath);
    57                 try {
    54                 emMap.put(urlPath, manager);
    58                     log.debug("initializing for the first time");
    55                 log.debug("initializeORM: manager: " + manager);
    59                     manager = new ORMEntityManager(urlPath);
    56                 log.debug("initializeORM: manager: " + manager.getEntityManager());
    60                     emMap.put(urlPath, manager);
    57             } catch ( IOException ex ) {
    61                     Integer countObj = new Integer(1);
    58                 throw new BuildException("Entity Manager creation failure");
    62                     emMapCount.put(urlPath, countObj);
       
    63                     log.debug("initializeORM: manager: " + manager);
       
    64                     log.debug("initializeORM: manager: " + manager.getEntityManager());
       
    65                 } catch ( IOException ex ) {
       
    66                     throw new BuildException("Entity Manager creation failure");
       
    67                 }
       
    68             } else {
       
    69                 Integer countObj = emMapCount.get(urlPath);
       
    70                 log.debug("object exists and incrementing the value");
       
    71                 countObj = new Integer(countObj.intValue() + 1);
       
    72                 log.debug("object exists count value: " + countObj.intValue());
       
    73                 emMapCount.put(urlPath, countObj);
    59             }
    74             }
    60         }
    75         }
    61     }
    76     }
    62 
    77 
    63     /**
    78     /**
    64      * Helper Function to return the entity manager.
    79      * Helper Function to return the entity manager.
    65      * @return entity manager created during initialization.
    80      * @return entity manager created during initialization.
    66      */
    81      */
    67     public static ORMEntityManager getEntityManager(String urlPath) {
    82     public static ORMEntityManager getEntityManager(String urlPath) {
    68         log.debug("getEntityManager: urlpath: " + urlPath);
    83         log.debug("getEntityManager: urlpath: " + urlPath);
    69         ORMEntityManager manager = emMap.get(urlPath);
    84         synchronized (mutexObject) {
    70         if (manager != null) {
    85             ORMEntityManager manager = emMap.get(urlPath);
    71             log.debug("getEntityManager: manager: " + manager);
    86             if (manager != null) {
    72             log.debug("getEntityManager: manager.entityManager: " + manager.getEntityManager());
    87                 log.debug("getEntityManager: manager: " + manager);
    73             return manager;
    88                 log.debug("getEntityManager: manager.entityManager: " + manager.getEntityManager());
    74         } else {
    89                 return manager;
    75             log.debug("getEntityManager: manager: is null");
    90             } else {
    76             throw new BuildException("ORM entity manager is null");
    91                 log.debug("getEntityManager: manager: is null");
       
    92                 throw new BuildException("ORM entity manager is null");
       
    93             }
    77         }
    94         }
    78     }
    95     }
    79     
    96     
    80     /**
    97     /**
    81      * Finalize the entity manager and release all the objects.
    98      * Finalize the entity manager and release all the objects.
    82      */
    99      */
    83     public static void finalizeORM(String urlPath) {
   100     public static void finalizeORM(String urlPath) {
    84         ORMEntityManager manager = emMap.get(urlPath);
   101         synchronized (mutexObject) {
    85         log.debug("finalizeORM: urlpath: " + urlPath);
   102             ORMEntityManager manager = emMap.get(urlPath);
    86         if (manager != null) {
   103             log.debug("finalizeORM: urlpath: " + urlPath);
    87             manager.finalizeEntityManager();
   104             if (manager != null) {
    88             manager = null;
   105                 Integer countObj = emMapCount.get(urlPath);
    89             log.debug("finalizeORM: manager" + manager);
   106                 if (countObj != null) {
    90             emMap.remove(urlPath);
   107                     int count = countObj.intValue();
       
   108                     count = count - 1;
       
   109                     if (count > 0) {
       
   110                         countObj = new Integer (count);
       
   111                         log.debug("countOBj value: " + countObj.intValue());
       
   112                         emMapCount.put(urlPath, countObj);
       
   113                     } else {
       
   114                         manager.finalizeEntityManager();
       
   115                         manager = null;
       
   116                         log.debug("finalizeORM: manager" + manager);
       
   117                         emMap.remove(urlPath);
       
   118                         emMapCount.remove(urlPath);
       
   119                     }
       
   120                 }
       
   121             }
    91         }
   122         }
    92     }
   123     }
       
   124     
       
   125     public static Object getMutexObject() {
       
   126         return mutexObject;
       
   127     }
    93 }
   128 }