Search in sources :

Example 11 with TestData

use of org.jpwh.shared.util.TestData in project microservices by pwillhan.

the class EagerSelect method storeTestData.

public FetchTestData storeTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] itemIds = new Long[3];
    Long[] userIds = new Long[3];
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    userIds[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    userIds[1] = janeroe.getId();
    User robertdoe = new User("robertdoe");
    em.persist(robertdoe);
    userIds[2] = robertdoe.getId();
    Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[1] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, janeroe, new BigDecimal(2 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
    em.persist(item);
    itemIds[2] = item.getId();
    tx.commit();
    em.close();
    FetchTestData testData = new FetchTestData();
    testData.items = new TestData(itemIds);
    testData.users = new TestData(userIds);
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.eagerselect.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.fetching.eagerselect.User) TestData(org.jpwh.shared.util.TestData) Bid(org.jpwh.model.fetching.eagerselect.Bid) BigDecimal(java.math.BigDecimal)

Example 12 with TestData

use of org.jpwh.shared.util.TestData in project microservices by pwillhan.

the class LazyInterception method storeTestData.

public FetchTestData storeTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] itemIds = new Long[3];
    Long[] userIds = new Long[3];
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    userIds[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    userIds[1] = janeroe.getId();
    User robertdoe = new User("robertdoe");
    em.persist(robertdoe);
    userIds[2] = robertdoe.getId();
    Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe, "Some description.");
    em.persist(item);
    itemIds[0] = item.getId();
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe, "Some description.");
    em.persist(item);
    itemIds[1] = item.getId();
    item = new Item("Item Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe, "Some description.");
    em.persist(item);
    itemIds[2] = item.getId();
    tx.commit();
    em.close();
    FetchTestData testData = new FetchTestData();
    testData.items = new TestData(itemIds);
    testData.users = new TestData(userIds);
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.fetching.interception.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.fetching.interception.User) TestData(org.jpwh.shared.util.TestData)

Example 13 with TestData

use of org.jpwh.shared.util.TestData in project microservices by pwillhan.

the class Versioning method storeItemAndBids.

public TestData storeItemAndBids() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] ids = new Long[1];
    Item item = new Item("Some Item");
    em.persist(item);
    ids[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(new BigDecimal(10 + i), item);
        em.persist(bid);
    }
    tx.commit();
    em.close();
    return new TestData(ids);
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.concurrency.version.Item) EntityManager(javax.persistence.EntityManager) TestData(org.jpwh.shared.util.TestData) Bid(org.jpwh.model.concurrency.version.Bid) BigDecimal(java.math.BigDecimal)

Example 14 with TestData

use of org.jpwh.shared.util.TestData in project microservices by pwillhan.

the class Versioning method storeCategoriesAndItems.

public ConcurrencyTestData storeCategoriesAndItems() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    ConcurrencyTestData testData = new ConcurrencyTestData();
    testData.categories = new TestData(new Long[3]);
    testData.items = new TestData(new Long[5]);
    for (int i = 1; i <= testData.categories.identifiers.length; i++) {
        Category category = new Category();
        category.setName("Category: " + i);
        em.persist(category);
        testData.categories.identifiers[i - 1] = category.getId();
        for (int j = 1; j <= testData.categories.identifiers.length; j++) {
            Item item = new Item("Item " + j);
            item.setCategory(category);
            item.setBuyNowPrice(new BigDecimal(10 + j));
            em.persist(item);
            testData.items.identifiers[(i - 1) + (j - 1)] = item.getId();
        }
    }
    tx.commit();
    em.close();
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.concurrency.version.Item) EntityManager(javax.persistence.EntityManager) Category(org.jpwh.model.concurrency.version.Category) TestData(org.jpwh.shared.util.TestData) BigDecimal(java.math.BigDecimal)

Example 15 with TestData

use of org.jpwh.shared.util.TestData in project microservices by pwillhan.

the class Versioning method forceIncrement.

// TODO This throws the wrong exception!
// @Test(expectedExceptions = OptimisticLockException.class)
@Test(expectedExceptions = org.hibernate.StaleObjectStateException.class)
public void forceIncrement() throws Throwable {
    final TestData testData = storeItemAndBids();
    Long ITEM_ID = testData.getFirstId();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        /* 
               The <code>find()</code> method accepts a <code>LockModeType</code>. The
               <code>OPTIMISTIC_FORCE_INCREMENT</code> mode tells Hibernate that the version
               of the retrieved <code>Item</code> should be incremented after loading,
               even if it's never modified in the unit of work.
             */
        Item item = em.find(Item.class, ITEM_ID, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        Bid highestBid = queryHighestBid(em, item);
        // Now a concurrent transaction will place a bid for this item, and
        // succeed because the first commit wins!
        Executors.newSingleThreadExecutor().submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                UserTransaction tx = TM.getUserTransaction();
                try {
                    tx.begin();
                    EntityManager em = JPA.createEntityManager();
                    Item item = em.find(Item.class, testData.getFirstId(), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
                    Bid highestBid = queryHighestBid(em, item);
                    try {
                        Bid newBid = new Bid(new BigDecimal("44.44"), item, highestBid);
                        em.persist(newBid);
                    } catch (InvalidBidException ex) {
                    // Ignore
                    }
                    tx.commit();
                    em.close();
                } catch (Exception ex) {
                    // This shouldn't happen, this commit should win!
                    TM.rollback();
                    throw new RuntimeException("Concurrent operation failure: " + ex, ex);
                }
                return null;
            }
        }).get();
        try {
            /* 
                   The code persists a new <code>Bid</code> instance; this does not affect
                   any values of the <code>Item</code> instance. A new row will be inserted
                   into the <code>BID</code> table. Hibernate would not detect concurrently
                   made bids at all without a forced version increment of the
                   <code>Item</code>. We also use a checked exception to validate the
                   new bid amount; it must be greater than the currently highest bid.
                */
            Bid newBid = new Bid(new BigDecimal("44.44"), item, highestBid);
            em.persist(newBid);
        } catch (InvalidBidException ex) {
        // Bid too low, show a validation error screen...
        }
        /* 
               When flushing the persistence context, Hibernate will execute an
               <code>INSERT</code> for the new <code>Bid</code> and force an
               <code>UPDATE</code> of the <code>Item</code> with a version check.
               If someone modified the <code>Item</code> concurrently, or placed a
               <code>Bid</code> concurrently with this procedure, Hibernate throws
               an exception.
             */
        tx.commit();
        em.close();
    } catch (Exception ex) {
        throw unwrapCauseOfType(ex, org.hibernate.StaleObjectStateException.class);
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) TestData(org.jpwh.shared.util.TestData) Callable(java.util.concurrent.Callable) BigDecimal(java.math.BigDecimal) OptimisticLockException(javax.persistence.OptimisticLockException) NoResultException(javax.persistence.NoResultException) InvalidBidException(org.jpwh.model.concurrency.version.InvalidBidException) InvalidBidException(org.jpwh.model.concurrency.version.InvalidBidException) Item(org.jpwh.model.concurrency.version.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.concurrency.version.Bid) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

UserTransaction (javax.transaction.UserTransaction)26 TestData (org.jpwh.shared.util.TestData)26 EntityManager (javax.persistence.EntityManager)25 BigDecimal (java.math.BigDecimal)20 BankAccount (org.jpwh.model.bulkbatch.BankAccount)3 Bid (org.jpwh.model.bulkbatch.Bid)3 CreditCard (org.jpwh.model.bulkbatch.CreditCard)3 Item (org.jpwh.model.bulkbatch.Item)3 User (org.jpwh.model.bulkbatch.User)3 Item (org.jpwh.model.concurrency.version.Item)3 Image (org.jpwh.model.conversation.Image)3 Item (org.jpwh.model.conversation.Item)3 JPATest (org.jpwh.env.JPATest)2 StolenCreditCard (org.jpwh.model.bulkbatch.StolenCreditCard)2 Bid (org.jpwh.model.concurrency.version.Bid)2 Bid (org.jpwh.model.fetching.nplusoneselects.Bid)2 Item (org.jpwh.model.fetching.nplusoneselects.Item)2 User (org.jpwh.model.fetching.nplusoneselects.User)2 Test (org.testng.annotations.Test)2 Callable (java.util.concurrent.Callable)1