KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > sessions > IsolatedClientSession


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.sessions;
23
24 import oracle.toplink.essentials.exceptions.*;
25 import oracle.toplink.essentials.threetier.*;
26 import oracle.toplink.essentials.internal.identitymaps.IdentityMapManager;
27 import oracle.toplink.essentials.queryframework.*;
28 import oracle.toplink.essentials.internal.sessions.AbstractSession;
29 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
30
31 /**
32  * Provides isolation support by allowing a client session
33  * to have a local cache of the subset of the classes.
34  * This can be used to avoid caching frequently changing data,
35  * or for security or VPD purposes.
36  */

37 public class IsolatedClientSession extends oracle.toplink.essentials.threetier.ClientSession {
38     public IsolatedClientSession(ServerSession parent, ConnectionPolicy connectionPolicy) {
39         super(parent, connectionPolicy);
40     }
41
42     /**
43     * INTERNAL:
44     * Set up the IdentityMapManager. This method allows subclasses of Session to override
45     * the default IdentityMapManager functionality.
46     */

47     public void initializeIdentityMapAccessor() {
48         this.identityMapAccessor = new IsolatedClientSessionIdentityMapAccessor(this, new IdentityMapManager(this));
49     }
50
51     /**
52     * INTERNAL:
53     * Helper method to calculate whether to execute this query locally or send
54     * it to the server session.
55     */

56     protected boolean shouldExecuteLocally(DatabaseQuery query) {
57         if (isIsolatedQuery(query)) {
58             return true;
59         }
60         return isInTransaction();
61     }
62
63     /**
64     * INTERNAL: Answers if this query is an isolated query and must be
65     * executed locally.
66     */

67     protected boolean isIsolatedQuery(DatabaseQuery query) {
68         query.checkDescriptor(this);
69         if (query.isDataModifyQuery() || ((query.getDescriptor() != null) && query.getDescriptor().isIsolated())) {
70             // For CR#4334 if in transaction stay on client session.
71
// That way client's write accessor will be used for all queries.
72
// This is to preserve transaction isolation levels.
73
// also if this is an isolated class and we are in an isolated session
74
//load locally.
75
return true;
76         }
77         return false;
78
79     }
80
81     /**
82     * INTERNAL:
83     * Gets the next link in the chain of sessions followed by a query's check
84     * early return, the chain of sessions with identity maps all the way up to
85     * the root session.
86     * <p>
87     * Used for session broker which delegates to registered sessions, or UnitOfWork
88     * which checks parent identity map also.
89     * @param canReturnSelf true when method calls itself. If the path
90     * starting at <code>this</code> is acceptable. Sometimes true if want to
91     * move to the first valid session, i.e. executing on ClientSession when really
92     * should be on ServerSession.
93     * @param terminalOnly return the session we will execute the call on, not
94     * the next step towards it.
95     * @return this if there is no next link in the chain
96     */

97     public AbstractSession getParentIdentityMapSession(DatabaseQuery query, boolean canReturnSelf, boolean terminalOnly) {
98         if ((query != null) && isIsolatedQuery(query)) {
99             return this;
100         } else {
101             return getParent().getParentIdentityMapSession(query, canReturnSelf, terminalOnly);
102         }
103     }
104
105     /**
106     * INTERNAL:
107     * Gets the session which this query will be executed on.
108     * Generally will be called immediately before the call is translated,
109     * which is immediately before session.executeCall.
110     * <p>
111     * Since the execution session also knows the correct datasource platform
112     * to execute on, it is often used in the mappings where the platform is
113     * needed for type conversion, or where calls are translated.
114     * <p>
115     * Is also the session with the accessor. Will return a ClientSession if
116     * it is in transaction and has a write connection.
117     * @return a session with a live accessor
118     * @param query may store session name or reference class for brokers case
119     */

120     public AbstractSession getExecutionSession(DatabaseQuery query) {
121         if (shouldExecuteLocally(query)) {
122             return this;
123         } else {
124             return getParent().getExecutionSession(query);
125         }
126     }
127
128     /**
129     * INTERNAL:
130     * Isolated sessions must forward call execution to its parent, unless in a transaction.
131     * This is required as isolated sessions are always the execution session for isolated classes.
132     */

133     public Object JavaDoc executeCall(Call call, AbstractRecord translationRow, DatabaseQuery query) throws DatabaseException {
134         if (isInTransaction()) {
135             return super.executeCall(call, translationRow, query);
136         }
137         return getParent().executeCall(call, translationRow, query);
138     }
139 }
140
Popular Tags