buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/AutoCommitEntityManager.java
changeset 628 7c4a911dc066
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 package com.nokia.helium.metadata;
       
    18 
       
    19 import javax.persistence.EntityManager;
       
    20 import javax.persistence.EntityManagerFactory;
       
    21 
       
    22 
       
    23 /**
       
    24  * This class implements a simplified EntityManager
       
    25  * designed for batch committing.
       
    26  * Serial persists are committed and cleared each time
       
    27  * the limit is reached.  
       
    28  *
       
    29  */
       
    30 public class AutoCommitEntityManager {
       
    31     private EntityManager entityManager;
       
    32     private int count;
       
    33     private int maxCount = 750;
       
    34     
       
    35     /**
       
    36      * Creating a new AutoCommitEntityManager. The entityManagerFactory will be 
       
    37      * used to get a new EntityManager.
       
    38      * @param entityManagerFactory the entity manager to use to create a EntityManager.
       
    39      */
       
    40     public AutoCommitEntityManager(EntityManagerFactory entityManagerFactory) {
       
    41         this.entityManager = entityManagerFactory.createEntityManager();
       
    42         entityManager.getTransaction().begin();
       
    43     }
       
    44     
       
    45     /**
       
    46      * Persisting an object into the database.
       
    47      * @param o the object to persist
       
    48      */
       
    49     public synchronized void persist(Object o) {
       
    50         entityManager.persist(o);
       
    51         count++;
       
    52         if (count >= maxCount) {
       
    53             entityManager.getTransaction().commit();
       
    54             entityManager.clear();
       
    55             count = 0;
       
    56             entityManager.getTransaction().begin();
       
    57         }
       
    58     }
       
    59     
       
    60     /**
       
    61      * Closing the current entity manager. Committing any 
       
    62      * pending operation.
       
    63      */
       
    64     public synchronized void close() {
       
    65         if (entityManager.getTransaction().isActive()) {
       
    66             entityManager.getTransaction().commit();
       
    67             entityManager.clear();
       
    68         }
       
    69         entityManager.close();
       
    70         entityManager = null;
       
    71     }
       
    72 
       
    73     /**
       
    74      * Internal EntityManager used to
       
    75      * @param <T>
       
    76      * @return the merged entity.
       
    77      */
       
    78     public <T> T merge(T entity) {
       
    79         return entityManager.merge(entity);
       
    80     }
       
    81     
       
    82 }