001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.orm.hibernate3; 018 019import java.sql.Connection; 020import java.sql.SQLException; 021import java.util.Properties; 022import javax.sql.DataSource; 023 024import org.hibernate.HibernateException; 025import org.hibernate.connection.ConnectionProvider; 026import org.hibernate.util.JDBCExceptionReporter; 027 028import org.springframework.jdbc.datasource.DataSourceUtils; 029 030/** 031 * Hibernate connection provider for local DataSource instances 032 * in an application context. This provider will be used if 033 * LocalSessionFactoryBean's "dataSource" property is set 034 * without a Hibernate TransactionManagerLookup. 035 * 036 * @author Juergen Hoeller 037 * @since 1.2 038 * @see LocalSessionFactoryBean#setDataSource 039 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 040 */ 041@Deprecated 042public class LocalDataSourceConnectionProvider implements ConnectionProvider { 043 044 private DataSource dataSource; 045 046 private DataSource dataSourceToUse; 047 048 049 @Override 050 public void configure(Properties props) throws HibernateException { 051 this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource(); 052 // absolutely needs thread-bound DataSource to initialize 053 if (this.dataSource == null) { 054 throw new HibernateException("No local DataSource found for configuration - " + 055 "'dataSource' property must be set on LocalSessionFactoryBean"); 056 } 057 this.dataSourceToUse = getDataSourceToUse(this.dataSource); 058 } 059 060 /** 061 * Return the DataSource to use for retrieving Connections. 062 * <p>This implementation returns the passed-in DataSource as-is. 063 * @param originalDataSource the DataSource as configured by the user 064 * on LocalSessionFactoryBean 065 * @return the DataSource to actually retrieve Connections from 066 * (potentially wrapped) 067 * @see LocalSessionFactoryBean#setDataSource 068 */ 069 protected DataSource getDataSourceToUse(DataSource originalDataSource) { 070 return originalDataSource; 071 } 072 073 /** 074 * Return the DataSource that this ConnectionProvider wraps. 075 */ 076 public DataSource getDataSource() { 077 return this.dataSource; 078 } 079 080 /** 081 * This implementation delegates to the underlying DataSource. 082 * @see javax.sql.DataSource#getConnection() 083 */ 084 @Override 085 public Connection getConnection() throws SQLException { 086 try { 087 return this.dataSourceToUse.getConnection(); 088 } 089 catch (SQLException ex) { 090 JDBCExceptionReporter.logExceptions(ex); 091 throw ex; 092 } 093 } 094 095 /** 096 * This implementation calls {@link DataSourceUtils#doCloseConnection}, 097 * checking against a {@link org.springframework.jdbc.datasource.SmartDataSource}. 098 */ 099 @Override 100 public void closeConnection(Connection con) throws SQLException { 101 try { 102 DataSourceUtils.doCloseConnection(con, this.dataSourceToUse); 103 } 104 catch (SQLException ex) { 105 JDBCExceptionReporter.logExceptions(ex); 106 throw ex; 107 } 108 } 109 110 /** 111 * This implementation does nothing: 112 * We're dealing with an externally managed DataSource. 113 */ 114 @Override 115 public void close() { 116 } 117 118 /** 119 * This implementation returns {@code false}: We cannot guarantee 120 * to receive the same Connection within a transaction, not even when 121 * dealing with a JNDI DataSource. 122 */ 123 @Override 124 public boolean supportsAggressiveRelease() { 125 return false; 126 } 127 128}