001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.toplink;
018:
019: import oracle.toplink.exceptions.TopLinkException;
020: import oracle.toplink.sessions.Session;
021: import org.aopalliance.intercept.MethodInterceptor;
022: import org.aopalliance.intercept.MethodInvocation;
023:
024: import org.springframework.transaction.support.TransactionSynchronizationManager;
025:
026: /**
027: * This interceptor binds a new TopLink Session to the thread before a method
028: * call, closing and removing it afterwards in case of any method outcome.
029: * If there already is a pre-bound Session (e.g. from TopLinkTransactionManager,
030: * or from a surrounding TopLink-intercepted method), the interceptor simply
031: * takes part in it.
032: *
033: * <p>Application code must retrieve a TopLink Session via the
034: * <code>SessionFactoryUtils.getSession</code> method or - preferably -
035: * TopLink's own <code>Session.getActiveSession()</code> method, to be able to
036: * detect a thread-bound Session. Typically, the code will look like as follows:
037: *
038: * <pre>
039: * public void doSomeDataAccessAction() {
040: * Session session = this.serverSession.getActiveSession();
041: * ...
042: * }</pre>
043: *
044: * Note that this interceptor automatically translates TopLinkExceptions,
045: * via delegating to the <code>SessionFactoryUtils.convertTopLikAccessException</code>
046: * method that converts them to exceptions that are compatible with the
047: * <code>org.springframework.dao</code> exception hierarchy (like TopLinkTemplate does).
048: * This can be turned off if the raw exceptions are preferred.
049: *
050: * <p>This class can be considered a declarative alternative to TopLinkTemplate's
051: * callback approach. The advantages are:
052: * <ul>
053: * <li>no anonymous classes necessary for callback implementations;
054: * <li>the possibility to throw any application exceptions from within data access code.
055: * </ul>
056: *
057: * <p>The drawback is the dependency on interceptor configuration. However, note
058: * that this interceptor is usually <i>not</i> necessary in scenarios where the
059: * data access code always executes within transactions. A transaction will always
060: * have a thread-bound Session in the first place, so adding this interceptor to the
061: * configuration just adds value when potentially executing outside of transactions
062: * and/or when relying on exception translation.
063: *
064: * @author Juergen Hoeller
065: * @since 1.2
066: */
067: public class TopLinkInterceptor extends TopLinkAccessor implements
068: MethodInterceptor {
069:
070: private boolean exceptionConversionEnabled = true;
071:
072: /**
073: * Set whether to convert any TopLinkException raised to a Spring DataAccessException,
074: * compatible with the <code>org.springframework.dao</code> exception hierarchy.
075: * <p>Default is "true". Turn this flag off to let the caller receive raw exceptions
076: * as-is, without any wrapping.
077: * @see org.springframework.dao.DataAccessException
078: */
079: public void setExceptionConversionEnabled(
080: boolean exceptionConversionEnabled) {
081: this .exceptionConversionEnabled = exceptionConversionEnabled;
082: }
083:
084: public Object invoke(MethodInvocation methodInvocation)
085: throws Throwable {
086: boolean existingTransaction = false;
087: Session session = SessionFactoryUtils.getSession(
088: getSessionFactory(), true);
089: if (TransactionSynchronizationManager
090: .hasResource(getSessionFactory())) {
091: logger
092: .debug("Found thread-bound Session for TopLink interceptor");
093: existingTransaction = true;
094: } else {
095: logger.debug("Using new Session for TopLink interceptor");
096: TransactionSynchronizationManager.bindResource(
097: getSessionFactory(), new SessionHolder(session));
098: }
099: try {
100: return methodInvocation.proceed();
101: } catch (TopLinkException ex) {
102: if (this .exceptionConversionEnabled) {
103: throw convertTopLinkAccessException(ex);
104: } else {
105: throw ex;
106: }
107: } finally {
108: if (existingTransaction) {
109: logger
110: .debug("Not closing pre-bound TopLink Session after interceptor");
111: } else {
112: TransactionSynchronizationManager
113: .unbindResource(getSessionFactory());
114: SessionFactoryUtils.releaseSession(session,
115: getSessionFactory());
116: }
117: }
118: }
119:
120: }
|