Search in sources :

Example 1 with Contact

use of org.hibernate.test.cache.infinispan.functional.entities.Contact in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method doGetCustomer.

private Customer doGetCustomer(Integer id, Session session) throws Exception {
    Customer customer = session.get(Customer.class, id);
    if (customer == null) {
        return null;
    }
    // Access all the contacts
    Set<Contact> contacts = customer.getContacts();
    if (contacts != null) {
        for (Iterator it = contacts.iterator(); it.hasNext(); ) {
            ((Contact) it.next()).getName();
        }
    }
    return customer;
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Iterator(java.util.Iterator) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 2 with Contact

use of org.hibernate.test.cache.infinispan.functional.entities.Contact in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method cleanup.

private void cleanup(SessionFactory sessionFactory) throws Exception {
    withTxSession(sessionFactory, session -> {
        Customer c = (Customer) session.get(Customer.class, CUSTOMER_ID);
        if (c != null) {
            Set contacts = c.getContacts();
            for (Iterator it = contacts.iterator(); it.hasNext(); ) {
                session.delete(it.next());
            }
            c.setContacts(null);
            session.delete(c);
        }
        for (Object contact : session.createCriteria(Contact.class).list()) {
            session.delete(contact);
        }
    });
}
Also used : ConcurrentSet(org.jboss.util.collection.ConcurrentSet) HashSet(java.util.HashSet) Set(java.util.Set) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Iterator(java.util.Iterator) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 3 with Contact

use of org.hibernate.test.cache.infinispan.functional.entities.Contact in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method createCustomer.

private IdContainer createCustomer(SessionFactory sessionFactory) throws Exception {
    log.debug("CREATE CUSTOMER");
    Customer customer = new Customer();
    customer.setName("JBoss");
    Set<Contact> contacts = new HashSet<Contact>();
    Contact kabir = new Contact();
    kabir.setCustomer(customer);
    kabir.setName("Kabir");
    kabir.setTlf("1111");
    contacts.add(kabir);
    Contact bill = new Contact();
    bill.setCustomer(customer);
    bill.setName("Bill");
    bill.setTlf("2222");
    contacts.add(bill);
    customer.setContacts(contacts);
    ArrayList<Runnable> cleanup = new ArrayList<>();
    CountDownLatch customerLatch = new CountDownLatch(1);
    CountDownLatch collectionLatch = new CountDownLatch(1);
    CountDownLatch contactsLatch = new CountDownLatch(2);
    if (cacheMode.isInvalidation()) {
        cleanup.add(mockValidator(remoteCustomerCache, customerLatch));
        cleanup.add(mockValidator(remoteCollectionCache, collectionLatch));
        cleanup.add(mockValidator(remoteContactCache, contactsLatch));
    } else if (accessType == AccessType.NONSTRICT_READ_WRITE) {
        // ATM nonstrict mode has sync after-invalidation update
        Stream.of(customerLatch, collectionLatch, contactsLatch, contactsLatch).forEach(l -> l.countDown());
    } else {
        ExpectingInterceptor.get(remoteCustomerCache).when(this::isFutureUpdate).countDown(collectionLatch);
        ExpectingInterceptor.get(remoteCollectionCache).when(this::isFutureUpdate).countDown(customerLatch);
        ExpectingInterceptor.get(remoteContactCache).when(this::isFutureUpdate).countDown(contactsLatch);
        cleanup.add(() -> ExpectingInterceptor.cleanup(remoteCustomerCache, remoteCollectionCache, remoteContactCache));
    }
    withTxSession(sessionFactory, session -> session.save(customer));
    assertTrue(customerLatch.await(2, TimeUnit.SECONDS));
    assertTrue(collectionLatch.await(2, TimeUnit.SECONDS));
    assertTrue(contactsLatch.await(2, TimeUnit.SECONDS));
    cleanup.forEach(Runnable::run);
    IdContainer ids = new IdContainer();
    ids.customerId = customer.getId();
    Set contactIds = new HashSet();
    contactIds.add(kabir.getId());
    contactIds.add(bill.getId());
    ids.contactIds = contactIds;
    log.debug("CREATE CUSTOMER -  END");
    return ids;
}
Also used : ConcurrentSet(org.jboss.util.collection.ConcurrentSet) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) CacheEntryVisitedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent) TimeoutException(java.util.concurrent.TimeoutException) Session(org.hibernate.Session) Cache(org.infinispan.Cache) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) TestForIssue(org.hibernate.testing.TestForIssue) InvocationContext(org.infinispan.context.InvocationContext) CacheEntryVisited(org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited) AdvancedCache(org.infinispan.AdvancedCache) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) AccessType(org.hibernate.cache.spi.access.AccessType) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) Listener(org.infinispan.notifications.Listener) Iterator(java.util.Iterator) SessionFactory(org.hibernate.SessionFactory) Util(org.infinispan.commons.util.Util) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) Phaser(java.util.concurrent.Phaser) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) Assert.assertEquals(org.junit.Assert.assertEquals) ConcurrentSet(org.jboss.util.collection.ConcurrentSet) HashSet(java.util.HashSet) Set(java.util.Set) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 4 with Contact

use of org.hibernate.test.cache.infinispan.functional.entities.Contact in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method readEveryonesFirstContact.

/**
	 * read first contact of every Customer participating in this test. this forces concurrent cache
	 * writes of Customer.contacts Collection cache node
	 *
	 * @return who cares
	 * @throws java.lang.Exception
	 */
private void readEveryonesFirstContact() throws Exception {
    withTxSession(s -> {
        for (Integer customerId : getCustomerIDs()) {
            if (TERMINATE_ALL_USERS) {
                markRollbackOnly(s);
                return;
            }
            Customer customer = s.load(Customer.class, customerId);
            Set<Contact> contacts = customer.getContacts();
            if (!contacts.isEmpty()) {
                contacts.iterator().next();
            }
        }
    });
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 5 with Contact

use of org.hibernate.test.cache.infinispan.functional.entities.Contact in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method getFirstContact.

/**
	 * -load existing Customer -get customer's contacts; return 1st one
	 *
	 * @param customerId
	 * @return first Contact or null if customer has none
	 */
private Contact getFirstContact(Integer customerId) throws Exception {
    assert customerId != null;
    return withTxSessionApply(s -> {
        Customer customer = s.load(Customer.class, customerId);
        Set<Contact> contacts = customer.getContacts();
        Contact firstContact = contacts.isEmpty() ? null : contacts.iterator().next();
        if (TERMINATE_ALL_USERS) {
            markRollbackOnly(s);
        }
        return firstContact;
    });
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Aggregations

Contact (org.hibernate.test.cache.infinispan.functional.entities.Contact)12 Customer (org.hibernate.test.cache.infinispan.functional.entities.Customer)11 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)3 Set (java.util.Set)3 ConcurrentSet (org.jboss.util.collection.ConcurrentSet)3 Test (org.junit.Test)3 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Phaser (java.util.concurrent.Phaser)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiPredicate (java.util.function.BiPredicate)1 Stream (java.util.stream.Stream)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1