alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (UnitOfWorkCallback.java)

This example Spring Framework source code file (UnitOfWorkCallback.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

object, object, toplinkcallback, toplinkexception, toplinkexception, unitofwork, unitofwork, unitofworkcallback, unitofworkcallback

The Spring Framework UnitOfWorkCallback.java source code

/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.orm.toplink;

import oracle.toplink.exceptions.TopLinkException;
import oracle.toplink.sessions.Session;
import oracle.toplink.sessions.UnitOfWork;

/**
 * Convenient abstract implementation of the TopLinkCallback interface,
 * exposing a UnitOfWork to perform write operations on.
 *
 * <p>The exposed UnitOfWork will either be be the active UnitOfWork of
 * the current transaction, if any, or a temporarily acquired UnitOfWork
 * that will be committed at the end of the operation.
 *
 * @author Juergen Hoeller
 * @since 1.2
 * @see #doInUnitOfWork(oracle.toplink.sessions.UnitOfWork)
 * @see oracle.toplink.sessions.Session#getActiveUnitOfWork()
 */
public abstract class UnitOfWorkCallback implements TopLinkCallback {

	/**
	 * Determines the UnitOfWork to work on (either the active UnitOfWork or a
	 * temporarily acquired UnitOfWork) and delegates to <code>doInUnitOfWork.
	 * @see #doInUnitOfWork(oracle.toplink.sessions.UnitOfWork)
	 */
	public final Object doInTopLink(Session session) throws TopLinkException {
		// Fetch active UnitOfWork or acquire temporary UnitOfWork.
		UnitOfWork unitOfWork = session.getActiveUnitOfWork();
		boolean newUnitOfWork = false;
		if (unitOfWork == null) {
			unitOfWork = session.acquireUnitOfWork();
			newUnitOfWork = true;
		}

		// Perform callback operation, committing the UnitOfWork unless
		// it is the active UnitOfWork of an externally managed transaction.
		try {
			Object result = doInUnitOfWork(unitOfWork);
			if (newUnitOfWork) {
				unitOfWork.commit();
			}
			return result;
		}
		finally {
			if (newUnitOfWork) {
				unitOfWork.release();
			}
		}
	}

	/**
	 * Called with a UnitOfWork to work on, either the active UnitOfWork or a
	 * temporarily acquired UnitOfWork (as determined by the transaction status).
	 * @param unitOfWork the TopLink UnitOfWork to perform write operations on
	 * @return a result object, or <code>null if none
	 * @throws TopLinkException in case of TopLink errors
	 */
	protected abstract Object doInUnitOfWork(UnitOfWork unitOfWork) throws TopLinkException;

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework UnitOfWorkCallback.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.