1. /*
  2. * Copyright 2002-2004 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. package org.springframework.orm.hibernate;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. import java.util.Properties;
  20. import javax.sql.DataSource;
  21. import net.sf.hibernate.HibernateException;
  22. import net.sf.hibernate.connection.ConnectionProvider;
  23. import net.sf.hibernate.util.JDBCExceptionReporter;
  24. /**
  25. * Hibernate connection provider for local DataSource instances
  26. * in an application context. This provider will be used if
  27. * LocalSessionFactoryBean's "dataSource" property is set.
  28. * @author Juergen Hoeller
  29. * @since 11.07.2003
  30. * @see LocalSessionFactoryBean#setDataSource
  31. */
  32. public class LocalDataSourceConnectionProvider implements ConnectionProvider {
  33. /**
  34. * This will hold the DataSource to use for the currently configured
  35. * Hibernate SessionFactory. It will be set just before initialization
  36. * of the respective SessionFactory, and reset immediately afterwards.
  37. */
  38. protected static ThreadLocal configTimeDataSourceHolder = new ThreadLocal();
  39. private DataSource dataSource;
  40. public void configure(Properties props) throws HibernateException {
  41. this.dataSource = (DataSource) configTimeDataSourceHolder.get();
  42. // absolutely needs thread-bound DataSource to initialize
  43. if (this.dataSource == null) {
  44. throw new HibernateException("No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean");
  45. }
  46. }
  47. /**
  48. * Return the DataSource that this ConnectionProvider wraps.
  49. */
  50. public DataSource getDataSource() {
  51. return dataSource;
  52. }
  53. public Connection getConnection() throws SQLException {
  54. try {
  55. return this.dataSource.getConnection();
  56. }
  57. catch (SQLException sqle) {
  58. JDBCExceptionReporter.logExceptions(sqle);
  59. throw sqle;
  60. }
  61. }
  62. public void closeConnection(Connection conn) throws SQLException {
  63. try {
  64. conn.close();
  65. }
  66. catch (SQLException sqle) {
  67. JDBCExceptionReporter.logExceptions(sqle);
  68. throw sqle;
  69. }
  70. }
  71. /**
  72. * Only used in Hibernate 2.0's ConnectionProvider.
  73. */
  74. public boolean isStatementCache() {
  75. return true;
  76. }
  77. public void close() {
  78. }
  79. }