001: /*
002: * Copyright 2002-2005 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.exceptions.ValidationException;
021: import oracle.toplink.sessionbroker.SessionBroker;
022: import oracle.toplink.sessions.Session;
023:
024: /**
025: * Spring SessionFactory implementation allowing users to
026: * inject a TopLink Session built from a TopLink SessionBroker.
027: *
028: * SessionBrokers are used identically to any other TopLink Session. DAO code
029: * should never have to distinguish between Sessions which broker requests to
030: * multiple databases and Sessions which manage requests to a single database.
031: *
032: * The only pertinent difference in the SessionBroker api involves the method
033: * for obtaining a thread-safe "client" Session from the SessionBroker.
034: * Instead of the typical acquireClientSession
035: * method, this SessionFactory implementation uses the
036: * acquireClientSessionBroker method.
037: * If a SessionBroker aggregates non thread-safe DatabaseSessions,
038: * the factory will throw UnsupportedOperationExceptions
039: * if used to create managed or transaction-aware Sessions.
040: *
041: * @author <a href="mailto:james.x.clark@oracle.com">James Clark</a>
042: * @author Juergen Hoeller
043: * @since 1.2.6
044: * @see org.springframework.orm.toplink.ServerSessionFactory
045: * @see oracle.toplink.threetier.ServerSession#acquireClientSession()
046: * @see oracle.toplink.sessionbroker.SessionBroker#acquireClientSessionBroker()
047: */
048: public class SessionBrokerSessionFactory extends AbstractSessionFactory {
049:
050: private final SessionBroker sessionBroker;
051:
052: /**
053: * Create a new SessionBrokerSessionFactory for the given SessionBroker.
054: * @param broker the TopLink SessionBroker to fetch Sessions from
055: */
056: public SessionBrokerSessionFactory(SessionBroker broker) {
057: this .sessionBroker = broker;
058: }
059:
060: /**
061: * Try to create a client Session; fall back to the master Session,
062: * if no client Session can be created (because of the session broker's
063: * configuration).
064: * @see #createClientSession()
065: * @see #getMasterSession()
066: */
067: public Session createSession() throws TopLinkException {
068: try {
069: return createClientSession();
070: } catch (ValidationException ex) {
071: logger
072: .debug(
073: "Could not create TopLink client session for SessionBroker - returning SessionBroker itself",
074: ex);
075: return getMasterSession();
076: }
077: }
078:
079: /**
080: * Return this factory's SessionBroker as-is.
081: */
082: protected Session getMasterSession() {
083: return this .sessionBroker;
084: }
085:
086: /**
087: * Create a plain client SessionBroker for this factory's ServerSession.
088: * @see oracle.toplink.sessionbroker.SessionBroker#acquireClientSessionBroker()
089: */
090: protected Session createClientSession() throws TopLinkException {
091: return this .sessionBroker.acquireClientSessionBroker();
092: }
093:
094: /**
095: * Shut the pre-configured TopLink SessionBroker down.
096: * @see oracle.toplink.sessions.DatabaseSession#logout()
097: * @see oracle.toplink.sessions.Session#release()
098: */
099: public void close() {
100: this.sessionBroker.logout();
101: this.sessionBroker.release();
102: }
103:
104: }
|