KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > TopLinkInterceptor


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

16
17 package org.springframework.orm.toplink;
18
19 import oracle.toplink.exceptions.TopLinkException;
20 import oracle.toplink.sessions.Session;
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23
24 import org.springframework.transaction.support.TransactionSynchronizationManager;
25
26 /**
27  * This interceptor binds a new TopLink Session to the thread before a method
28  * call, closing and removing it afterwards in case of any method outcome.
29  * If there already is a pre-bound Session (e.g. from TopLinkTransactionManager,
30  * or from a surrounding TopLink-intercepted method), the interceptor simply
31  * takes part in it.
32  *
33  * <p>Application code must retrieve a TopLink Session via the
34  * <code>SessionFactoryUtils.getSession</code> method or - preferably -
35  * TopLink's own <code>Session.getActiveSession()</code> method, to be able to
36  * detect a thread-bound Session. Typically, the code will look like as follows:
37  *
38  * <pre>
39  * public void doSomeDataAccessAction() {
40  * Session session = this.serverSession.getActiveSession();
41  * ...
42  * }</pre>
43  *
44  * Note that this interceptor automatically translates TopLinkExceptions,
45  * via delegating to the <code>SessionFactoryUtils.convertTopLikAccessException</code>
46  * method that converts them to exceptions that are compatible with the
47  * <code>org.springframework.dao</code> exception hierarchy (like TopLinkTemplate does).
48  * This can be turned off if the raw exceptions are preferred.
49  *
50  * <p>This class can be considered a declarative alternative to TopLinkTemplate's
51  * callback approach. The advantages are:
52  * <ul>
53  * <li>no anonymous classes necessary for callback implementations;
54  * <li>the possibility to throw any application exceptions from within data access code.
55  * </ul>
56  *
57  * <p>The drawback is the dependency on interceptor configuration. However, note
58  * that this interceptor is usually <i>not</i> necessary in scenarios where the
59  * data access code always executes within transactions. A transaction will always
60  * have a thread-bound Session in the first place, so adding this interceptor to the
61  * configuration just adds value when potentially executing outside of transactions
62  * and/or when relying on exception translation.
63  *
64  * @author Juergen Hoeller
65  * @since 1.2
66  */

67 public class TopLinkInterceptor extends TopLinkAccessor implements MethodInterceptor {
68
69     private boolean exceptionConversionEnabled = true;
70
71
72     /**
73      * Set whether to convert any TopLinkException raised to a Spring DataAccessException,
74      * compatible with the <code>org.springframework.dao</code> exception hierarchy.
75      * <p>Default is "true". Turn this flag off to let the caller receive raw exceptions
76      * as-is, without any wrapping.
77      * @see org.springframework.dao.DataAccessException
78      */

79     public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) {
80         this.exceptionConversionEnabled = exceptionConversionEnabled;
81     }
82
83
84     public Object JavaDoc invoke(MethodInvocation methodInvocation) throws Throwable JavaDoc {
85         boolean existingTransaction = false;
86         Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
87         if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
88             logger.debug("Found thread-bound Session for TopLink interceptor");
89             existingTransaction = true;
90         }
91         else {
92             logger.debug("Using new Session for TopLink interceptor");
93             TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
94         }
95         try {
96             return methodInvocation.proceed();
97         }
98         catch (TopLinkException ex) {
99             if (this.exceptionConversionEnabled) {
100                 throw convertTopLinkAccessException(ex);
101             }
102             else {
103                 throw ex;
104             }
105         }
106         finally {
107             if (existingTransaction) {
108                 logger.debug("Not closing pre-bound TopLink Session after interceptor");
109             }
110             else {
111                 TransactionSynchronizationManager.unbindResource(getSessionFactory());
112                 SessionFactoryUtils.releaseSession(session, getSessionFactory());
113             }
114         }
115     }
116
117 }
118
Popular Tags