KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > LocalDataSourceConnectionProvider


1 /*
2  * Copyright 2002-2006 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.hibernate;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import net.sf.hibernate.HibernateException;
26 import net.sf.hibernate.connection.ConnectionProvider;
27 import net.sf.hibernate.util.JDBCExceptionReporter;
28
29 /**
30  * Hibernate connection provider for local DataSource instances
31  * in an application context. This provider will be used if
32  * LocalSessionFactoryBean's "dataSource" property is set.
33  *
34  * @author Juergen Hoeller
35  * @since 11.07.2003
36  * @see LocalSessionFactoryBean#setDataSource
37  */

38 public class LocalDataSourceConnectionProvider implements ConnectionProvider {
39
40     private DataSource JavaDoc dataSource;
41
42     private DataSource JavaDoc dataSourceToUse;
43
44
45     public void configure(Properties JavaDoc props) throws HibernateException {
46         this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource();
47         // absolutely needs thread-bound DataSource to initialize
48
if (this.dataSource == null) {
49             throw new HibernateException("No local DataSource found for configuration - " +
50                 "dataSource property must be set on LocalSessionFactoryBean");
51         }
52         this.dataSourceToUse = getDataSourceToUse(this.dataSource);
53     }
54
55     /**
56      * Return the DataSource to use for retrieving Connections.
57      * <p>This implementation returns the passed-in DataSource as-is.
58      * @param originalDataSource the DataSource as configured by the user
59      * on LocalSessionFactoryBean
60      * @return the DataSource to actually retrieve Connections from
61      * (potentially wrapped)
62      * @see LocalSessionFactoryBean#setDataSource
63      */

64     protected DataSource JavaDoc getDataSourceToUse(DataSource JavaDoc originalDataSource) {
65         return originalDataSource;
66     }
67
68     /**
69      * Return the DataSource that this ConnectionProvider wraps.
70      */

71     public DataSource JavaDoc getDataSource() {
72         return dataSource;
73     }
74
75     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
76         try {
77             return this.dataSourceToUse.getConnection();
78         }
79         catch (SQLException JavaDoc ex) {
80             JDBCExceptionReporter.logExceptions(ex);
81             throw ex;
82         }
83     }
84
85     public void closeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
86         try {
87             con.close();
88         }
89         catch (SQLException JavaDoc ex) {
90             JDBCExceptionReporter.logExceptions(ex);
91             throw ex;
92         }
93     }
94
95     public void close() {
96         // Do nothing here - it's an externally managed DataSource.
97
}
98
99 }
100
Popular Tags