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.core;
  17. import java.sql.CallableStatement;
  18. import java.sql.SQLException;
  19. import org.springframework.dao.DataAccessException;
  20. /**
  21. * Generic callback interface for code that operates on a CallableStatement.
  22. * Allows to execute any number of operations on a single CallableStatement,
  23. * for example a single execute call or repeated execute calls with varying
  24. * parameters.
  25. *
  26. * <p>Used internally by JdbcTemplate, but also useful for application code.
  27. * Note that the passed-in CallableStatement can have been created by the
  28. * framework or by a custom CallableStatementCreator. However, the latter is
  29. * hardly ever necessary, as most custom callback actions will perform updates
  30. * in which case a standard CallableStatement is fine. Custom actions will
  31. * always set parameter values themselves, so that CallableStatementCreator
  32. * capability is not needed either.
  33. *
  34. * @author Juergen Hoeller
  35. * @since 16.03.2004
  36. * @see JdbcTemplate#execute(String, CallableStatementCallback)
  37. * @see JdbcTemplate#execute(CallableStatementCreator, CallableStatementCallback)
  38. */
  39. public interface CallableStatementCallback {
  40. /**
  41. * Gets called by JdbcTemplate.execute with an active JDBC CallableStatement.
  42. * Does not need to care about activating or closing the Connection,
  43. * or handling transactions.
  44. *
  45. * <p>If called without a thread-bound JDBC transaction (initiated by
  46. * DataSourceTransactionManager), the code will simply get executed on the
  47. * JDBC connection with its transactional semantics. If JdbcTemplate is
  48. * configured to use a JTA-aware DataSource, the JDBC connection and thus
  49. * the callback code will be transactional if a JTA transaction is active.
  50. *
  51. * <p>Allows for returning a result object created within the callback, i.e.
  52. * a domain object or a collection of domain objects. A thrown RuntimeException
  53. * is treated as application exception, it gets propagated to the caller of
  54. * the template.
  55. *
  56. * @param cs active JDBC CallableStatement
  57. * @return a result object, or null if none
  58. * @throws SQLException if thrown by a JDBC method, to be auto-converted
  59. * into a DataAccessException by a SQLExceptionTranslator
  60. * @throws DataAccessException in case of custom exceptions
  61. */
  62. Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException;
  63. }