Search in sources :

Example 11 with Contact

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

the class ConcurrentWriteTest method addContact.

/**
	 * -load existing Customer -create a new Contact and add to customer's contacts
	 *
	 * @param customerId
	 * @return added Contact
	 */
private Contact addContact(Integer customerId) throws Exception {
    assert customerId != null;
    return withTxSessionApply(s -> {
        final Customer customer = s.load(Customer.class, customerId);
        Contact contact = new Contact();
        contact.setName("contact name");
        contact.setTlf("wtf is tlf?");
        contact.setCustomer(customer);
        customer.getContacts().add(contact);
        if (TERMINATE_ALL_USERS) {
            markRollbackOnly(s);
        }
        return contact;
    });
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 12 with Contact

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

the class ConcurrentWriteTest method removeContact.

/**
	 * remove existing 'contact' from customer's list of contacts
	 *
	 * @param customerId
	 * @throws IllegalStateException
	 *            if customer does not own a contact
	 */
private void removeContact(Integer customerId) throws Exception {
    assert customerId != null;
    withTxSession(s -> {
        Customer customer = s.load(Customer.class, customerId);
        Set<Contact> contacts = customer.getContacts();
        if (contacts.size() != 1) {
            throw new IllegalStateException("can't remove contact: customer id=" + customerId + " expected exactly 1 contact, " + "actual count=" + contacts.size());
        }
        Contact contact = contacts.iterator().next();
        s.lock(contact, LockMode.PESSIMISTIC_WRITE);
        contacts.remove(contact);
        contact.setCustomer(null);
        if (TERMINATE_ALL_USERS) {
            markRollbackOnly(s);
        }
    });
}
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