private void checkEntityManagerFactory() { if (! getEntityManagerFactory().isOpen()) { open = false; } } }
/** * Executes some DB work that require a DataSource instance. * <p> * If we cannot get a DataSource from the connection provider, we'll just provide a * dummy DataSource that returns the connection extracted from the EntityManager as * in doWork(Work). * @param work DsWork callback interface */ public void doWork(DsWork work) { // Flush the EntityManager to make sure we have all entities available //this.em.flush(); HibernateEntityManagerFactory factory = (HibernateEntityManagerFactory) this.em.unwrap(EntityManagerImpl.class).getEntityManagerFactory(); SessionFactoryImpl sessionFactory = (SessionFactoryImpl) factory.getSessionFactory(); ConnectionProvider connProvider = sessionFactory.getConnectionProvider(); DataSource ds = null; if (connProvider instanceof DatasourceConnectionProviderImpl) { ds = ((DatasourceConnectionProviderImpl) connProvider).getDataSource(); logger.debug("Using actual DataSource to execute DataSource work: " + ds); } else { ds = new ConnectionProviderDataSource(connProvider); logger.debug("Using ConnectionProviderDataSource to execute DataSource work."); } work.execute(ds); }
@Override protected Session getRawSession() { if ( session == null ) { SessionBuilder sessionBuilder = getEntityManagerFactory().getSessionFactory().withOptions(); if (sessionInterceptorClass != null) { try { Interceptor interceptor = (Interceptor) sessionInterceptorClass.newInstance(); sessionBuilder.interceptor( interceptor ); } catch (InstantiationException e) { throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e); } catch (IllegalAccessException e) { throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e); } catch (ClassCastException e) { throw new PersistenceException("Session interceptor does not implement Interceptor: " + sessionInterceptorClass, e); } } sessionBuilder.autoJoinTransactions( getTransactionType() != PersistenceUnitTransactionType.JTA ); session = sessionBuilder.openSession(); if ( persistenceContextType == PersistenceContextType.TRANSACTION ) { ( (SessionImplementor) session ).setAutoClear( true ); } } return session; }