Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.foo.bar;
- import org.hibernate.jpa.internal.EntityManagerFactoryAdapter;
- import javax.annotation.Nonnull;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import java.sql.Connection;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * <p>
- * Holds one {@link EntityManager} for every Thread to allow lazy-loading.
- * </p>
- * <p>
- * This is an implementation of the <em>persistence context per conversation</em> strategy. See
- * "Java Persistence with Hibernate, Second Edition" (ISBN 9781617290459), chapter 11 for details.
- *
- * </p>
- * @see <a href="http://stackoverflow.com/questions/24060782/jpa-2-hibernate-persistence-context-per-conversation-closed-connection">JPA 2 (Hibernate) persistence context per conversation - closed connection</a>
- * @see <a href="http://www.manning.com/bauer3/">Java Persistence with Hibernate, Second Edition</a>
- */
- public class EntityManagerHolder {
- private static final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();
- private static EntityManagerFactoryAdapter entityManagerFactoryAdapter;
- /**
- * @return The {@link EntityManager} linked to this thread
- */
- public static EntityManager getEntityManager() {
- return entityManager.get();
- }
- /**
- * If the invoking thread does not have an associated {@link EntityManager} yet,
- * creates a new one and associates it with the thread. Otherwise, reconnects
- * the existing EntityManager to a JDBC connection (if the connection has been closed).
- *
- * @param connection An active JDBC connection to use for the EntityManager
- */
- public static synchronized void initializeEntityManager(@Nonnull Connection connection) {
- EntityManager currentEntityManager = entityManager.get();
- if (currentEntityManager == null || !currentEntityManager.isOpen()) {
- if (entityManagerFactoryAdapter == null) {
- // First invocation ever; create a factory to produce EntityManagers from a JDBC connection
- EntityManagerFactory entityManagerFactory =
- Persistence.createEntityManagerFactory("myPersistenceUnit");
- entityManagerFactoryAdapter = new EntityManagerFactoryAdapter(entityManagerFactory);
- }
- // Start the conversation by creating the EntityManager for this thread
- EntityManager newEntityManager = entityManagerFactoryAdapter.createEntityManager(connection);
- entityManager.set(newEntityManager);
- } else {
- // Continuing conversation; ensure the connection of the EntityManager is alive
- entityManagerFactoryAdapter.reconnect(currentEntityManager, connection);
- }
- }
- /**
- * Conversation has been finished; close the EntityManager linked to this thread.
- */
- public static synchronized void closeEntityManager() {
- if (entityManager.get() != null && entityManager.get().isOpen()) {
- entityManager.get().close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement