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.sessions.DatabaseSession;
20: import oracle.toplink.sessions.Session;
21:
22: /**
23: * Simple implementation of the SessionFactory interface: always returns
24: * the passed-in Session as-is.
25: *
26: * <p>Useful for testing or standalone usage of TopLink-based data access objects.
27: * <b>In a server environment, use ServerSessionFactory instead.</code>
28: *
29: * @author Juergen Hoeller
30: * @since 1.2
31: * @see ServerSessionFactory
32: */
33: public class SingleSessionFactory implements SessionFactory {
34:
35: private final Session session;
36:
37: /**
38: * Create a new SingleSessionFactory with the given Session.
39: * @param session the TopLink Session to hold
40: */
41: public SingleSessionFactory(Session session) {
42: this .session = session;
43: }
44:
45: /**
46: * Return the held TopLink Session as-is.
47: */
48: public Session createSession() {
49: return this .session;
50: }
51:
52: /**
53: * Throws an UnsupportedOperationException: SingleSessionFactory does not
54: * support managed client Sessions. Use ServerSessionFactory instead.
55: */
56: public Session createManagedClientSession() {
57: throw new UnsupportedOperationException(
58: "SingleSessionFactory does not support managed client Sessions");
59: }
60:
61: /**
62: * Throws an UnsupportedOperationException: SingleSessionFactory does not
63: * support transaction-aware Sessions. Use ServerSessionFactory instead.
64: */
65: public Session createTransactionAwareSession() {
66: throw new UnsupportedOperationException(
67: "SingleSessionFactory does not support transaction-aware Sessions");
68: }
69:
70: /**
71: * Shut the pre-configured TopLink Session down.
72: * @see oracle.toplink.sessions.DatabaseSession#logout()
73: * @see oracle.toplink.sessions.Session#release()
74: */
75: public void close() {
76: if (this .session instanceof DatabaseSession) {
77: ((DatabaseSession) this.session).logout();
78: }
79: this.session.release();
80: }
81:
82: }
|