@Test public void createQueryCacheAndEvictAllThenRetry() throws Exception { List<Author> beforeResults = getAuthorsWithQuery("Author query", "어느나라"); log.warn("#####################################################################"); HibernateEntityManagerFactory entityManagerFactory = (HibernateEntityManagerFactory) EntityTestUtils.getEntityManagerFactory(); org.hibernate.Cache cache = entityManagerFactory.getSessionFactory().getCache(); cache.evictEntityRegions(); cache.evictQueryRegions(); cache.evictDefaultQueryRegion(); cache.evictCollectionRegions(); log.warn("just eviected all."); List<Author> againResults = getAuthorsWithQuery("Author query again after evict all", "어느나라"); assertThat(againResults).isEqualTo(beforeResults); log.warn("#####################################################################"); }
private org.hibernate.stat.QueryStatistics getStatistics(EntityManagerFactory entityManagerFactory, String displayQueryName) { HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); // convert displayed (transformed by QueryNames) query name to original query name to look up query statistics if (sessionFactory != null) { String[] originalQueryNames = sessionFactory.getStatistics().getQueries(); if (originalQueryNames != null) { for (String originalQueryName : originalQueryNames) { if (QueryName.queryName(originalQueryName).getDisplayName().equals(displayQueryName)) { return sessionFactory.getStatistics().getQueryStatistics(originalQueryName); } } } } return null; }
@Override public SessionFactory getObject() { EntityManagerFactory emf = getEntityManagerFactory(); Assert.isInstanceOf(HibernateEntityManagerFactory.class, emf); return ((HibernateEntityManagerFactory) emf).getSessionFactory(); }
@PostConstruct public void findMethods() { for (Object listener : listeners) { findMethodsForListener(listener); } HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) emf; SessionFactory sf = hemf.getSessionFactory(); registry = ((SessionFactoryImpl) sf).getServiceRegistry().getService(EventListenerRegistry.class); }
@PostConstruct public void registerListeners() { HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) emf; SessionFactory sf = hemf.getSessionFactory(); EventListenerRegistry registry = ((SessionFactoryImpl) sf).getServiceRegistry().getService( EventListenerRegistry.class); registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(listener); registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(listener); registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(listener); registry.getEventListenerGroup(EventType.POST_COMMIT_UPDATE).appendListener(listener); registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(listener); registry.getEventListenerGroup(EventType.POST_COMMIT_DELETE).appendListener(listener); registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(listener); }
/** Get JBoss TM out of Hibernate */ public static TransactionManager extractJBossTransactionManager(EntityManagerFactory factory) { if (log.isDebugEnabled()) log.debug("TransactionManager를 꺼냅니다."); SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) ((HibernateEntityManagerFactory) factory).getSessionFactory(); return sessionFactory.getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager(); }
private org.hibernate.stat.Statistics getBaseStatistics(EntityManagerFactory entityManagerFactory) { HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); if (sessionFactory != null) { return sessionFactory.getStatistics(); } return null; }
private CollectionStatistics getStatistics(final EntityManagerFactory entityManagerFactory, String collectionName) { HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); if (sessionFactory != null) { return sessionFactory.getStatistics().getCollectionStatistics(collectionName); } return null; }
private org.hibernate.stat.EntityStatistics getStatistics(EntityManagerFactory entityManagerFactory, String entityName) { HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); if (sessionFactory != null) { return sessionFactory.getStatistics().getEntityStatistics(entityName); } return null; }
static final org.hibernate.stat.Statistics getStatistics(final EntityManagerFactory entityManagerFactory) { HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); if (sessionFactory != null) { return sessionFactory.getStatistics(); } return null; }
org.hibernate.stat.SecondLevelCacheStatistics getStatistics(EntityManagerFactoryAccess entityManagerFactoryaccess, PathAddress pathAddress) { String scopedPersistenceUnitName = pathAddress.getValue(HibernateStatistics.PROVIDER_LABEL); HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactoryaccess.entityManagerFactory(scopedPersistenceUnitName); SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory(); if (sessionFactory != null) { // The entity class name is prefixed by the application scoped persistence unit name return sessionFactory.getStatistics().getSecondLevelCacheStatistics(scopedPersistenceUnitName + "." + pathAddress.getValue(HibernateStatistics.ENTITYCACHE)); } return null; }
public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl() { EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory; assertTrue(emfi.getNativeEntityManagerFactory() instanceof HibernateEntityManagerFactory); }
@PostConstruct public void registerListeners() { // get registry so we can add listeners. HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) entityManagerFactory; SessionFactory sf = hemf.getSessionFactory(); EventListenerRegistry registry = ((SessionFactoryImpl) sf).getServiceRegistry().getService( EventListenerRegistry.class); final Set<Object> listeners = new HashSet<Object>(); EntityManager entityManager = null; try { entityManager = hemf.createEntityManager(); // for every entity known to the system... for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) { // ... register event listeners for it. if (entity.getJavaType().isAnnotationPresent(SpringEntityListeners.class)) { SpringEntityListeners annotation = (SpringEntityListeners) entity.getJavaType().getAnnotation( SpringEntityListeners.class); for (Class<?> beanClass : annotation.value()) { Map<String, ?> map = context.getBeansOfType(beanClass); listeners.addAll(map.values()); } } } } finally { if (entityManager != null) { entityManager.close(); } } // register adapter and listeners. HibernateEntityListenersAdapter adapter = new HibernateEntityListenersAdapter(new ArrayList<Object>(listeners), entityManagerFactory); registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(adapter); registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(adapter); registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(adapter); registry.getEventListenerGroup(EventType.POST_COMMIT_UPDATE).appendListener(adapter); registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(adapter); registry.getEventListenerGroup(EventType.POST_COMMIT_DELETE).appendListener(adapter); registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(adapter); }
public SessionFactory getObject() { EntityManagerFactory emf = getEntityManagerFactory(); Assert.isInstanceOf(HibernateEntityManagerFactory.class, emf); return ((HibernateEntityManagerFactory) emf).getSessionFactory(); }
@Override public Class<? extends EntityManagerFactory> getEntityManagerFactoryInterface() { return HibernateEntityManagerFactory.class; }
public static void dropSchemaAndDatabase(EntityManagerFactory emf) { dropSchemaAndDatabase(((HibernateEntityManagerFactory) emf).getSessionFactory()); }
/** * Returns an instance of the current session. * * @return Instance of the current session */ public static Session getCurrentSession() { SessionFactory sessionFactory = ((HibernateEntityManagerFactory) EMF.get()).getSessionFactory(); return sessionFactory.getCurrentSession(); }