/** * Look up the EntityManagerFactory that this filter should use. * <p>The default implementation looks for a bean with the specified name * in Spring's root application context. * @return the EntityManagerFactory to use * @see #getEntityManagerFactoryBeanName */ protected EntityManagerFactory lookupEntityManagerFactory() { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); String emfBeanName = getEntityManagerFactoryBeanName(); String puName = getPersistenceUnitName(); if (StringUtils.hasLength(emfBeanName)) { return wac.getBean(emfBeanName, EntityManagerFactory.class); } else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) { return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class); } else { // Includes fallback search for single EntityManagerFactory bean by type. return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName); } }
@Override public void beforeCommit() { if (flush) { EntityManagerFactoryUtils.getTransactionalEntityManager(emf).flush(); } final MutableEntityDataChangeSet cs = preChangeset.get(); if (! cs.isEmpty()) { lazySetId(cs); for (EntityChangeSetListener listener : entityChangeSetListeners) { listener.preDataChanged(MutableEntityDataChangeSet.clone(cs)); } } preChangeset.remove(); }
@Override public String[] extractIdPropertyNames(Object entity) { final EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf); final ClassDescriptor desc = em.unwrap(JpaEntityManager.class).getServerSession().getClassDescriptor(entity); if (desc != null) { final Collection<DatabaseMapping> fieldNames = desc.getMappings(); final List<DatabaseMapping> tmp = new LinkedList<>(); for (DatabaseMapping m : fieldNames) { if (m.isPrimaryKeyMapping()) { tmp.add(m); } } final String[] retVal = new String[tmp.size()]; for (int i = 0; i < retVal.length; i++) { retVal[i] = tmp.get(i).getAttributeName(); } return retVal; } return null; }
/** * 获得EntityManager * * @return */ protected final EntityManager getEntityManager() { TransactionMode tx = jefEmf.getDefault().getTxType(); EntityManager em; switch (tx) { case JPA: case JTA: em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(entityManagerFactory, null); if (em == null) { // 当无事务时。Spring返回null em = entityManagerFactory.createEntityManager(null, Collections.EMPTY_MAP); } break; case JDBC: ConnectionHolder conn = (ConnectionHolder) TransactionSynchronizationManager.getResource(jefEmf.getDefault().getDataSource()); if (conn == null) {// 基于数据源的Spring事务 em = entityManagerFactory.createEntityManager(null, Collections.EMPTY_MAP); } else { ManagedTransactionImpl session = new ManagedTransactionImpl(jefEmf.getDefault(), conn.getConnection()); em = new JefEntityManager(entityManagerFactory, null, session); } break; default: throw new UnsupportedOperationException(tx.name()); } return em; }
/** * 获得EntityManager * * @return */ public static final EntityManager getEntityManager(JefEntityManagerFactory jefEmf) { TransactionMode tx = jefEmf.getDefault().getTxType(); EntityManager em; switch (tx) { case JPA: case JTA: em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(jefEmf, null); if (em == null) { // 当无事务时。Spring返回null em = jefEmf.createEntityManager(null, Collections.EMPTY_MAP); } break; case JDBC: ConnectionHolder conn = (ConnectionHolder) TransactionSynchronizationManager.getResource(jefEmf.getDefault().getDataSource()); if (conn == null) {// 基于数据源的Spring事务 em = jefEmf.createEntityManager(null, Collections.EMPTY_MAP); } else { ManagedTransactionImpl session = new ManagedTransactionImpl(jefEmf.getDefault(), conn.getConnection()); em = new JefEntityManager(jefEmf, null, session); } break; default: throw new UnsupportedOperationException(tx.name()); } return em; }
private EntityManager getEntityManager() { EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf); if (em == null) { return emf.createEntityManager(); } return em; }
protected void applyQueryImpl(Query query) { EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf); if (em == null) { entityManager = emf.createEntityManager(); em = entityManager; } FullTextSession fullTextSession = Search.getFullTextSession(em.unwrap(Session.class)); fullTextQuery = fullTextSession.createFullTextQuery(query, entityClass); }
@Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(getEntityManagerFactory()); logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } }
/** * Find an EntityManagerFactory with the given name in the current * Spring application context. * @param unitName the name of the persistence unit (never empty) * @param requestingBeanName the name of the requesting bean * @return the EntityManagerFactory * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context */ protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, String requestingBeanName) throws NoSuchBeanDefinitionException { EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); if (this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; }
@Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (ex instanceof HibernateException) { return convertHibernateAccessException((HibernateException) ex); } if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) { return convertHibernateAccessException((HibernateException) ex.getCause()); } return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); }
/** * close the entity manager. * Use it with caution! This is only intended for use with async request, which Spring won't * close the entity manager until the async request is finished. */ public void closeEntityManager() { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(getEntityManagerFactory()); if (emHolder == null) { return; } logger.debug("Closing JPA EntityManager in EntityManagerUtil"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); }
/** * Return the underlying {@link EntityManager} that's actually used to perform all * operations. * @return the entity manager */ public final EntityManager getEntityManager() { EntityManager manager = EntityManagerFactoryUtils .getTransactionalEntityManager(this.entityManagerFactory); Assert.state(manager != null, "No transactional EntityManager found"); return manager; }
@Override public Session openSession() { EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); if (entityManager == null) { return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager); } return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false); }
@Override public Session openSession(CommandContext commandContext) { EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); if (entityManager == null) { return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager); } return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false); }
@Override public EntityManager getEntityManager(String store) { if (!TransactionSynchronizationManager.isActualTransactionActive()) throw new IllegalStateException("No active transaction"); EntityManagerFactory emf; if (Stores.isMain(store)) emf = this.jpaEmf; else emf = AppBeans.get("entityManagerFactory_" + store); javax.persistence.EntityManager jpaEm = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null); if (!jpaEm.isJoinedToTransaction()) throw new IllegalStateException("No active transaction for " + store + " database"); EntityManager entityManager = createEntityManager(jpaEm); EntityManagerContext ctx = contextHolder.get(store); if (ctx != null) { entityManager.setSoftDeletion(ctx.isSoftDeletion()); } else { ctx = new EntityManagerContext(); ctx.setSoftDeletion(isSoftDeletion()); contextHolder.set(ctx, store); entityManager.setSoftDeletion(isSoftDeletion()); } EntityManager emProxy = (EntityManager) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{EntityManager.class}, new EntityManagerInvocationHandler(entityManager, store) ); return emProxy; }
/** * Suppression du EntityManager. */ public void closeEntityManager() { if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(entityManagerFactory); EntityManagerFactoryUtils.closeEntityManager(entityManagerHolder.getEntityManager()); } }
public Session openSession() { EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); if (entityManager == null) { return new EntityManagerSessionImpl(entityManagerFactory, handleTransactions, closeEntityManager); } return new EntityManagerSessionImpl(entityManagerFactory, entityManager, false, false); }
protected Session getSession() { EntityManager em = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null); if (em == null) { // 当无事务时。Spring返回null em = emf.createEntityManager(null, Collections.EMPTY_MAP); } if (em instanceof JefEntityManager) { return ((JefEntityManager) em).getSession(); } throw new IllegalArgumentException(em.getClass().getName()); }
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(getEntityManagerFactory()); logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } }
@Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (ex instanceof HibernateException) { return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex); } if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) { return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause()); } return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); }
/** * Return the Entity Manager * * @return The Entity Manager */ public EntityManager getEM( ) { EntityManagerFactory emf = getEntityManagerFactory( ); if ( TransactionSynchronizationManager.isSynchronizationActive( ) ) { // first, get Spring entitymanager (if available) try { EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager( emf ); if ( em == null ) { LOG.error( "getEM( ) : no EntityManager found. Will use native entity manager factory [Transaction will not be supported]" ); } else { LOG.debug( "EntityManager found for the current transaction : " + em.toString( ) + " - using Factory : " + emf.toString( ) ); return em; } } catch( DataAccessResourceFailureException ex ) { LOG.error( ex ); } } LOG.error( "getEM( ) : no EntityManager found. Will use native entity manager factory [Transaction will not be supported]" ); if ( _defaultEM == null ) { _defaultEM = emf.createEntityManager( ); } return _defaultEM; }
@Override public void execute() throws Exception { EntityManager em = emf.createEntityManager(); EntityManagerHolder emHolder = new EntityManagerHolder(em); try { TransactionSynchronizationManager.bindResource(emf, emHolder); super.execute(); } finally { EntityManagerHolder emHolderB = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf); log.debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter"); EntityManagerFactoryUtils.closeEntityManager(emHolderB.getEntityManager()); } sleep(DateTry.Second*10); }
@Before public void setUp() { TransactionSynchronizationManager.initSynchronization(); em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf); }
@Override public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException { EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean); EntityManagerFactoryUtils.closeEntityManager(emToClose); }
private void closeAfterTimeout() { if (this.timeoutInProgress) { logger.debug("Closing JPA EntityManager after async request timeout"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } }
protected EntityManager doGetPersistenceContext(Object testObject) { return EntityManagerFactoryUtils.getTransactionalEntityManager(getPersistenceUnit(testObject)); }
@Before public void initializeFixture() { person = new Person(1L, "johnDoe"); entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); }