/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/IsolationLevelDataSourceAdapter.java

https://github.com/keesun/spring-framework · Java · 165 lines · 50 code · 15 blank · 100 comment · 11 complexity · ea1a3979739d1af16a4b436b315e16ff MD5 · raw file

  1. /*
  2. * Copyright 2002-2012 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.datasource;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. import org.springframework.core.Constants;
  20. import org.springframework.transaction.TransactionDefinition;
  21. import org.springframework.transaction.support.DefaultTransactionDefinition;
  22. import org.springframework.transaction.support.TransactionSynchronizationManager;
  23. /**
  24. * An adapter for a target {@link javax.sql.DataSource}, applying the current
  25. * Spring transaction's isolation level (and potentially specified user credentials)
  26. * to every {@code getConnection} call. Also applies the read-only flag,
  27. * if specified.
  28. *
  29. * <p>Can be used to proxy a target JNDI DataSource that does not have the
  30. * desired isolation level (and user credentials) configured. Client code
  31. * can work with this DataSource as usual, not worrying about such settings.
  32. *
  33. * <p>Inherits the capability to apply specific user credentials from its superclass
  34. * {@link UserCredentialsDataSourceAdapter}; see the latter's javadoc for details
  35. * on that functionality (e.g. {@link #setCredentialsForCurrentThread}).
  36. *
  37. * <p><b>WARNING:</b> This adapter simply calls
  38. * {@link java.sql.Connection#setTransactionIsolation} and/or
  39. * {@link java.sql.Connection#setReadOnly} for every Connection obtained from it.
  40. * It does, however, <i>not</i> reset those settings; it rather expects the target
  41. * DataSource to perform such resetting as part of its connection pool handling.
  42. * <b>Make sure that the target DataSource properly cleans up such transaction state.</b>
  43. *
  44. * @author Juergen Hoeller
  45. * @since 2.0.3
  46. * @see #setIsolationLevel
  47. * @see #setIsolationLevelName
  48. * @see #setUsername
  49. * @see #setPassword
  50. */
  51. public class IsolationLevelDataSourceAdapter extends UserCredentialsDataSourceAdapter {
  52. /** Constants instance for TransactionDefinition */
  53. private static final Constants constants = new Constants(TransactionDefinition.class);
  54. private Integer isolationLevel;
  55. /**
  56. * Set the default isolation level by the name of the corresponding constant
  57. * in {@link org.springframework.transaction.TransactionDefinition}, e.g.
  58. * "ISOLATION_SERIALIZABLE".
  59. * <p>If not specified, the target DataSource's default will be used.
  60. * Note that a transaction-specific isolation value will always override
  61. * any isolation setting specified at the DataSource level.
  62. * @param constantName name of the constant
  63. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
  64. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
  65. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
  66. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
  67. * @see #setIsolationLevel
  68. */
  69. public final void setIsolationLevelName(String constantName) throws IllegalArgumentException {
  70. if (constantName == null || !constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
  71. throw new IllegalArgumentException("Only isolation constants allowed");
  72. }
  73. setIsolationLevel(constants.asNumber(constantName).intValue());
  74. }
  75. /**
  76. * Specify the default isolation level to use for Connection retrieval,
  77. * according to the JDBC {@link java.sql.Connection} constants
  78. * (equivalent to the corresponding Spring
  79. * {@link org.springframework.transaction.TransactionDefinition} constants).
  80. * <p>If not specified, the target DataSource's default will be used.
  81. * Note that a transaction-specific isolation value will always override
  82. * any isolation setting specified at the DataSource level.
  83. * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
  84. * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
  85. * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
  86. * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
  87. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
  88. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
  89. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
  90. * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
  91. * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel()
  92. * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
  93. */
  94. public void setIsolationLevel(int isolationLevel) {
  95. if (!constants.getValues(DefaultTransactionDefinition.PREFIX_ISOLATION).contains(isolationLevel)) {
  96. throw new IllegalArgumentException("Only values of isolation constants allowed");
  97. }
  98. this.isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? isolationLevel : null);
  99. }
  100. /**
  101. * Return the statically specified isolation level,
  102. * or {@code null} if none.
  103. */
  104. protected Integer getIsolationLevel() {
  105. return this.isolationLevel;
  106. }
  107. /**
  108. * Applies the current isolation level value and read-only flag
  109. * to the returned Connection.
  110. * @see #getCurrentIsolationLevel()
  111. * @see #getCurrentReadOnlyFlag()
  112. */
  113. @Override
  114. protected Connection doGetConnection(String username, String password) throws SQLException {
  115. Connection con = super.doGetConnection(username, password);
  116. Boolean readOnlyToUse = getCurrentReadOnlyFlag();
  117. if (readOnlyToUse != null) {
  118. con.setReadOnly(readOnlyToUse);
  119. }
  120. Integer isolationLevelToUse = getCurrentIsolationLevel();
  121. if (isolationLevelToUse != null) {
  122. con.setTransactionIsolation(isolationLevelToUse);
  123. }
  124. return con;
  125. }
  126. /**
  127. * Determine the current isolation level: either the transaction's
  128. * isolation level or a statically defined isolation level.
  129. * @return the current isolation level, or {@code null} if none
  130. * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
  131. * @see #setIsolationLevel
  132. */
  133. protected Integer getCurrentIsolationLevel() {
  134. Integer isolationLevelToUse = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
  135. if (isolationLevelToUse == null) {
  136. isolationLevelToUse = getIsolationLevel();
  137. }
  138. return isolationLevelToUse;
  139. }
  140. /**
  141. * Determine the current read-only flag: by default,
  142. * the transaction's read-only hint.
  143. * @return whether there is a read-only hint for the current scope
  144. * @see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()
  145. */
  146. protected Boolean getCurrentReadOnlyFlag() {
  147. boolean txReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
  148. return (txReadOnly ? Boolean.TRUE : null);
  149. }
  150. }