KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > support > TransactionAwareSessionAdapter


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.support;
18
19 import oracle.toplink.sessions.Session;
20
21 import org.springframework.beans.factory.FactoryBean;
22 import org.springframework.orm.toplink.SessionFactory;
23
24 /**
25  * This adapter FactoryBean takes a TopLink SessionFactory and exposes a
26  * corresponding transaction-aware TopLink Session as bean reference.
27  *
28  * <p>This adapter bean will usually be defined in front of a Spring
29  * LocalSessionFactoryBean, to allow for passing Session references to DAOs
30  * that expect to work on a raw TopLink Session. Your DAOs can then,
31  * for example, access the currently active Session and UnitOfWork via
32  * <code>Session.getActiveSession()</code> and
33  * <code>Session.getActiveUnitOfWork()</code>, respectively.
34  *
35  * <p>The main advantage of this proxy is that it allows DAOs to work with a
36  * plain TopLink Session reference, while still participating in Spring's
37  * (or a J2EE server's) resource and transaction management. DAOs will only
38  * rely on the TopLink API in such a scenario, without any Spring dependencies.
39  *
40  * <p>It is usually preferable to write your TopLink-based DAOs with Spring's
41  * TopLinkTemplate, offering benefits such as consistent data access exceptions
42  * instead of TopLinkExceptions at the DAO layer. However, Spring's resource
43  * and transaction management (and Dependency Injection) will work for DAOs
44  * written against the plain TopLink API too.
45  *
46  * <p>Of course, you can still access the target TopLink SessionFactory
47  * even when your DAOs go through this adapter, by defining a bean reference
48  * that points directly at your target SessionFactory bean.
49  *
50  * <p>Note that the actual creation of a transaction-aware TopLink Session
51  * is available on the TopLink SessionFactory itself. This adapter FactoryBean
52  * is just a convenient way to expose such a Session in a declarative fashion.
53  *
54  * @author Juergen Hoeller
55  * @since 1.2
56  * @see org.springframework.orm.toplink.LocalSessionFactoryBean
57  * @see org.springframework.orm.toplink.SessionFactory#createTransactionAwareSession()
58  * @see oracle.toplink.sessions.Session#getActiveSession()
59  * @see oracle.toplink.sessions.Session#getActiveUnitOfWork()
60  */

61 public class TransactionAwareSessionAdapter implements FactoryBean {
62
63     private Session session;
64
65
66     /**
67      * Set the SessionFactory that this adapter is supposed to expose a
68      * transaction-aware TopLink Session for. This should be the raw
69      * SessionFactory, as accessed by TopLinkTransactionManager.
70      * @see org.springframework.orm.toplink.TopLinkTransactionManager
71      */

72     public void setSessionFactory(SessionFactory sessionFactory) {
73         this.session = sessionFactory.createTransactionAwareSession();
74     }
75
76
77     public Object JavaDoc getObject() {
78         return this.session;
79     }
80
81     public Class JavaDoc getObjectType() {
82         return Session.class;
83     }
84
85     public boolean isSingleton() {
86         return true;
87     }
88
89 }
90
Popular Tags