dc907b27f076105737bcaea475c64f8678c3d198
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * feature-active-standby-management
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.activestandby;
22
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.FlushModeType;
31 import javax.persistence.LockModeType;
32 import javax.persistence.Query;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class JpaDroolsPdpsConnector implements DroolsPdpsConnector {
38
39         // get an instance of logger 
40         private static final Logger  logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class);
41         private EntityManagerFactory emf;
42                 
43         
44         //not sure if we want to use the same entity manager factory for drools session and pass it in here, or create a new one
45         public JpaDroolsPdpsConnector(EntityManagerFactory emf){
46                 this.emf = emf;         
47         }
48         @Override
49         public Collection<DroolsPdp> getDroolsPdps() {
50                 //return a list of all the DroolsPdps in the database
51                 EntityManager em = emf.createEntityManager();
52                 try {
53                         em.getTransaction().begin();
54                         Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p");
55                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
56                         LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>();
57                         for(Object o : droolsPdpsList){
58                                 if(o instanceof DroolsPdp){
59                                         //Make sure it is not a cached version
60                                         em.refresh((DroolsPdpEntity)o);
61                                         droolsPdpsReturnList.add((DroolsPdp)o);
62                                         if (logger.isDebugEnabled()) {
63                                                 DroolsPdp droolsPdp = (DroolsPdp)o;
64                                                 logger.debug("getDroolsPdps: PDP= {}"
65                                                                 + ", isDesignated= {}"
66                                                                 + ", updatedDate= {}"
67                                                                 + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(),
68                                                                  droolsPdp.getUpdatedDate(), droolsPdp.getPriority());
69                                         }
70                                 }
71                         }
72                         try{
73                         em.getTransaction().commit();
74                         }catch(Exception e){
75                                   logger.error
76                                         ("Cannot commit getDroolsPdps() transaction", e);
77                         }
78                         return droolsPdpsReturnList;
79                 } finally {
80                         cleanup(em, "getDroolsPdps");
81                 }
82         }
83
84         private boolean nullSafeEquals(Object one, Object two){
85                 if(one == null && two == null){
86                         return true;
87                 }
88                 if(one != null && two != null){
89                         return one.equals(two);
90                 }
91                 return false;
92         }
93         
94         @Override
95         public void update(DroolsPdp pdp) {
96                 
97                 if (logger.isDebugEnabled()) {
98                         logger.debug("update: Entering, pdpId={}", pdp.getPdpId());
99                 }
100                 
101                 //this is to update our own pdp in the database
102                 EntityManager em = emf.createEntityManager();
103                 try {
104                         em.getTransaction().begin();
105                         Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
106                         droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
107                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
108                         DroolsPdpEntity droolsPdpEntity;
109                         if(droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)){                                           
110                                 droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
111                                 em.refresh(droolsPdpEntity); //Make sure we have current values
112                                 Date currentDate = new Date();
113                                 long difference = currentDate.getTime()-droolsPdpEntity.getUpdatedDate().getTime();
114                                 //just set some kind of default here
115                                 long pdpTimeout = 15000;
116                                 try{
117                                         pdpTimeout = Long.parseLong(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
118                                 }catch(Exception e){
119                                           logger.error
120                                                 ("Could not get PDP timeout property, using default.", e);
121                                 }
122                                 boolean isCurrent = difference<pdpTimeout;
123                                 if (logger.isDebugEnabled()) {
124                                         logger.debug("update: PDP= {}, isCurrent={}"
125                                                         + " difference= {}"
126                                                         + ", pdpTimeout= {}, designated= {}",
127                                                         pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated());
128                                 }
129                         } else {
130                                 if (logger.isDebugEnabled()) {
131                                         logger.debug("update: For PDP={}"
132                                                         + ", instantiating new DroolsPdpEntity", pdp.getPdpId());
133                                 }
134                                 droolsPdpEntity = new DroolsPdpEntity();
135                                 em.persist(droolsPdpEntity);
136                                 droolsPdpEntity.setPdpId(pdp.getPdpId());                               
137                         }
138                         if(droolsPdpEntity.getPriority() != pdp.getPriority()){
139                                 droolsPdpEntity.setPriority(pdp.getPriority());
140                         }
141                         if(!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())){
142                                 droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
143                         }
144                         /*if(!droolsPdpEntity.getDesignatedDate().equals(pdp.getDesignatedDate())){
145                                 droolsPdpEntity.setDesignatedDate(pdp.getDesignatedDate());
146                         } The designated date is only set below when this first becomes designated*/
147                         if(!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())){
148                                 droolsPdpEntity.setSiteName(pdp.getSiteName());
149                         }
150                         
151                         if(droolsPdpEntity.isDesignated() != pdp.isDesignated()){
152                                 if (logger.isDebugEnabled()) {
153                                         logger.debug("update: pdpId={}"
154                                                         + ", pdp.isDesignated={}"
155                                                         + ", droolsPdpEntity.pdpId= {}"
156                                                         + ", droolsPdpEntity.isDesignated={}",
157                                                          pdp.getPdpId(), pdp.isDesignated(),droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated());
158                                 }
159                                 droolsPdpEntity.setDesignated(pdp.isDesignated());
160                                 //The isDesignated value is not the same and the new one == true
161                                 if(pdp.isDesignated()){
162                                         droolsPdpEntity.setDesignatedDate(new Date());
163                                 }
164                         }
165                         em.getTransaction().commit();
166                 } finally {
167                         cleanup(em, "update");
168                 }
169                 
170                 if (logger.isDebugEnabled()) {
171                         logger.debug("update: Exiting");
172                 }
173
174         }
175
176         /*
177          * Note: A side effect of this boolean method is that if the PDP is designated but not current, the 
178          * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as
179          * being in standby mode)
180          */
181         @Override
182         public boolean isPdpCurrent(DroolsPdp pdp) {
183                 
184                 boolean isCurrent = isCurrent(pdp);
185                 
186                 EntityManager em = emf.createEntityManager();
187                 try{
188                 if(!isCurrent && pdp.isDesignated()){
189                         em.getTransaction().begin();
190                         Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
191                         droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
192                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
193                         if(droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity){                     
194                                 if (logger.isDebugEnabled()) {
195                                         logger.debug("isPdpCurrent: PDP={}  designated but not current; setting designated to false", pdp.getPdpId());
196                                 }
197                                 DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
198                                 droolsPdpEntity.setDesignated(false);
199                                 em.getTransaction().commit();
200                         } else {
201                                 logger.warn("isPdpCurrent: PDP={} is designated but not current; "
202                                                 + "however it does not have a DB entry, so cannot set DESIGNATED to false!", pdp.getPdpId());
203                         }
204                 } else {
205                         if (logger.isDebugEnabled()) {
206                                 logger.debug("isPdpCurrent: For PDP= {}, "
207                                                 + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent);
208                         }
209                 }
210                 }catch(Exception e){
211                           logger.error
212                                 ("Could not update expired record marked as designated in the database", e);
213                 } finally {
214                         cleanup(em, "isPdpCurrent");
215                 }
216                 return isCurrent;
217                 
218         }
219         
220         @Override
221         public void setDesignated(DroolsPdp pdp, boolean designated) {
222
223                 if (logger.isDebugEnabled()) {
224                         logger.debug("setDesignated: Entering, pdpId={}"
225                                         + ", designated={}", pdp.getPdpId(), designated);
226                 }
227
228                 EntityManager em = null;
229                 try {
230                         em = emf.createEntityManager();
231                         em.getTransaction().begin();
232                         Query droolsPdpsListQuery = em
233                                         .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
234                         droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
235                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
236                                         LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
237                         if (droolsPdpsList.size() == 1
238                                         && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
239                                 DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList
240                                                 .get(0);
241
242                                 if (logger.isDebugEnabled()) {
243                                         logger.debug("setDesignated: PDP={}"
244                                                         + " found, designated= {}"
245                                                         + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(),
246                                                          designated);
247                                 }
248                                 droolsPdpEntity.setDesignated(designated);
249                                 if(designated){
250                                         em.refresh(droolsPdpEntity); //make sure we get the DB value
251                                         if(!droolsPdpEntity.isDesignated()){ 
252                                                 droolsPdpEntity.setDesignatedDate(new Date());
253                                         }
254                                 
255                                 }
256                                 em.getTransaction().commit();
257                         } else {
258                                 logger.error("setDesignated: PDP={}"
259                                                 + " not in DB; cannot update designation", pdp.getPdpId());
260                         }
261                 } catch (Exception e) {
262                         logger.error("setDesignated: Caught Exception", e);
263                 } finally {
264                         cleanup(em, "setDesignated");
265                 }
266
267                 if (logger.isDebugEnabled()) {
268                         logger.debug("setDesignated: Exiting");
269                 }
270
271         }
272         
273         
274         @Override
275         public void standDownPdp(String pdpId) {
276                 if(logger.isDebugEnabled()){
277                         logger.debug("standDownPdp: Entering, pdpId={}", pdpId);
278                 }
279
280                 EntityManager em = null;
281                 try {
282                         /*
283                          * Start transaction.
284                          */
285                         em = emf.createEntityManager();
286                         em.getTransaction().begin();
287
288                         /*
289                          * Get droolspdpentity record for this PDP and mark DESIGNATED as
290                          * false.
291                          */
292                         Query droolsPdpsListQuery = em
293                                         .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
294                         droolsPdpsListQuery.setParameter("pdpId", pdpId);
295                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
296                                         LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
297                         DroolsPdpEntity droolsPdpEntity;
298                         if (droolsPdpsList.size() == 1
299                                         && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
300                                 droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
301                                 droolsPdpEntity.setDesignated(false);
302                                 em.persist(droolsPdpEntity);
303                                 if(logger.isDebugEnabled()){
304                                         logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId );
305                                 }
306                         } else {
307                                 logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}"
308                                                 + "; cannot stand down PDP", pdpId);
309                         }
310
311                         /*
312                          * End transaction.
313                          */
314                         em.getTransaction().commit();
315                         cleanup(em, "standDownPdp");
316                         em = null;
317                         
318                         // Keep the election handler in sync with the DB
319                         DroolsPdpsElectionHandler.setMyPdpDesignated(false);
320
321                 } catch (Exception e) {
322                         logger.error("standDownPdp: Unexpected Exception attempting to mark "
323                                         + "DESIGNATED as false for droolspdpentity, pdpId={}"
324                                         + ".  Cannot stand down PDP; message={}", pdpId, e.getMessage(), e);
325                 } finally {
326                         cleanup(em, "standDownPdp");
327                 }
328                 if(logger.isDebugEnabled()){
329                         logger.debug("standDownPdp: Exiting");
330                 }
331
332         }
333         
334         /*
335          * Determines whether or not a designated PDP has failed.
336          * 
337          * Note: The update method, which is run periodically by the
338          * TimerUpdateClass, will un-designate a PDP that is stale.
339          */
340         @Override
341         public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) {
342
343                 if (logger.isDebugEnabled()) {
344                         logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size());
345                 }
346
347                 boolean failed = true;
348                 boolean foundDesignatedPdp = false;
349
350                 for (DroolsPdp pdp : pdps) {
351
352                         /*
353                          * Normally, the update method will un-designate any stale PDP, but
354                          * we check here to see if the PDP has gone stale since the update
355                          * method was run.
356                          * 
357                          * Even if we determine that the designated PDP is current, we keep
358                          * going (we don't break), so we can get visibility into the other
359                          * PDPs, when in DEBUG mode.
360                          */
361                         if (pdp.isDesignated() && isCurrent(pdp)) {
362                                 if (logger.isDebugEnabled()) {
363                                         logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId());
364                                 }
365                                 failed = false;
366                                 foundDesignatedPdp = true;
367                         } else if (pdp.isDesignated() && !isCurrent(pdp)) {
368                                 logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId());
369                                 foundDesignatedPdp = true;
370                         } else {
371                                 if (logger.isDebugEnabled()) {
372                                         logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId());
373                                 }
374                         }
375                 }
376
377                 if (logger.isDebugEnabled()) {
378                         logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}",
379                                         foundDesignatedPdp);
380                 }
381                 return failed;
382         }
383         
384         
385         private boolean isCurrent(DroolsPdp pdp) {
386         
387                 if (logger.isDebugEnabled()) {
388                         logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId());
389                 }
390         
391                 boolean current = false;
392         
393                 // Return if the current PDP is considered "current" based on whatever
394                 // time box that may be.
395                 // If the the PDP is not current, we should mark it as not primary in
396                 // the database
397                 Date currentDate = new Date();
398                 long difference = currentDate.getTime()
399                                 - pdp.getUpdatedDate().getTime();
400                 // just set some kind of default here
401                 long pdpTimeout = 15000;
402                 try {
403                         pdpTimeout = Long.parseLong(ActiveStandbyProperties
404                                         .getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
405                         if (logger.isDebugEnabled()) {
406                                 logger.debug("isCurrent: pdp.timeout={}", pdpTimeout);
407                         }               
408                 } catch (Exception e) {
409                           logger.error
410                                 ("isCurrent: Could not get PDP timeout property, using default.", e);
411                 }
412                 current = difference < pdpTimeout;
413         
414                 if (logger.isDebugEnabled()) {
415                         logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}"
416                                         + "; returning current={}", difference, pdpTimeout, current);
417                 }
418         
419                 return current;
420         }
421         
422         
423         /*
424          * Currently this method is only used in a JUnit test environment. Gets a
425          * PDP record from droolspdpentity table.
426          */
427         @Override
428         public DroolsPdpEntity getPdp(String pdpId) {
429         
430                 if (logger.isDebugEnabled()) {
431                         logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId);
432                 }
433         
434                 DroolsPdpEntity droolsPdpEntity = null;
435         
436                 EntityManager em = null;
437                 try {
438                         em = emf.createEntityManager();
439                         em.getTransaction().begin();
440                         Query droolsPdpsListQuery = em
441                                         .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
442                         droolsPdpsListQuery.setParameter("pdpId", pdpId);
443                         List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
444                                         LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
445                         if (droolsPdpsList.size() == 1
446                                         && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
447                                 droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
448                                 if (logger.isDebugEnabled()) {
449                                         logger.debug("getPdp: PDP={}"
450                                                         + " found, isDesignated={},"
451                                                         + " updatedDate={}, "
452                                                         + "priority={}", pdpId,
453                                                         droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(),
454                                                         droolsPdpEntity.getPriority());                                                 
455                                 }
456                                 
457                                 // Make sure the droolsPdpEntity is not a cached version
458                                 em.refresh(droolsPdpEntity);
459                                 
460                                 em.getTransaction().commit();
461                         } else {
462                                 logger.error("getPdp: PDP={} not found!?", pdpId);
463                         }
464                 } catch (Exception e) {
465                           logger.error
466                                 ("getPdp: Caught Exception attempting to get PDP", e);
467                 } finally {
468                         cleanup(em, "getPdp");
469                 }
470         
471                 if (logger.isDebugEnabled()) {
472                         logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity);
473                 }
474                 return droolsPdpEntity;
475         
476         }
477         
478         /*
479          * Normally this method should only be used in a JUnit test environment.
480          * Manually inserts a PDP record in droolspdpentity table.
481          */
482         @Override
483         public void insertPdp(DroolsPdp pdp) {
484                 if(logger.isDebugEnabled()){
485                         logger.debug("insertPdp: Entering and manually inserting PDP");
486                 }
487
488                 /*
489                  * Start transaction
490                  */
491                 EntityManager em = emf.createEntityManager();
492                 try {
493                         em.getTransaction().begin();
494
495                         /*
496                          * Insert record.
497                          */
498                         DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity();
499                         em.persist(droolsPdpEntity);
500                         droolsPdpEntity.setPdpId(pdp.getPdpId());
501                         droolsPdpEntity.setDesignated(pdp.isDesignated());
502                         droolsPdpEntity.setPriority(pdp.getPriority());
503                         droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
504                         droolsPdpEntity.setSiteName(pdp.getSiteName());
505
506                         /*
507                          * End transaction.
508                          */
509                         em.getTransaction().commit();
510                 } finally {
511                         cleanup(em, "insertPdp");
512                 }
513                 if(logger.isDebugEnabled()){
514                         logger.debug("insertPdp: Exiting");
515                 }
516
517         }
518         
519         /*
520          * Normally this method should only be used in a JUnit test environment.
521          * Manually deletes all PDP records in droolspdpentity table.
522          */
523         @Override
524         public void deleteAllPdps() {
525         
526                 if(logger.isDebugEnabled()){
527                         logger.debug("deleteAllPdps: Entering");
528                 }
529         
530                 /*
531                  * Start transaction
532                  */
533                 EntityManager em = emf.createEntityManager();
534                 try {
535                         em.getTransaction().begin();
536         
537                         Query droolsPdpsListQuery = em
538                                         .createQuery("SELECT p FROM DroolsPdpEntity p");
539                         @SuppressWarnings("unchecked")
540                         List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode(
541                                         LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
542                         if(logger.isDebugEnabled()){
543                                 logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size());
544                         }
545                         for (DroolsPdp droolsPdp : droolsPdpsList) {
546                                 String pdpId = droolsPdp.getPdpId();
547                                 deletePdp(pdpId);
548                         }
549         
550                         /*
551                          * End transaction.
552                          */
553                         em.getTransaction().commit();
554                 } finally {
555                         cleanup(em, "deleteAllPdps");
556                 }
557                 if(logger.isDebugEnabled()){
558                         logger.debug("deleteAllPdps: Exiting");
559                 }
560         
561         }
562         
563         /*
564          * Normally this method should only be used in a JUnit test environment.
565          * Manually deletes a PDP record in droolspdpentity table.
566          */
567         @Override
568         public void deletePdp(String pdpId) {
569                 if(logger.isDebugEnabled()){
570                         logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId);
571                 }
572         
573                 /*
574                  * Start transaction
575                  */
576                 EntityManager em = emf.createEntityManager();
577                 try {
578                         em.getTransaction().begin();
579                 
580                         /*
581                          * Delete record.
582                          */
583                         DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId);
584                         if (droolsPdpEntity != null) {
585                                 if(logger.isDebugEnabled()){
586                                         logger.debug("deletePdp: Removing PDP");
587                                 }
588                                 em.remove(droolsPdpEntity);
589                         } else {
590                                 if(logger.isDebugEnabled()){
591                                         logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId);
592                                 }
593                         }
594
595                         /*
596                          * End transaction.
597                          */
598                         em.getTransaction().commit();
599                 } finally {
600                         cleanup(em, "deletePdp");
601                 }
602                 if(logger.isDebugEnabled()){
603                         logger.debug("deletePdp: Exiting");
604                 }
605         
606         }
607         
608         /*
609          * Close the specified EntityManager, rolling back any pending transaction
610          *
611          * @param em the EntityManager to close ('null' is OK)
612          * @param method the invoking Java method (used for log messages)
613          */
614         private static void cleanup(EntityManager em, String method)
615         {
616                 if (em != null && em.isOpen()) {
617                         if (em.getTransaction().isActive()) {
618                                 // there is an active EntityTransaction -- roll it back
619                                 try {
620                                         em.getTransaction().rollback();
621                                 } catch (Exception e) {
622                                         logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e);
623                                 }
624                         }
625
626                         // now, close the EntityManager
627                         try {
628                                 em.close();
629                         } catch (Exception e) {
630                                 logger.error(method + ": Caught Exception attempting to close EntityManager, ", e);
631                         }
632                 }
633         }
634 }