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));
}
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;
});
}
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);
}
}
}
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");
}
}
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;
});
}
Aggregations