KickJava   Java API By Example, From Geeks To Geeks.

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


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.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     /**
39      * Create a new SingleSessionFactory with the given Session.
40      * @param session the TopLink Session to hold
41      */

42     public SingleSessionFactory(Session session) {
43         this.session = session;
44     }
45
46
47     /**
48      * Return the held TopLink Session as-is.
49      */

50     public Session createSession() {
51         return this.session;
52     }
53
54     /**
55      * Throws an UnsupportedOperationException: SingleSessionFactory does not
56      * support managed client Sessions. Use ServerSessionFactory instead.
57      */

58     public Session createManagedClientSession() {
59         throw new UnsupportedOperationException JavaDoc("SingleSessionFactory does not support managed client Sessions");
60     }
61
62     /**
63      * Throws an UnsupportedOperationException: SingleSessionFactory does not
64      * support transaction-aware Sessions. Use ServerSessionFactory instead.
65      */

66     public Session createTransactionAwareSession() {
67         throw new UnsupportedOperationException JavaDoc("SingleSessionFactory does not support transaction-aware Sessions");
68     }
69
70
71     /**
72      * Shut the pre-configured TopLink Session down.
73      * @see oracle.toplink.sessions.DatabaseSession#logout()
74      * @see oracle.toplink.sessions.Session#release()
75      */

76     public void close() {
77         if (this.session instanceof DatabaseSession) {
78             ((DatabaseSession) this.session).logout();
79         }
80         this.session.release();
81     }
82
83 }
84
Popular Tags