##### Coverage Report - com.ctrip.apollo.biz.utils.EntityManagerUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
EntityManagerUtil |
28% | 2/7
N/A | N/A
| 1.0;1

1 |  |
     package com.ctrip.apollo.biz.utils;
2 |  |
3 |  |
     import org.slf4j.Logger;
4 |  |
     import org.slf4j.LoggerFactory;
5 |  |
     import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
6 |  |
     import org.springframework.orm.jpa.EntityManagerFactoryUtils;
7 |  |
     import org.springframework.orm.jpa.EntityManagerHolder;
8 |  |
     import org.springframework.stereotype.Component;
9 |  |
     import org.springframework.transaction.support.TransactionSynchronizationManager;
10 |  |
11 |  |
     /**
12 |  |
      * @author Jason Song(song_s@ctrip.com)
13 |  |
      */
14 |  |
     @Component
15 | 1 |
     public class EntityManagerUtil extends EntityManagerFactoryAccessor {
16 | 1 |
       private static final Logger logger = LoggerFactory.getLogger(EntityManagerUtil.class);
17 |  |
       /**
18 |  |
        * close the entity manager.
19 |  |
        * Use it with caution! This is only intended for use with async request, which Spring won't
20 |  |
        * close the entity manager until the async request is finished.
21 |  |
        */
22 |  |
       public void closeEntityManager() {
23 | 0 |
         EntityManagerHolder emHolder = (EntityManagerHolder)
24 | 0 |
             TransactionSynchronizationManager.getResource(getEntityManagerFactory());
25 | 0 |
         logger.debug("Closing JPA EntityManager in EntityManagerUtil");
26 | 0 |
         EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
27 | 0 |
       }
28 |  |
     }