|
Hibernate example source code file (EntityCollectionInvalidationTestCase.java)
The Hibernate EntityCollectionInvalidationTestCase.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2009-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.test.cache.infinispan.functional.cluster; import javax.transaction.TransactionManager; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.infinispan.Cache; import org.infinispan.manager.CacheContainer; import org.infinispan.notifications.Listener; import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited; import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; import org.jboss.util.collection.ConcurrentSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.infinispan.util.CacheHelper; import org.junit.Test; import org.hibernate.test.cache.infinispan.functional.Contact; import org.hibernate.test.cache.infinispan.functional.Customer; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * EntityCollectionInvalidationTestCase. * * @author Galder ZamarreƱo * @since 3.5 */ public class EntityCollectionInvalidationTestCase extends DualNodeTestCase { private static final Log log = LogFactory.getLog( EntityCollectionInvalidationTestCase.class ); private static final long SLEEP_TIME = 50l; private static final Integer CUSTOMER_ID = new Integer( 1 ); static int test = 0; @Test public void testAll() throws Exception { log.info( "*** testAll()" ); // Bind a listener to the "local" cache // Our region factory makes its CacheManager available to us CacheContainer localManager = ClusterAwareRegionFactory.getCacheManager( DualNodeTestCase.LOCAL ); // Cache localCache = localManager.getCache("entity"); Cache localCustomerCache = localManager.getCache( Customer.class.getName() ); Cache localContactCache = localManager.getCache( Contact.class.getName() ); Cache localCollectionCache = localManager.getCache( Customer.class.getName() + ".contacts" ); MyListener localListener = new MyListener( "local" ); localCustomerCache.addListener( localListener ); localContactCache.addListener( localListener ); localCollectionCache.addListener( localListener ); TransactionManager localTM = DualNodeJtaTransactionManagerImpl.getInstance( DualNodeTestCase.LOCAL ); // Bind a listener to the "remote" cache CacheContainer remoteManager = ClusterAwareRegionFactory.getCacheManager( DualNodeTestCase.REMOTE ); Cache remoteCustomerCache = remoteManager.getCache( Customer.class.getName() ); Cache remoteContactCache = remoteManager.getCache( Contact.class.getName() ); Cache remoteCollectionCache = remoteManager.getCache( Customer.class.getName() + ".contacts" ); MyListener remoteListener = new MyListener( "remote" ); remoteCustomerCache.addListener( remoteListener ); remoteContactCache.addListener( remoteListener ); remoteCollectionCache.addListener( remoteListener ); TransactionManager remoteTM = DualNodeJtaTransactionManagerImpl.getInstance( DualNodeTestCase.REMOTE ); SessionFactory localFactory = sessionFactory(); SessionFactory remoteFactory = secondNodeEnvironment().getSessionFactory(); try { assertTrue( remoteListener.isEmpty() ); assertTrue( localListener.isEmpty() ); log.debug( "Create node 0" ); IdContainer ids = createCustomer( localFactory, localTM ); assertTrue( remoteListener.isEmpty() ); assertTrue( localListener.isEmpty() ); // Sleep a bit to let async commit propagate. Really just to // help keep the logs organized for debugging any issues sleep( SLEEP_TIME ); log.debug( "Find node 0" ); // This actually brings the collection into the cache getCustomer( ids.customerId, localFactory, localTM ); sleep( SLEEP_TIME ); // Now the collection is in the cache so, the 2nd "get" // should read everything from the cache log.debug( "Find(2) node 0" ); localListener.clear(); getCustomer( ids.customerId, localFactory, localTM ); // Check the read came from the cache log.debug( "Check cache 0" ); assertLoadedFromCache( localListener, ids.customerId, ids.contactIds ); log.debug( "Find node 1" ); // This actually brings the collection into the cache since invalidation is in use getCustomer( ids.customerId, remoteFactory, remoteTM ); // Now the collection is in the cache so, the 2nd "get" // should read everything from the cache log.debug( "Find(2) node 1" ); remoteListener.clear(); getCustomer( ids.customerId, remoteFactory, remoteTM ); // Check the read came from the cache log.debug( "Check cache 1" ); assertLoadedFromCache( remoteListener, ids.customerId, ids.contactIds ); // Modify customer in remote remoteListener.clear(); ids = modifyCustomer( ids.customerId, remoteFactory, remoteTM ); sleep( 250 ); assertLoadedFromCache( remoteListener, ids.customerId, ids.contactIds ); // After modification, local cache should have been invalidated and hence should be empty assertEquals( 0, getValidKeyCount( localCollectionCache.keySet() ) ); assertEquals( 0, getValidKeyCount( localCustomerCache.keySet() ) ); } catch (Exception e) { log.error( "Error", e ); throw e; } finally { // cleanup the db log.debug( "Cleaning up" ); cleanup( localFactory, localTM ); } } private IdContainer createCustomer(SessionFactory sessionFactory, TransactionManager tm) throws Exception { log.debug( "CREATE CUSTOMER" ); tm.begin(); try { Session session = sessionFactory.getCurrentSession(); Customer customer = new Customer(); customer.setName( "JBoss" ); Set<Contact> contacts = new HashSet Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate EntityCollectionInvalidationTestCase.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.