001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.hibernate;
018:
019: import java.sql.Connection;
020: import java.sql.SQLException;
021: import java.util.Properties;
022:
023: import javax.sql.DataSource;
024:
025: import net.sf.hibernate.HibernateException;
026: import net.sf.hibernate.connection.ConnectionProvider;
027: import net.sf.hibernate.util.JDBCExceptionReporter;
028:
029: /**
030: * Hibernate connection provider for local DataSource instances
031: * in an application context. This provider will be used if
032: * LocalSessionFactoryBean's "dataSource" property is set.
033: *
034: * @author Juergen Hoeller
035: * @since 11.07.2003
036: * @see LocalSessionFactoryBean#setDataSource
037: */
038: public class LocalDataSourceConnectionProvider implements
039: ConnectionProvider {
040:
041: private DataSource dataSource;
042:
043: private DataSource dataSourceToUse;
044:
045: public void configure(Properties props) throws HibernateException {
046: this .dataSource = LocalSessionFactoryBean
047: .getConfigTimeDataSource();
048: // absolutely needs thread-bound DataSource to initialize
049: if (this .dataSource == null) {
050: throw new HibernateException(
051: "No local DataSource found for configuration - "
052: + "dataSource property must be set on LocalSessionFactoryBean");
053: }
054: this .dataSourceToUse = getDataSourceToUse(this .dataSource);
055: }
056:
057: /**
058: * Return the DataSource to use for retrieving Connections.
059: * <p>This implementation returns the passed-in DataSource as-is.
060: * @param originalDataSource the DataSource as configured by the user
061: * on LocalSessionFactoryBean
062: * @return the DataSource to actually retrieve Connections from
063: * (potentially wrapped)
064: * @see LocalSessionFactoryBean#setDataSource
065: */
066: protected DataSource getDataSourceToUse(
067: DataSource originalDataSource) {
068: return originalDataSource;
069: }
070:
071: /**
072: * Return the DataSource that this ConnectionProvider wraps.
073: */
074: public DataSource getDataSource() {
075: return dataSource;
076: }
077:
078: public Connection getConnection() throws SQLException {
079: try {
080: return this .dataSourceToUse.getConnection();
081: } catch (SQLException ex) {
082: JDBCExceptionReporter.logExceptions(ex);
083: throw ex;
084: }
085: }
086:
087: public void closeConnection(Connection con) throws SQLException {
088: try {
089: con.close();
090: } catch (SQLException ex) {
091: JDBCExceptionReporter.logExceptions(ex);
092: throw ex;
093: }
094: }
095:
096: public void close() {
097: // Do nothing here - it's an externally managed DataSource.
098: }
099:
100: }
|