001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jdbc.datasource;
018:
019: import java.sql.Connection;
020: import java.sql.SQLException;
021:
022: import org.springframework.core.Constants;
023: import org.springframework.transaction.TransactionDefinition;
024: import org.springframework.transaction.support.DefaultTransactionDefinition;
025: import org.springframework.transaction.support.TransactionSynchronizationManager;
026:
027: /**
028: * An adapter for a target {@link javax.sql.DataSource}, applying the current
029: * Spring transaction's isolation level (and potentially specified user credentials)
030: * to every <code>getConnection</code> call. Also applies the read-only flag,
031: * if specified.
032: *
033: * <p>Can be used to proxy a target JNDI DataSource that does not have the
034: * desired isolation level (and user credentials) configured. Client code
035: * can work with this DataSource as usual, not worrying about such settings.
036: *
037: * <p>Inherits the capability to apply specific user credentials from its superclass
038: * {@link UserCredentialsDataSourceAdapter}; see the latter's javadoc for details
039: * on that functionality (e.g. {@link #setCredentialsForCurrentThread}).
040: *
041: * <p><b>WARNING:</b> This adapter simply calls
042: * {@link java.sql.Connection#setTransactionIsolation} and/or
043: * {@link java.sql.Connection#setReadOnly} for every Connection obtained from it.
044: * It does, however, <i>not</i> reset those settings; it rather expects the target
045: * DataSource to perform such resetting as part of its connection pool handling.
046: * <b>Make sure that the target DataSource properly cleans up such transaction state.</b>
047: *
048: * @author Juergen Hoeller
049: * @since 2.0.3
050: * @see #setIsolationLevel
051: * @see #setIsolationLevelName
052: * @see #setUsername
053: * @see #setPassword
054: */
055: public class IsolationLevelDataSourceAdapter extends
056: UserCredentialsDataSourceAdapter {
057:
058: /** Constants instance for TransactionDefinition */
059: private static final Constants constants = new Constants(
060: TransactionDefinition.class);
061:
062: private Integer isolationLevel;
063:
064: /**
065: * Set the default isolation level by the name of the corresponding constant
066: * in {@link org.springframework.transaction.TransactionDefinition}, e.g.
067: * "ISOLATION_SERIALIZABLE".
068: * <p>If not specified, the target DataSource's default will be used.
069: * Note that a transaction-specific isolation value will always override
070: * any isolation setting specified at the DataSource level.
071: * @param constantName name of the constant
072: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
073: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
074: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
075: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
076: * @see #setIsolationLevel
077: */
078: public final void setIsolationLevelName(String constantName)
079: throws IllegalArgumentException {
080: if (constantName == null
081: || !constantName
082: .startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
083: throw new IllegalArgumentException(
084: "Only isolation constants allowed");
085: }
086: setIsolationLevel(constants.asNumber(constantName).intValue());
087: }
088:
089: /**
090: * Specify the default isolation level to use for Connection retrieval,
091: * according to the JDBC {@link java.sql.Connection} constants
092: * (equivalent to the corresponding Spring
093: * {@link org.springframework.transaction.TransactionDefinition} constants).
094: * <p>If not specified, the target DataSource's default will be used.
095: * Note that a transaction-specific isolation value will always override
096: * any isolation setting specified at the DataSource level.
097: * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
098: * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
099: * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
100: * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
101: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
102: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
103: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
104: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
105: * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel()
106: * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
107: */
108: public void setIsolationLevel(int isolationLevel) {
109: if (!constants.getValues(
110: DefaultTransactionDefinition.PREFIX_ISOLATION)
111: .contains(new Integer(isolationLevel))) {
112: throw new IllegalArgumentException(
113: "Only values of isolation constants allowed");
114: }
115: this .isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? new Integer(
116: isolationLevel)
117: : null);
118: }
119:
120: /**
121: * Return the statically specified isolation level,
122: * or <code>null</code> if none.
123: */
124: protected Integer getIsolationLevel() {
125: return this .isolationLevel;
126: }
127:
128: /**
129: * Applies the current isolation level value and read-only flag
130: * to the returned Connection.
131: * @see #getCurrentIsolationLevel()
132: * @see #getCurrentReadOnlyFlag()
133: */
134: protected Connection doGetConnection(String username,
135: String password) throws SQLException {
136: Connection con = super .doGetConnection(username, password);
137: Boolean readOnlyToUse = getCurrentReadOnlyFlag();
138: if (readOnlyToUse != null) {
139: con.setReadOnly(readOnlyToUse.booleanValue());
140: }
141: Integer isolationLevelToUse = getCurrentIsolationLevel();
142: if (isolationLevelToUse != null) {
143: con.setTransactionIsolation(isolationLevelToUse.intValue());
144: }
145: return con;
146: }
147:
148: /**
149: * Determine the current isolation level: either the transaction's
150: * isolation level or a statically defined isolation level.
151: * @return the current isolation level, or <code>null</code> if none
152: * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
153: * @see #setIsolationLevel
154: */
155: protected Integer getCurrentIsolationLevel() {
156: Integer isolationLevelToUse = TransactionSynchronizationManager
157: .getCurrentTransactionIsolationLevel();
158: if (isolationLevelToUse == null) {
159: isolationLevelToUse = getIsolationLevel();
160: }
161: return isolationLevelToUse;
162: }
163:
164: /**
165: * Determine the current read-only flag: by default,
166: * the transaction's read-only hint.
167: * @return whether there is a read-only hint for the current scope
168: * @see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()
169: */
170: protected Boolean getCurrentReadOnlyFlag() {
171: boolean txReadOnly = TransactionSynchronizationManager
172: .isCurrentTransactionReadOnly();
173: return (txReadOnly ? Boolean.TRUE : null);
174: }
175:
176: }
|