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.jdbc.support.nativejdbc;
  17. import java.sql.CallableStatement;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import org.apache.commons.dbcp.DelegatingCallableStatement;
  24. import org.apache.commons.dbcp.DelegatingConnection;
  25. import org.apache.commons.dbcp.DelegatingPreparedStatement;
  26. import org.apache.commons.dbcp.DelegatingResultSet;
  27. import org.apache.commons.dbcp.DelegatingStatement;
  28. /**
  29. * Implementation of the NativeJdbcExtractor interface for the Jakarta Commons
  30. * DBCP connection pool. Returns the underlying native Connection, Statement,
  31. * ResultSet etc to application code instead of DBCP's wrapper implementations.
  32. * The returned JDBC classes can then safely be cast, e.g. to OracleResultSet.
  33. *
  34. * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with a
  35. * Commons DBCP DataSource: If a given object is not a Commons DBCP wrapper,
  36. * it will be returned as-is.
  37. *
  38. * <p>Note: Before Commons DBCP 1.1, DelegatingCallableStatement and
  39. * DelegatingResultSet have not offered any means to access underlying delegates.
  40. * Therefore, getNativeCallableStatement and getNativeResultSet will just work
  41. * with DBCP 1.1. But getNativeResultSet will not be invoked by JdbcTemplate for a
  42. * wrapped ResultSet anyway, because getNativeStatement/getNativePreparedStatement
  43. * will already have returned the underlying delegate before.
  44. *
  45. * @author Juergen Hoeller
  46. * @since 25.08.2003
  47. */
  48. public class CommonsDbcpNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
  49. public Connection getNativeConnection(Connection con) throws SQLException {
  50. if (con instanceof DelegatingConnection) {
  51. Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate();
  52. // For some reason, the innermost delegate can be null: not for a
  53. // Statement's Connection but for the Connection handle returned by the pool.
  54. // We'll fall back to the MetaData's Connection in this case, which is
  55. // a native unwrapped Connection with Commons DBCP 1.1.
  56. return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
  57. }
  58. return con;
  59. }
  60. public Statement getNativeStatement(Statement stmt) throws SQLException {
  61. if (stmt instanceof DelegatingStatement) {
  62. return ((DelegatingStatement) stmt).getInnermostDelegate();
  63. }
  64. return stmt;
  65. }
  66. public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
  67. if (ps instanceof DelegatingPreparedStatement) {
  68. return ((DelegatingPreparedStatement) ps).getInnermostDelegate();
  69. }
  70. return ps;
  71. }
  72. public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException {
  73. if (cs instanceof DelegatingCallableStatement) {
  74. return ((DelegatingCallableStatement) cs).getInnermostDelegate();
  75. }
  76. return cs;
  77. }
  78. public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
  79. if (rs instanceof DelegatingResultSet) {
  80. return ((DelegatingResultSet) rs).getInnermostDelegate();
  81. }
  82. return rs;
  83. }
  84. }