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;
  17. import javax.sql.DataSource;
  18. /**
  19. * Base class for JdbcTemplate and other JDBC-accessing DAO helpers,
  20. * defining common properties like exception translator.
  21. *
  22. * <p>Not intended to be used directly. See JdbcTemplate.
  23. *
  24. * @author Juergen Hoeller
  25. * @since 28.11.2003
  26. * @see org.springframework.jdbc.core.JdbcTemplate
  27. */
  28. public class JdbcAccessor {
  29. /**
  30. * Used to obtain connections throughout the lifecycle of this object.
  31. * This enables this class to close connections if necessary.
  32. **/
  33. private DataSource dataSource;
  34. /** Helper to translate SQL exceptions to DataAccessExceptions */
  35. private SQLExceptionTranslator exceptionTranslator;
  36. /**
  37. * Set the JDBC DataSource to obtain connections from.
  38. */
  39. public void setDataSource(DataSource dataSource) {
  40. this.dataSource = dataSource;
  41. }
  42. /**
  43. * Return the DataSource used by this template.
  44. */
  45. public DataSource getDataSource() {
  46. return dataSource;
  47. }
  48. /**
  49. * Set the exception translator for this instance.
  50. * If no custom translator is provided, a default is used
  51. * which examines the SQLException's vendor-specific error code.
  52. * @param exceptionTranslator exception translator
  53. * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
  54. * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
  55. */
  56. public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator) {
  57. this.exceptionTranslator = exceptionTranslator;
  58. }
  59. /**
  60. * Return the exception translator for this instance.
  61. * Creates a default one for the specified DataSource if none set.
  62. */
  63. public synchronized SQLExceptionTranslator getExceptionTranslator() {
  64. if (this.exceptionTranslator == null) {
  65. this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(this.dataSource);
  66. }
  67. return this.exceptionTranslator;
  68. }
  69. /**
  70. * Eagerly initialize the exception translator,
  71. * creating a default one for the specified DataSource if none set.
  72. */
  73. public void afterPropertiesSet() {
  74. if (this.dataSource == null) {
  75. throw new IllegalArgumentException("dataSource is required");
  76. }
  77. getExceptionTranslator();
  78. }
  79. }