KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.exceptions.ValidationException;
21 import oracle.toplink.sessionbroker.SessionBroker;
22 import oracle.toplink.sessions.Session;
23
24 /**
25  * Spring SessionFactory implementation allowing users to
26  * inject a TopLink Session built from a TopLink SessionBroker.
27  *
28  * SessionBrokers are used identically to any other TopLink Session. DAO code
29  * should never have to distinguish between Sessions which broker requests to
30  * multiple databases and Sessions which manage requests to a single database.
31  *
32  * The only pertinent difference in the SessionBroker api involves the method
33  * for obtaining a thread-safe "client" Session from the SessionBroker.
34  * Instead of the typical acquireClientSession
35  * method, this SessionFactory implementation uses the
36  * acquireClientSessionBroker method.
37  * If a SessionBroker aggregates non thread-safe DatabaseSessions,
38  * the factory will throw UnsupportedOperationExceptions
39  * if used to create managed or transaction-aware Sessions.
40  *
41  * @author <a HREF="mailto:james.x.clark@oracle.com">James Clark</a>
42  * @author Juergen Hoeller
43  * @since 1.2.6
44  * @see org.springframework.orm.toplink.ServerSessionFactory
45  * @see oracle.toplink.threetier.ServerSession#acquireClientSession()
46  * @see oracle.toplink.sessionbroker.SessionBroker#acquireClientSessionBroker()
47  */

48 public class SessionBrokerSessionFactory extends AbstractSessionFactory {
49
50     private final SessionBroker sessionBroker;
51
52
53     /**
54      * Create a new SessionBrokerSessionFactory for the given SessionBroker.
55      * @param broker the TopLink SessionBroker to fetch Sessions from
56      */

57     public SessionBrokerSessionFactory(SessionBroker broker) {
58         this.sessionBroker = broker;
59     }
60
61
62     /**
63      * Try to create a client Session; fall back to the master Session,
64      * if no client Session can be created (because of the session broker's
65      * configuration).
66      * @see #createClientSession()
67      * @see #getMasterSession()
68      */

69     public Session createSession() throws TopLinkException {
70         try {
71             return createClientSession();
72         }
73         catch (ValidationException ex) {
74             logger.debug(
75                     "Could not create TopLink client session for SessionBroker - returning SessionBroker itself", ex);
76             return getMasterSession();
77         }
78     }
79
80     /**
81      * Return this factory's SessionBroker as-is.
82      */

83     protected Session getMasterSession() {
84         return this.sessionBroker;
85     }
86
87     /**
88      * Create a plain client SessionBroker for this factory's ServerSession.
89      * @see oracle.toplink.sessionbroker.SessionBroker#acquireClientSessionBroker()
90      */

91     protected Session createClientSession() throws TopLinkException {
92         return this.sessionBroker.acquireClientSessionBroker();
93     }
94
95
96     /**
97      * Shut the pre-configured TopLink SessionBroker down.
98      * @see oracle.toplink.sessions.DatabaseSession#logout()
99      * @see oracle.toplink.sessions.Session#release()
100      */

101     public void close() {
102         this.sessionBroker.logout();
103         this.sessionBroker.release();
104     }
105
106 }
107
Popular Tags