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.hibernate3;
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 org.hibernate.HibernateException;
026: import org.hibernate.connection.ConnectionProvider;
027: import org.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 1.2
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: /**
079: * This implementation delegates to the underlying DataSource.
080: * @see javax.sql.DataSource#getConnection()
081: */
082: public Connection getConnection() throws SQLException {
083: try {
084: return this .dataSourceToUse.getConnection();
085: } catch (SQLException ex) {
086: JDBCExceptionReporter.logExceptions(ex);
087: throw ex;
088: }
089: }
090:
091: /**
092: * This implementation simply calls <code>Connection.close</code>.
093: * @see java.sql.Connection#close()
094: */
095: public void closeConnection(Connection con) throws SQLException {
096: try {
097: con.close();
098: } catch (SQLException ex) {
099: JDBCExceptionReporter.logExceptions(ex);
100: throw ex;
101: }
102: }
103:
104: /**
105: * This implementation does nothing:
106: * We're dealing with an externally managed DataSource.
107: */
108: public void close() {
109: }
110:
111: /**
112: * This implementation returns <code>false</code>: We cannot guarantee
113: * to receive the same Connection within a transaction, not even when
114: * dealing with a JNDI DataSource.
115: */
116: public boolean supportsAggressiveRelease() {
117: return false;
118: }
119:
120: }
|