KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 java.sql.SQLException JavaDoc;
20
21 import oracle.toplink.exceptions.DatabaseException;
22 import oracle.toplink.exceptions.TopLinkException;
23
24 import org.springframework.beans.factory.BeanClassLoaderAware;
25 import org.springframework.beans.factory.DisposableBean;
26 import org.springframework.beans.factory.FactoryBean;
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.dao.DataAccessException;
29 import org.springframework.dao.support.PersistenceExceptionTranslator;
30 import org.springframework.jdbc.support.SQLExceptionTranslator;
31
32 /**
33  * {@link org.springframework.beans.factory.FactoryBean} that creates a
34  * TopLink {@link SessionFactory}. This is the usual way to set up a shared
35  * TopLink SessionFactory in a Spring application context; the SessionFactory
36  * can then be passed to TopLink-based DAOs via dependency injection.
37  *
38  * <p>See the base class {@link LocalSessionFactory} for configuration details.
39  *
40  * <p>This class also implements the
41  * {@link org.springframework.dao.support.PersistenceExceptionTranslator}
42  * interface, as autodetected by Spring's
43  * {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor},
44  * for AOP-based translation of native exceptions to Spring DataAccessExceptions.
45  * Hence, the presence of a LocalSessionFactoryBean automatically enables a
46  * PersistenceExceptionTranslationPostProcessor to translate TopLink exceptions.
47  *
48  * <p>If your DAOs expect to receive a raw TopLink Session, consider defining a
49  * {@link org.springframework.orm.toplink.support.TransactionAwareSessionAdapter}
50  * in front of this bean. This adapter will provide a TopLink Session rather
51  * than a SessionFactory as bean reference. Your DAOs can then, for example,
52  * access the currently active Session and UnitOfWork via
53  * <code>Session.getActiveSession()</code> and <code>Session.getActiveUnitOfWork()</code>,
54  * respectively. Note that you can still access the SessionFactory as well, by
55  * defining a bean reference that points directly at the LocalSessionFactoryBean.
56  *
57  * @author Juergen Hoeller
58  * @since 1.2
59  * @see LocalSessionFactory
60  * @see org.springframework.orm.toplink.support.TransactionAwareSessionAdapter
61  * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
62  */

63 public class LocalSessionFactoryBean extends LocalSessionFactory
64         implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean, PersistenceExceptionTranslator {
65
66     private SessionFactory sessionFactory;
67
68     private SQLExceptionTranslator jdbcExceptionTranslator;
69
70
71     /**
72      * Set the JDBC exception translator for this SessionFactory.
73      * <p>Applied to any SQLException root cause of a TopLink DatabaseException,
74      * within Spring's PersistenceExceptionTranslator mechanism.
75      * The default is to rely on TopLink's native exception translation.
76      * @see oracle.toplink.exceptions.DatabaseException
77      * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
78      * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
79      */

80     public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
81         this.jdbcExceptionTranslator = jdbcExceptionTranslator;
82     }
83
84     /**
85      * Return the JDBC exception translator for this instance, if any.
86      */

87     public SQLExceptionTranslator getJdbcExceptionTranslator() {
88         return this.jdbcExceptionTranslator;
89     }
90
91     /**
92      * Sets the given bean ClassLoader as TopLink Session ClassLoader.
93      * @see #setSessionClassLoader
94      */

95     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
96         setSessionClassLoader(classLoader);
97     }
98
99     public void afterPropertiesSet() throws TopLinkException {
100         this.sessionFactory = createSessionFactory();
101     }
102
103
104     public Object JavaDoc getObject() {
105         return this.sessionFactory;
106     }
107
108     public Class JavaDoc getObjectType() {
109         return (this.sessionFactory != null ? this.sessionFactory.getClass() : SessionFactory.class);
110     }
111
112     public boolean isSingleton() {
113         return true;
114     }
115
116
117     /**
118      * Implementation of the PersistenceExceptionTranslator interface,
119      * as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
120      * <p>Converts the exception if it is a TopLinkException;
121      * else returns <code>null</code> to indicate an unknown exception.
122      * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
123      * @see #convertTopLinkAccessException
124      */

125     public DataAccessException translateExceptionIfPossible(RuntimeException JavaDoc ex) {
126         if (ex instanceof TopLinkException) {
127             return convertTopLinkAccessException((TopLinkException) ex);
128         }
129         return null;
130     }
131
132     /**
133      * Convert the given TopLinkException to an appropriate exception from the
134      * <code>org.springframework.dao</code> hierarchy.
135      * <p>Will automatically apply a specified SQLExceptionTranslator to a
136      * TopLink DatabaseException, else rely on TopLink's default translation.
137      * @param ex TopLinkException that occured
138      * @return a corresponding DataAccessException
139      * @see SessionFactoryUtils#convertTopLinkAccessException
140      * @see #setJdbcExceptionTranslator
141      */

142     public DataAccessException convertTopLinkAccessException(TopLinkException ex) {
143         if (getJdbcExceptionTranslator() != null && ex instanceof DatabaseException) {
144             Throwable JavaDoc internalEx = ex.getInternalException();
145             // Should always be a SQLException inside a DatabaseException.
146
if (internalEx instanceof SQLException JavaDoc) {
147                 return getJdbcExceptionTranslator().translate(
148                         "TopLink operation: " + ex.getMessage(), null, (SQLException JavaDoc) internalEx);
149             }
150         }
151         return SessionFactoryUtils.convertTopLinkAccessException(ex);
152     }
153
154
155     public void destroy() {
156         logger.info("Closing TopLink SessionFactory");
157         this.sessionFactory.close();
158     }
159
160 }
161
Popular Tags