/** * PUBLIC: * Return a unit of work for this session. * The unit of work is an object level transaction that allows * a group of changes to be applied as a unit. * * @see UnitOfWork */ public UnitOfWork acquireUnitOfWork() { UnitOfWorkImpl unitOfWork = acquireNonSynchronizedUnitOfWork(); unitOfWork.registerWithTransactionIfRequired(); return unitOfWork; }
/** * PUBLIC: * Add the query to the session queries with the given name. * This allows for common queries to be pre-defined, reused and executed by name. */ public void addQuery(String name, DatabaseQuery query) { query.setName(name); addQuery(query); }
if (!isInTransaction()) { beginExternalTransaction(); if (isUnitOfWork() || isClientSession()) { getTransactionMutex().setActiveThread(Thread.currentThread()); getTransactionMutex().acquire(); if (!getTransactionMutex().isNested()) { getEventManager().preBeginTransaction(); basicBeginTransaction(); getEventManager().postBeginTransaction();
/** * PUBLIC: * Commit the active database transaction. * This allows a group of database modification to be commited or rolledback as a unit. * All writes/deletes will be sent to the database be will not be visible to other users until commit. * Although databases do not allow nested transaction, * TopLink supports nesting through only committing to the database on the outer commit. * * @exception DatabaseException most databases validate changes as they are done, * normally errors do not occur on commit unless the disk fails or the connection is lost. * @exception ConcurrencyException if this session is not within a transaction. */ public void commitTransaction() throws DatabaseException, ConcurrencyException { // Release mutex and call subclass specific commit. if (!getTransactionMutex().isNested()) { getEventManager().preCommitTransaction(); basicCommitTransaction(); getEventManager().postCommitTransaction(); } // This MUST not be in a try catch or finally as if the commit failed the transaction is still open. getTransactionMutex().release(); // If there is no db transaction in progress // if there is an active external transaction // which was started internally - it should be committed internally, too. if (!isInTransaction()) { commitExternalTransaction(); } }
/** * PUBLIC: * Rollback the active database transaction. * This allows a group of database modification to be commited or rolledback as a unit. * All writes/deletes will be sent to the database be will not be visible to other users until commit. * Although databases do not allow nested transaction, * TopLink supports nesting through only committing to the database on the outer commit. * * @exception DatabaseException if the database connection is lost or the rollback fails. * @exception ConcurrencyException if this session is not within a transaction. */ public void rollbackTransaction() throws DatabaseException, ConcurrencyException { // Ensure release of mutex and call subclass specific release. try { if (!getTransactionMutex().isNested()) { getEventManager().preRollbackTransaction(); basicRollbackTransaction(); getEventManager().postRollbackTransaction(); } } finally { getTransactionMutex().release(); // If there is no db transaction in progress // if there is an active external transaction // which was started internally - it should be rolled back internally, too. if (!isInTransaction()) { rollbackExternalTransaction(); } } }
if (session.getExternalTransactionController() == null) { setIsInTransaction(false); if ((getDatasourceConnection() != null) && usesExternalConnectionPooling()) { session.log(SessionLog.FINER, SessionLog.TRANSACTION, "commit_transaction", (Object[])null, this); session.startOperationProfile(SessionProfiler.TRANSACTION); incrementCallCount(session); basicCommitTransaction(session); session.afterTransaction(true, false); setIsInTransaction(false); } finally { decrementCallCount(); session.endOperationProfile(SessionProfiler.TRANSACTION);
getSession().beginTransaction(); try { if (getSession().isUnitOfWork()) { commitQuery.cascadeOnlyDependentParts(); } else { getSession().executeQuery(commitQuery); for (Enumeration objectsToDeleteEnum = objects.elements(); objectsToDeleteEnum.hasMoreElements();) { getSession().deleteObject(objectsToDeleteEnum.nextElement()); getSession().rollbackTransaction(); throw exception; } finally { getSession().commitTransaction();
return sequenceValue; } else { if (!getOwnerSession().getDatasourceLogin().shouldUseExternalTransactionController() && (sequence.getPreallocationSize() > 1) && !writeSession.isInTransaction()) { writeSession.beginTransaction();//write accessor is set in begin keepLocked = true; Vector localSequencesForName; if (!keepLocked) { writeSession.beginTransaction();//write accessor is set in begin accessor = writeSession.getAccessor(); Hashtable localSequences = (Hashtable)accessorToPreallocated.get(accessor); if (localSequences == null) { writeSession.rollbackTransaction(); } catch (Exception rollbackException) { writeSession.commitTransaction(); } catch (DatabaseException ex) { try { writeSession.rollbackTransaction(); } catch (Exception rollbackException) {
WriteObjectQuery writeQuery = getWriteObjectQuery(); Object object = writeQuery.getObject(); CommitManager commitManager = getSession().getCommitManager(); getSession().beginTransaction(); getSession().commitTransaction(); getSession().rollbackTransaction(); commitManager.markCommitCompleted(object); throw exception;
getSession().beginTransaction(); objectsEnum.hasMoreElements();) { Object deleted = objectsEnum.nextElement(); if (getSession().isUnitOfWork()) { getSession().getIdentityMapAccessor().removeFromIdentityMap(deleted); getSession().commitTransaction(); getSession().rollbackTransaction(); throw exception;
getSession().beginTransaction(); deleteQuery.setObject(objectToDelete); deleteQuery.cascadeOnlyDependentParts(); getSession().executeQuery(deleteQuery); getSession().rollbackTransaction(); } catch (Exception ignore) { getSession().commitTransaction();
reinitialize(); setIsActive(true); getSession().beginTransaction(); try { for (Enumeration objectsToDeleteEnum = objects.elements(); objectsToDeleteEnum.hasMoreElements();) { getSession().deleteObject(objectsToDeleteEnum.nextElement()); getSession().rollbackTransaction(); throw exception; } finally { getSession().commitTransaction();
/** * INTERNAL: * Called after transaction is completed (committed or rolled back) */ public void afterTransaction(boolean committed, boolean isExternalTransaction) { if (!committed && isExternalTransaction) { // In case jts transaction was internally started but rolled back // directly by TransactionManager this flag may still be true during afterCompletion getParent().setWasJTSTransactionInternallyStarted(false); //bug#4699614 -- added a new life cycle status so we know if the external transaction was rolledback and we don't try to rollback again later setLifecycle(AfterExternalTransactionRolledBack); } if ((getMergeManager() != null) && (getMergeManager().getAcquiredLocks() != null) && (!getMergeManager().getAcquiredLocks().isEmpty())) { //may have unreleased cache locks because of a rollback... getParent().getIdentityMapAccessorInstance().getWriteLockManager().releaseAllAcquiredLocks(getMergeManager()); this.setMergeManager(null); } getParent().afterTransaction(committed, isExternalTransaction); }
/** * INTERNAL: * This is internal to the uow, transactions should not be used explictly in a uow. * The uow shares its parents transactions. */ public void beginTransaction() throws DatabaseException { getParent().beginTransaction(); }
/** * INTERNAL: * Process the named queries we found and add them to the given session. */ public void processNamedQueries(MetadataValidator validator) { for (MetadataNamedQuery query : m_namedQueries.values()) { try { HashMap<String, String> hints = processQueryHints(query.getHints(), query.getName()); m_session.addEjbqlPlaceHolderQuery(new EJBQLPlaceHolderQuery(query.getName(), query.getEJBQLString(), hints)); } catch (Exception exception) { validator.throwErrorProcessingNamedQueryAnnotation(query.getClass(), query.getName(), exception); } } }
/** * INTERNAL: * This is internal to the unit of work and should never be called otherwise. */ public void basicBeginTransaction() { // if an exclusve connection is use this client session may have // a connection already if (!hasWriteConnection()) { // Ensure that the client is logged in for lazy clients. if (getConnectionPolicy().isLazy()) { getParent().acquireClientConnection(this); } } super.basicBeginTransaction(); }
/** * INTERNAL: * Return the active unit of work for the current external transaction. * If no transaction is active then return null. If a transaction is active * but no unit of work has been bound to it then create and return one. */ public UnitOfWorkImpl getActiveUnitOfWork() { Object transaction = getTransaction(); if (transaction == null) { return null; } UnitOfWorkImpl activeUnitOfWork = lookupActiveUnitOfWork(transaction); if (activeUnitOfWork == null) { // Txn is active but no UoW has been associated with it yet, so create one. activeUnitOfWork = (UnitOfWorkImpl) getSession().acquireUnitOfWork(); Object transactionKey = getTransactionKey(transaction); addUnitOfWork(transactionKey, activeUnitOfWork); activeUnitOfWork.setTransaction(transaction); this.activeUnitOfWorkThreadLocal.set(activeUnitOfWork); } return activeUnitOfWork; }
/** * INTERNAL: * This is internal to the unit of work and should not be called otherwise. */ public void basicCommitTransaction() { //Only releasee connection when transaction succeeds. //If not, connection will be released in rollback. super.basicCommitTransaction(); // the connection will be released by afterCompletion callback to // afterTransaction(., true); if (!getParent().getDatasourceLogin().shouldUseExternalTransactionController()) { releaseWriteConnection(); } // if there is no external TX controller, then that means we are currently not synchronized // with a global JTS transaction. In this case, there won't be any 'afterCompletion' // callbacks so we have to release the connection here. It is possible (WLS 5.1) to choose // 'usesExternalTransactionController' on the login, but still acquire a uow that WON'T be // synchronized with a global TX. else if (this.getExternalTransactionController() == null) { releaseWriteConnection(); } }
/** * INTERNAL: * This is internal to the unit of work and should not be called otherwise. */ public void basicRollbackTransaction() { try { //BUG 2660471: Make sure there is an accessor (moved here from Session) //BUG 2846785: EXCEPTION THROWN IN PREBEGINTRANSACTION EVENT CAUSES NPE if (hasWriteConnection()) { super.basicRollbackTransaction(); } } finally { // the connection will be released by afterCompletion callback to // afterTransaction(., true); if (!getParent().getDatasourceLogin().shouldUseExternalTransactionController()) { releaseWriteConnection(); } // if there is no external TX controller, then that means we are currently not synchronized // with a global JTS transaction. In this case, there won't be any 'afterCompletion' // callbacks so we have to release the connection here. It is possible (WLS 5.1) to choose // 'usesExternalTransactionController' on the login, but still acquire a uow that WON'T be // synchronized with a global TX. else if (this.getExternalTransactionController() == null) { releaseWriteConnection(); } } }
if (session.getExternalTransactionController() == null) { setIsInTransaction(false); if ((getDatasourceConnection() != null) && usesExternalConnectionPooling()) { session.log(SessionLog.FINER, SessionLog.TRANSACTION, "rollback_transaction", (Object[])null, this); session.startOperationProfile(SessionProfiler.TRANSACTION); incrementCallCount(session); basicRollbackTransaction(session); } finally { session.afterTransaction(false, false); setIsInTransaction(false); decrementCallCount(); session.endOperationProfile(SessionProfiler.TRANSACTION);