Search in sources :

Example 81 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class VersioningTimestamp method firstCommitWins.

@Test(expectedExceptions = OptimisticLockException.class)
public void firstCommitWins() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some Item");
        em.persist(someItem);
        tx.commit();
        em.close();
        final Long ITEM_ID = someItem.getId();
        // Load an item and change its name
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        item.setName("New Name");
        // The concurrent second unit of work doing the same
        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, ITEM_ID);
                    item.setName("Other Name");
                    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 {
            tx.commit();
        // Version check: How many rows have been updated?
        } catch (Exception ex) {
            throw unwrapCauseOfType(ex, OptimisticLockException.class);
        }
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.concurrency.versiontimestamp.Item) EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) Callable(java.util.concurrent.Callable) OptimisticLockException(javax.persistence.OptimisticLockException) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 82 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class ExtendedPC method storeItemImagesTestData.

/* ################################################################################### */
public TestData storeItemImagesTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] ids = new Long[1];
    Item item = new Item();
    item.setName("Some Item");
    em.persist(item);
    ids[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        item.getImages().add(new Image("Image " + i, "image" + i + ".jpg", 640, 480));
    }
    tx.commit();
    em.close();
    return new TestData(ids);
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.conversation.Item) EntityManager(javax.persistence.EntityManager) TestData(org.jpwh.shared.util.TestData) Image(org.jpwh.model.conversation.Image)

Example 83 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class GeneratedProperties method storeLoadLastModified.

@Test(groups = { "H2" })
public void storeLoadLastModified() throws Exception {
    long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertNotNull(item.getCreatedOn());
        Date lastModified = item.getLastModified();
        assertNotNull(lastModified);
        assertTrue(item.getCreatedOn().getTime() < lastModified.getTime());
        item.setDescription("Some modification.");
        em.flush();
        Date newLastModified = item.getLastModified();
        assertNotEquals(lastModified, newLastModified, "Modification time should have been updated");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Date(java.util.Date) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 84 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class LazyProperties method storeLoadLocator.

@Test
public void storeLoadLocator() throws Exception {
    // TODO: This test fails on H2 standalone
    // http://groups.google.com/group/h2-database/browse_thread/thread/9c6f4893a62c9b1a
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        byte[] bytes = new byte[131072];
        new Random().nextBytes(bytes);
        InputStream imageInputStream = new ByteArrayInputStream(bytes);
        int byteLength = bytes.length;
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        // Need the native Hibernate API
        Session session = em.unwrap(Session.class);
        // You need to know the number of bytes you want to read from the stream!
        Blob blob = session.getLobHelper().createBlob(imageInputStream, byteLength);
        someItem.setImageBlob(blob);
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        // You can stream the bytes directly...
        InputStream imageDataStream = item.getImageBlob().getBinaryStream();
        // ... or materialize them into memory:
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        StreamUtils.copy(imageDataStream, outStream);
        byte[] imageBytes = outStream.toByteArray();
        assertEquals(imageBytes.length, 131072);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Blob(java.sql.Blob) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(org.hibernate.Session) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 85 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class LazyProperties method storeLoadProperties.

@Test
public void storeLoadProperties() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        byte[] bytes = new byte[131072];
        new Random().nextBytes(bytes);
        someItem.setImage(bytes);
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        // Accessing one initializes ALL lazy properties in a single SELECT
        assertEquals(item.getDescription(), "This is some description.");
        // 128 kilobytes
        assertEquals(item.getImage().length, 131072);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Random(java.util.Random) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

UserTransaction (javax.transaction.UserTransaction)642 EntityManager (javax.persistence.EntityManager)244 Test (org.testng.annotations.Test)187 Test (org.junit.Test)157 JPATest (org.jpwh.env.JPATest)138 InitialContext (javax.naming.InitialContext)62 BigDecimal (java.math.BigDecimal)53 HashMap (java.util.HashMap)52 IOException (java.io.IOException)50 Context (javax.naming.Context)49 List (java.util.List)47 FacesContext (javax.faces.context.FacesContext)43 NamingException (javax.naming.NamingException)43 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 Node (javax.jcr.Node)42 KieSession (org.kie.api.runtime.KieSession)42 Query (javax.persistence.Query)38 QueryingTest (org.jpwh.test.querying.QueryingTest)36 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 Item (org.jpwh.model.querying.Item)35