All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.jdbc.support.JdbcAccessor Maven / Gradle / Ivy

There is a newer version: 6.1.8
Show newest version
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jdbc.support;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * Base class for {@link org.springframework.jdbc.core.JdbcTemplate} and
 * other JDBC-accessing DAO helpers, defining common properties such as
 * DataSource and exception translator.
 *
 * 

Not intended to be used directly. * See {@link org.springframework.jdbc.core.JdbcTemplate}. * * @author Juergen Hoeller * @author Sebastien Deleuze * @since 28.11.2003 * @see org.springframework.jdbc.core.JdbcTemplate */ public abstract class JdbcAccessor implements InitializingBean { /** Logger available to subclasses. */ protected final Log logger = LogFactory.getLog(getClass()); @Nullable private DataSource dataSource; @Nullable private volatile SQLExceptionTranslator exceptionTranslator; private boolean lazyInit = true; /** * Set the JDBC DataSource to obtain connections from. */ public void setDataSource(@Nullable DataSource dataSource) { this.dataSource = dataSource; } /** * Return the DataSource used by this template. */ @Nullable public DataSource getDataSource() { return this.dataSource; } /** * Obtain the DataSource for actual use. * @return the DataSource (never {@code null}) * @throws IllegalStateException in case of no DataSource set * @since 5.0 */ protected DataSource obtainDataSource() { DataSource dataSource = getDataSource(); Assert.state(dataSource != null, "No DataSource set"); return dataSource; } /** * Specify the database product name for the {@code DataSource} that this accessor uses. * This allows for initializing a {@link SQLErrorCodeSQLExceptionTranslator} without * obtaining a {@code Connection} from the {@code DataSource} to get the meta-data. * @param dbName the database product name that identifies the error codes entry * @see #setExceptionTranslator * @see SQLErrorCodeSQLExceptionTranslator#setDatabaseProductName * @see java.sql.DatabaseMetaData#getDatabaseProductName() */ public void setDatabaseProductName(String dbName) { if (SQLErrorCodeSQLExceptionTranslator.hasUserProvidedErrorCodesFile()) { this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); } else { this.exceptionTranslator = new SQLExceptionSubclassTranslator(); } } /** * Set the exception translator for this instance. *

A {@link SQLErrorCodeSQLExceptionTranslator} used by default if a user-provided * `sql-error-codes.xml` file has been found in the root of the classpath. Otherwise, * {@link SQLExceptionSubclassTranslator} serves as the default translator as of 6.0. * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator * @see org.springframework.jdbc.support.SQLExceptionSubclassTranslator */ public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator) { this.exceptionTranslator = exceptionTranslator; } /** * Return the exception translator to use for this instance, * creating a default if necessary. * @see #setExceptionTranslator */ public SQLExceptionTranslator getExceptionTranslator() { SQLExceptionTranslator exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator != null) { return exceptionTranslator; } synchronized (this) { exceptionTranslator = this.exceptionTranslator; if (exceptionTranslator == null) { if (SQLErrorCodeSQLExceptionTranslator.hasUserProvidedErrorCodesFile()) { exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(obtainDataSource()); } else { exceptionTranslator = new SQLExceptionSubclassTranslator(); } this.exceptionTranslator = exceptionTranslator; } return exceptionTranslator; } } /** * Set whether to lazily initialize the SQLExceptionTranslator for this accessor, * on first encounter of an SQLException. Default is "true"; can be switched to * "false" for initialization on startup. *

Early initialization just applies if {@code afterPropertiesSet()} is called. * @see #getExceptionTranslator() * @see #afterPropertiesSet() */ public void setLazyInit(boolean lazyInit) { this.lazyInit = lazyInit; } /** * Return whether to lazily initialize the SQLExceptionTranslator for this accessor. * @see #getExceptionTranslator() */ public boolean isLazyInit() { return this.lazyInit; } /** * Eagerly initialize the exception translator, if demanded, * creating a default one for the specified DataSource if none set. */ @Override public void afterPropertiesSet() { if (getDataSource() == null) { throw new IllegalArgumentException("Property 'dataSource' is required"); } if (!isLazyInit()) { getExceptionTranslator(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy