01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 oracle.toplink.sessions.UnitOfWork;
22:
23: /**
24: * Convenient abstract implementation of the TopLinkCallback interface,
25: * exposing either the plain TopLink Session or the TopLink UnitOfWork
26: * (which extends the Session interface) to code that reads persistent objects.
27: *
28: * <p>Exposes the UnitOfWork if there is an active one (that is, if we're running
29: * within a non-read-only transaction); else exposes the Session itself.
30: * This allows to modify returned objects within a transaction, which is
31: * often desired, while the same code will return shared cache objects
32: * if running outside a transaction.
33: *
34: * <p>If "enforceReadOnly" is demanded, the callback will always expose the
35: * Session itself, avoiding the UnitOfWork overhead in any case.
36: *
37: * @author Juergen Hoeller
38: * @since 1.2
39: * @see oracle.toplink.sessions.Session#getActiveUnitOfWork()
40: * @see #readFromSession(oracle.toplink.sessions.Session)
41: */
42: public abstract class SessionReadCallback implements TopLinkCallback {
43:
44: private final boolean enforceReadOnly;
45:
46: /**
47: * Create a new SessionReadCallback, not enforcing read-only objects.
48: */
49: public SessionReadCallback() {
50: this .enforceReadOnly = false;
51: }
52:
53: /**
54: * Create a new SessionReadCallback, enforcing read-only objects if demanded.
55: * @param enforceReadOnly whether to enforce returning read-only objects,
56: * even if running within a non-read-only transaction
57: */
58: public SessionReadCallback(boolean enforceReadOnly) {
59: this .enforceReadOnly = enforceReadOnly;
60: }
61:
62: /**
63: * Determines the Session to work on (either the active UnitOfWork
64: * or the plain Session) and delegates to <code>readFromSession</code>.
65: * @see #readFromSession(oracle.toplink.sessions.Session)
66: */
67: public final Object doInTopLink(Session session)
68: throws TopLinkException {
69: Session sessionToUse = session;
70: if (!this .enforceReadOnly) {
71: UnitOfWork unitOfWork = session.getActiveUnitOfWork();
72: if (unitOfWork != null) {
73: sessionToUse = unitOfWork;
74: }
75: }
76: return readFromSession(sessionToUse);
77: }
78:
79: /**
80: * Called with a Session to work on, either the active UnitOfWork
81: * or the plain Session (as determined by the transaction status).
82: * @param session the TopLink Session to perform read operations on
83: * @return a result object, or <code>null</code> if none
84: * @throws TopLinkException in case of TopLink errors
85: */
86: protected abstract Object readFromSession(Session session)
87: throws TopLinkException;
88:
89: }
|