|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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 | + * https://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 | + |
| 17 | +package org.springframework.jdbc.support; |
| 18 | + |
| 19 | +import java.sql.SQLException; |
| 20 | + |
| 21 | +import javax.sql.DataSource; |
| 22 | + |
| 23 | +import org.springframework.dao.DataAccessException; |
| 24 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | + |
| 27 | +/** |
| 28 | + * {@link JdbcAccessor}-aligned subclass of the plain {@link DataSourceTransactionManager}, |
| 29 | + * adding common JDBC exception translation for the commit and rollback step. |
| 30 | + * Typically used in combination with {@link org.springframework.jdbc.core.JdbcTemplate} |
| 31 | + * which applies the same {@link SQLExceptionTranslator} infrastructure by default. |
| 32 | + * |
| 33 | + * <p>Exception translation is specifically relevant for commit steps in serializable |
| 34 | + * transactions (e.g. on Postgres) where concurrency failures may occur late on commit. |
| 35 | + * This allows for throwing {@link org.springframework.dao.ConcurrencyFailureException} to |
| 36 | + * callers instead of {@link org.springframework.transaction.TransactionSystemException}. |
| 37 | + * |
| 38 | + * <p>Analogous to {@code HibernateTransactionManager} and {@code JpaTransactionManager}, |
| 39 | + * this transaction manager may throw {@link DataAccessException} from {@link #commit} |
| 40 | + * and possibly also from {@link #rollback}. Calling code should be prepared for handling |
| 41 | + * such exceptions next to {@link org.springframework.transaction.TransactionException}, |
| 42 | + * which is generally sensible since {@code TransactionSynchronization} implementations |
| 43 | + * may also throw such exceptions in their {@code flush} and {@code beforeCommit} phases. |
| 44 | + * |
| 45 | + * @author Juergen Hoeller |
| 46 | + * @since 5.3 |
| 47 | + * @see DataSourceTransactionManager |
| 48 | + * @see #setDataSource |
| 49 | + * @see #setExceptionTranslator |
| 50 | + */ |
| 51 | +@SuppressWarnings("serial") |
| 52 | +public class JdbcTransactionManager extends DataSourceTransactionManager { |
| 53 | + |
| 54 | + @Nullable |
| 55 | + private volatile SQLExceptionTranslator exceptionTranslator; |
| 56 | + |
| 57 | + private boolean lazyInit = true; |
| 58 | + |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new JdbcTransactionManager instance. |
| 62 | + * A DataSource has to be set to be able to use it. |
| 63 | + * @see #setDataSource |
| 64 | + */ |
| 65 | + public JdbcTransactionManager() { |
| 66 | + super(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create a new JdbcTransactionManager instance. |
| 71 | + * @param dataSource the JDBC DataSource to manage transactions for |
| 72 | + */ |
| 73 | + public JdbcTransactionManager(DataSource dataSource) { |
| 74 | + this(); |
| 75 | + setDataSource(dataSource); |
| 76 | + afterPropertiesSet(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Specify the database product name for the DataSource that this transaction manager |
| 82 | + * uses. This allows to initialize an SQLErrorCodeSQLExceptionTranslator without |
| 83 | + * obtaining a Connection from the DataSource to get the meta-data. |
| 84 | + * @param dbName the database product name that identifies the error codes entry |
| 85 | + * @see JdbcAccessor#setDatabaseProductName |
| 86 | + * @see SQLErrorCodeSQLExceptionTranslator#setDatabaseProductName |
| 87 | + * @see java.sql.DatabaseMetaData#getDatabaseProductName() |
| 88 | + */ |
| 89 | + public void setDatabaseProductName(String dbName) { |
| 90 | + this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set the exception translator for this instance. |
| 95 | + * <p>If no custom translator is provided, a default |
| 96 | + * {@link SQLErrorCodeSQLExceptionTranslator} is used |
| 97 | + * which examines the SQLException's vendor-specific error code. |
| 98 | + * @see JdbcAccessor#setExceptionTranslator |
| 99 | + * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator |
| 100 | + */ |
| 101 | + public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator) { |
| 102 | + this.exceptionTranslator = exceptionTranslator; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return the exception translator for this instance. |
| 107 | + * <p>Creates a default {@link SQLErrorCodeSQLExceptionTranslator} |
| 108 | + * for the specified DataSource if none set. |
| 109 | + * @see #getDataSource() |
| 110 | + */ |
| 111 | + public SQLExceptionTranslator getExceptionTranslator() { |
| 112 | + SQLExceptionTranslator exceptionTranslator = this.exceptionTranslator; |
| 113 | + if (exceptionTranslator != null) { |
| 114 | + return exceptionTranslator; |
| 115 | + } |
| 116 | + synchronized (this) { |
| 117 | + exceptionTranslator = this.exceptionTranslator; |
| 118 | + if (exceptionTranslator == null) { |
| 119 | + exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(obtainDataSource()); |
| 120 | + this.exceptionTranslator = exceptionTranslator; |
| 121 | + } |
| 122 | + return exceptionTranslator; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Set whether to lazily initialize the SQLExceptionTranslator for this transaction manager, |
| 128 | + * on first encounter of an SQLException. Default is "true"; can be switched to |
| 129 | + * "false" for initialization on startup. |
| 130 | + * <p>Early initialization just applies if {@code afterPropertiesSet()} is called. |
| 131 | + * @see #getExceptionTranslator() |
| 132 | + * @see #afterPropertiesSet() |
| 133 | + */ |
| 134 | + public void setLazyInit(boolean lazyInit) { |
| 135 | + this.lazyInit = lazyInit; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager. |
| 140 | + * @see #getExceptionTranslator() |
| 141 | + */ |
| 142 | + public boolean isLazyInit() { |
| 143 | + return this.lazyInit; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Eagerly initialize the exception translator, if demanded, |
| 148 | + * creating a default one for the specified DataSource if none set. |
| 149 | + */ |
| 150 | + @Override |
| 151 | + public void afterPropertiesSet() { |
| 152 | + super.afterPropertiesSet(); |
| 153 | + if (!isLazyInit()) { |
| 154 | + getExceptionTranslator(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * This implementation attempts to use the {@link SQLExceptionTranslator}, |
| 161 | + * falling back to a {@link org.springframework.transaction.TransactionSystemException}. |
| 162 | + * @see #getExceptionTranslator() |
| 163 | + * @see DataSourceTransactionManager#translateException |
| 164 | + */ |
| 165 | + @Override |
| 166 | + protected RuntimeException translateException(String task, SQLException ex) { |
| 167 | + DataAccessException dae = getExceptionTranslator().translate(task, null, ex); |
| 168 | + if (dae != null) { |
| 169 | + return dae; |
| 170 | + } |
| 171 | + return super.translateException(task, ex); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments