| 1 | |
package com.ctrip.framework.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 | |
|
| 13 | |
|
| 14 | |
@Component |
| 15 | 1 | public class EntityManagerUtil extends EntityManagerFactoryAccessor { |
| 16 | 1 | private static final Logger logger = LoggerFactory.getLogger(EntityManagerUtil.class); |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
public void closeEntityManager() { |
| 23 | 0 | EntityManagerHolder emHolder = (EntityManagerHolder) |
| 24 | 0 | TransactionSynchronizationManager.getResource(getEntityManagerFactory()); |
| 25 | 0 | if (emHolder == null) { |
| 26 | 0 | return; |
| 27 | |
} |
| 28 | 0 | logger.debug("Closing JPA EntityManager in EntityManagerUtil"); |
| 29 | 0 | EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); |
| 30 | 0 | } |
| 31 | |
} |