Search in sources :

Example 6 with Contact

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

the class ConcurrentWriteTest method testSingleUser.

@Test
public void testSingleUser() throws Exception {
    // setup
    sessionFactory().getStatistics().clear();
    // wait a while to make sure that timestamp comparison works after invalidateRegion
    TIME_SERVICE.advance(1);
    Customer customer = createCustomer(0);
    final Integer customerId = customer.getId();
    getCustomerIDs().add(customerId);
    // wait a while to make sure that timestamp comparison works after collection remove (during insert)
    TIME_SERVICE.advance(1);
    assertNull("contact exists despite not being added", getFirstContact(customerId));
    // check that cache was hit
    SecondLevelCacheStatistics customerSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName());
    assertEquals(1, customerSlcs.getPutCount());
    assertEquals(1, customerSlcs.getElementCountInMemory());
    assertEquals(1, customerSlcs.getEntries().size());
    log.infof("Add contact to customer {0}", customerId);
    SecondLevelCacheStatistics contactsCollectionSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName() + ".contacts");
    assertEquals(1, contactsCollectionSlcs.getPutCount());
    assertEquals(1, contactsCollectionSlcs.getElementCountInMemory());
    assertEquals(1, contactsCollectionSlcs.getEntries().size());
    final Contact contact = addContact(customerId);
    assertNotNull("contact returned by addContact is null", contact);
    assertEquals("Customer.contacts cache was not invalidated after addContact", 0, contactsCollectionSlcs.getElementCountInMemory());
    assertNotNull("Contact missing after successful add call", getFirstContact(customerId));
    // read everyone's contacts
    readEveryonesFirstContact();
    removeContact(customerId);
    assertNull("contact still exists after successful remove call", getFirstContact(customerId));
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) Test(org.junit.Test)

Example 7 with Contact

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

the class EntityCollectionInvalidationTest method modifyCustomer.

private IdContainer modifyCustomer(Integer id, SessionFactory sessionFactory) throws Exception {
    log.debug("Modify customer with id=" + id);
    return withTxSessionApply(sessionFactory, session -> {
        IdContainer ids = new IdContainer();
        Set contactIds = new HashSet();
        Customer customer = doGetCustomer(id, session);
        customer.setName("NewJBoss");
        ids.customerId = customer.getId();
        Set<Contact> contacts = customer.getContacts();
        for (Contact c : contacts) {
            contactIds.add(c.getId());
        }
        Contact contact = contacts.iterator().next();
        contacts.remove(contact);
        contactIds.remove(contact.getId());
        ids.contactIds = contactIds;
        contact.setCustomer(null);
        session.save(customer);
        return ids;
    });
}
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) HashSet(java.util.HashSet) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 8 with Contact

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

the class BulkOperationsTest method testBulkOperations.

@Test
public void testBulkOperations() throws Throwable {
    boolean cleanedUp = false;
    try {
        createContacts();
        List<Integer> rhContacts = getContactsByCustomer("Red Hat");
        assertNotNull("Red Hat contacts exist", rhContacts);
        assertEquals("Created expected number of Red Hat contacts", 10, rhContacts.size());
        SecondLevelCacheStatistics contactSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Contact.class.getName());
        assertEquals(20, contactSlcs.getElementCountInMemory());
        assertEquals("Deleted all Red Hat contacts", 10, deleteContacts());
        assertEquals(0, contactSlcs.getElementCountInMemory());
        List<Integer> jbContacts = getContactsByCustomer("JBoss");
        assertNotNull("JBoss contacts exist", jbContacts);
        assertEquals("JBoss contacts remain", 10, jbContacts.size());
        for (Integer id : rhContacts) {
            assertNull("Red Hat contact " + id + " cannot be retrieved", getContact(id));
        }
        rhContacts = getContactsByCustomer("Red Hat");
        if (rhContacts != null) {
            assertEquals("No Red Hat contacts remain", 0, rhContacts.size());
        }
        updateContacts("Kabir", "Updated");
        assertEquals(0, contactSlcs.getElementCountInMemory());
        for (Integer id : jbContacts) {
            Contact contact = getContact(id);
            assertNotNull("JBoss contact " + id + " exists", contact);
            String expected = ("Kabir".equals(contact.getName())) ? "Updated" : "2222";
            assertEquals("JBoss contact " + id + " has correct TLF", expected, contact.getTlf());
        }
        List<Integer> updated = getContactsByTLF("Updated");
        assertNotNull("Got updated contacts", updated);
        assertEquals("Updated contacts", 5, updated.size());
        assertEquals(10, contactSlcs.getElementCountInMemory());
        updateContactsWithOneManual("Kabir", "UpdatedAgain");
        assertEquals(0, contactSlcs.getElementCountInMemory());
        for (Integer id : jbContacts) {
            Contact contact = getContact(id);
            assertNotNull("JBoss contact " + id + " exists", contact);
            String expected = ("Kabir".equals(contact.getName())) ? "UpdatedAgain" : "2222";
            assertEquals("JBoss contact " + id + " has correct TLF", expected, contact.getTlf());
        }
        updated = getContactsByTLF("UpdatedAgain");
        assertNotNull("Got updated contacts", updated);
        assertEquals("Updated contacts", 5, updated.size());
    } catch (Throwable t) {
        cleanedUp = true;
        cleanup(true);
        throw t;
    } finally {
        // cleanup the db so we can run this test multiple times w/o restarting the cluster
        if (!cleanedUp) {
            cleanup(false);
        }
    }
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) Test(org.junit.Test)

Example 9 with Contact

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

the class BulkOperationsTest method createCustomer.

private Customer createCustomer(int id) throws Exception {
    System.out.println("CREATE CUSTOMER " + id);
    try {
        Customer customer = new Customer();
        customer.setName((id % 2 == 0) ? "JBoss" : "Red Hat");
        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);
        return customer;
    } finally {
        System.out.println("CREATE CUSTOMER " + id + " -  END");
    }
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) HashSet(java.util.HashSet) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 10 with Contact

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

the class ConcurrentWriteTest method createCustomer.

private Customer createCustomer(int nameSuffix) throws Exception {
    return withTxSessionApply(s -> {
        Customer customer = new Customer();
        customer.setName("customer_" + nameSuffix);
        customer.setContacts(new HashSet<Contact>());
        s.persist(customer);
        return customer;
    });
}
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