private boolean runTestQuery() { Transaction tx = pm.currentTransaction(); boolean doCommit = false; if (!tx.isActive()) { tx.begin(); doCommit = true; } Query query = null; // Run a self-test query. If it doesn't work, we will self-disable. What a PITA... String selfTestQuery = "select \"DB_ID\" from " + DBS + ""; try { prepareTxn(); query = pm.newQuery("javax.jdo.query.SQL", selfTestQuery); query.execute(); return true; } catch (Throwable t) { doCommit = false; LOG.warn("Self-test query [" + selfTestQuery + "] failed; direct SQL is disabled", t); tx.rollback(); return false; } finally { if (doCommit) { tx.commit(); } if (query != null) { query.closeAll(); } } }
/** * Opens a new one or the one already created Every call of this function must * have corresponding commit or rollback function call * * @return an active transaction */ @Override public boolean openTransaction() { openTrasactionCalls++; if (openTrasactionCalls == 1) { currentTransaction = pm.currentTransaction(); currentTransaction.begin(); transactionStatus = TXN_STATUS.OPEN; } else { // openTransactionCalls > 1 means this is an interior transaction // We should already have a transaction created that is active. if ((currentTransaction == null) || (!currentTransaction.isActive())){ throw new RuntimeException("openTransaction called in an interior" + " transaction scope, but currentTransaction is not active."); } } boolean result = currentTransaction.isActive(); debugLog("Open transaction: count = " + openTrasactionCalls + ", isActive = " + result); return result; }
throw e; if (!currentTransaction.isActive()) { RuntimeException e = new RuntimeException("commitTransaction was called but openTransactionCalls = " + openTrasactionCalls + ". This probably indicates that there are unbalanced " + debugLog("Commit transaction: count = " + openTrasactionCalls + ", isactive "+ currentTransaction.isActive()); if ((openTrasactionCalls == 0) && currentTransaction.isActive()) { transactionStatus = TXN_STATUS.COMMITED; currentTransaction.commit();
/** * Rolls back the current transaction if it is active */ public void rollbackTransaction() { if (openTrasactionCalls < 1) { return; } openTrasactionCalls = 0; if (currentTransaction.isActive() && transactionStatus != TXN_STATUS.ROLLBACK) { transactionStatus = TXN_STATUS.ROLLBACK; // could already be rolled back currentTransaction.rollback(); } }
public Object getCurrentTransaction() { Transaction t = pm.currentTransaction(); t.begin(); return t; }
private boolean runDbCheck(String queryText, String name) { Transaction tx = pm.currentTransaction(); if (!tx.isActive()) { tx.begin(); } try { executeNoResult(queryText); return true; } catch (Throwable t) { LOG.debug(name + " check failed, assuming we are not on " + name + ": " + t.getMessage()); tx.rollback(); tx = pm.currentTransaction(); tx.begin(); return false; } }
@Test public void testSchemaCreationWithEmptyArray_Issue98() { PersistenceManager pm = TestTools.openPM(props); pm.currentTransaction().begin(); pm.makePersistent(new TestClassWithArray()); pm.currentTransaction().commit(); pm.currentTransaction().begin(); ZooSchema schema = ZooJdoHelper.schema(pm); assertNotNull(schema.getClass(TestClassWithArray.class.getName())); assertNotNull(schema.getClass(TestClassInArray.class.getName())); pm.currentTransaction().rollback(); TestTools.closePM(); }
/** * @return true if there is an active transaction. If the current transaction * is either committed or rolled back it returns false */ @Override public boolean isActiveTransaction() { if (currentTransaction == null) { return false; } return currentTransaction.isActive(); }
private boolean runTestQuery() { Transaction tx = pm.currentTransaction(); // Run a self-test query. If it doesn't work, we will self-disable. What a PITA... String selfTestQuery = "select \"DB_ID\" from \"DBS\""; try { pm.newQuery("javax.jdo.query.SQL", selfTestQuery).execute(); tx.commit(); return true; } catch (Exception ex) { LOG.warn("Self-test query [" + selfTestQuery + "] failed; direct SQL is disabled", ex); tx.rollback(); return false; } }
@Test(expected=UnsupportedOperationException.class) public void testOptimistic() { PersistenceManager pm = TestTools.openPM(); pm.currentTransaction().begin(); //default assertFalse(pm.currentTransaction().getOptimistic()); //should work fine pm.currentTransaction().setOptimistic(true); pm.currentTransaction().setOptimistic(false); pm.currentTransaction().rollback(); TestTools.closePM(); }
public void rollbackIfActive( Transaction tx ) { PersistenceManager pm = tx.getPersistenceManager(); try { if ( tx.isActive() ) { tx.rollback(); } } finally { closePersistenceManager( pm ); } }
/** * Rolls back the current transaction if it is active */ @Override public void rollbackTransaction() { if (openTrasactionCalls < 1) { debugLog("rolling back transaction: no open transactions: " + openTrasactionCalls); return; } debugLog("Rollback transaction, isActive: " + isActiveTransaction()); try { if (isActiveTransaction() && transactionStatus != TXN_STATUS.ROLLBACK) { currentTransaction.rollback(); } } finally { openTrasactionCalls = 0; transactionStatus = TXN_STATUS.ROLLBACK; // remove all detached objects from the cache, since the transaction is // being rolled back they are no longer relevant, and this prevents them // from reattaching in future transactions pm.evictAll(); } }
/** * Method to transition when serialised. * @param sm State Manager * @return The new LifeCycle state */ public LifeCycleState transitionSerialize(StateManager sm) { Transaction tx = sm.getPersistenceManager().currentTransaction(); if (tx.isActive() && !tx.getOptimistic()) { return changeState(sm, P_CLEAN); } return this; }
public void setRollbackOnly() { Transaction tx = this.persistenceManagerHolder.getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
private void checkThatDefinitionFails(PersistenceManager pm, ZooClass s, String name) { try { //re-create existing index s.createIndex(name, true); fail("Should have failed: " + name); } catch (IllegalArgumentException e) { //good } pm.currentTransaction().commit(); pm.currentTransaction().begin(); }