1 26 package org.objectweb.speedo.j2eedo.common; 27 28 import javax.jdo.JDOException; 29 import javax.jdo.PersistenceManager; 30 import javax.jdo.PersistenceManagerFactory; 31 32 import org.objectweb.speedo.j2eedo.bo.PollsSynchronizations; 33 import org.objectweb.util.monolog.Monolog; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.objectweb.util.monolog.api.Logger; 36 import org.objectweb.util.monolog.api.LoggerFactory; 37 38 42 public class PMHolder { 43 private PersistenceManagerFactory pmf = null; 44 private PersistenceManager pm = null; 45 private int countGetPM = 0; 46 47 static Logger logger = null; 48 static { 49 LoggerFactory lf = Monolog.initialize(); 50 logger = lf.getLogger(PMHolder.class.getName()); 51 } 52 53 57 public PMHolder(PersistenceManagerFactory newPMF){ 58 this.pmf = newPMF; 59 this.countGetPM = 0; 60 } 61 62 public PersistenceManagerFactory getPersistenceManagerFactory() { 63 return pmf; 64 } 65 66 70 public synchronized PersistenceManager getPersistenceManager() { 71 this.countGetPM ++; 72 if (null != this.pm && this.pm.isClosed()) { 73 logger.log(BasicLevel.WARN, "PersistenceManager is closed"); 74 this.countGetPM = 1; 75 this.pm = null; 76 } 77 if (null == this.pm) { 78 try { 79 logger.log(BasicLevel.DEBUG, "Get PersistenceManager"); 80 this.pm = this.pmf.getPersistenceManager(); 81 } catch (NullPointerException e) { 82 throw e; 83 } catch (JDOException e) { 84 throw e; 85 } 86 } 87 logger.log(BasicLevel.DEBUG, "Returned persistenceManager : " + pm); 88 return this.pm; 89 } 90 91 101 public void closePersistenceManager() { 102 this.countGetPM --; 103 if (null != this.pm && this.countGetPM == 0) { 104 logger.log(BasicLevel.DEBUG, "Close PersistenceManager : " + this.pm); 105 if (!this.pm.isClosed()) { 106 if (null != this.pm.getUserObject()) { 108 ((PollsSynchronizations)this.pm.getUserObject()).afterCompletion(-1); 109 } 110 this.pm.close(); 111 } else { 112 logger.log(BasicLevel.WARN, "PersistenceManager allready closed : " + this.pm); 113 } 114 this.pm = null; 115 } 116 } 117 } | Popular Tags |