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