use of org.datanucleus.api.jdo.exceptions.TransactionNotActiveException in project tests by datanucleus.
the class PersistenceManagerTest method testGetObjectByIdNonTransactional.
/**
* Test for getObjectById() when the object is retrieved from the L2 cache.
* @throws Exception
*/
public void testGetObjectByIdNonTransactional() throws Exception {
try {
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Insert a record for use later
BigDecimal bd = new BigDecimal("12345.12345");
BigInteger bi = new BigInteger("12345");
java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
tx.begin();
Primitive p = new Primitive();
setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
pm.makePersistent(p);
tx.commit();
id = pm.getObjectId(p);
tx.begin();
Primitive p1 = (Primitive) pm.getObjectById(id);
Object id1 = pm.getObjectId(p1);
assertEquals(id, id1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object in a nontransactionalRead PMF from L2 cache
PersistenceManager pm2 = null;
try {
pm = pmf.getPersistenceManager();
try {
// first getObjectById() will result in hollow instance that
// is put in L2 cache
Object pc = pm.getObjectById(id);
// pin pc in the L2 cache to make sure it is not garbage collected
pmf.getDataStoreCache().pin(pc);
// take a new PM to have an empty L1 cache
pm2 = pmf.getPersistenceManager();
// take PC from L2 cache
pm2.getObjectById(id);
} catch (TransactionNotActiveException e) {
LOG.error("Exception thrown in test", e);
fail("TransactionNotActiveException thrown even though nontransactionRead is true");
}
} finally {
if (!pm.isClosed()) {
pm.close();
}
if (pm2 != null) {
pm2.close();
}
}
} finally {
// Clean out our data
clean(Primitive.class);
}
}
use of org.datanucleus.api.jdo.exceptions.TransactionNotActiveException in project tests by datanucleus.
the class PersistenceManagerTest method testNonTransactionWriteNegative.
/**
* Check for non active transactions and non-transactional write is false.
*/
public void testNonTransactionWriteNegative() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Dog rex = new Dog();
rex.setName("rex");
rex.setId("rex" + new Random().nextInt());
rex.setColor("blue");
Object id = null;
try {
tx.begin();
pm.makePersistent(rex);
id = pm.getObjectId(rex);
tx.commit();
tx.setNontransactionalWrite(false);
// check updating field
rex.setColor("yellow");
fail("Expected TransactionNotWritableException");
} catch (TransactionNotWritableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// check updating application id field
try {
tx.setNontransactionalWrite(false);
Dog d = (Dog) pm.getObjectById(id, false);
d.setId("newid");
fail("Expected TransactionNotWritableException");
} catch (TransactionNotWritableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
pm.newQuery(Dog.class).deletePersistentAll();
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
pm.newQuery(Dog.class).deletePersistentAll((Object[]) new String[] { "stupidarg" });
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
Dog d = (Dog) it.next();
d.setId("newid");
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Dog.class);
}
}
Aggregations