001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.jdbc;
020:
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025:
026: /**
027: * Connection decorator that can configure some properties of the
028: * underlying connection. Understands the following properties:
029: * <ul>
030: * <li>QueryTimeout</li>
031: * <li>TransactionIsolation({@link Connection} constants)</li>
032: * <li>AutoCommit</li>
033: * </ul>
034: *
035: * @author Abe White
036: * @nojavadoc
037: */
038: public class ConfiguringConnectionDecorator implements
039: ConnectionDecorator {
040:
041: private int _isolation = -1;
042: private int _queryTimeout = -1;
043: private Boolean _autoCommit = null;
044:
045: /**
046: * The number of seconds to wait for a query to execute before
047: * terminating it.
048: */
049: public int getQueryTimeout() {
050: return _queryTimeout;
051: }
052:
053: /**
054: * The number of seconds to wait for a query to execute before
055: * terminating it.
056: */
057: public void setQueryTimeout(int timeout) {
058: _queryTimeout = timeout;
059: }
060:
061: /**
062: * The transaction isolation level.
063: */
064: public int getTransactionIsolation() {
065: return _isolation;
066: }
067:
068: /**
069: * The transaction isolation level.
070: */
071: public void setTransactionIsolation(int isolation) {
072: _isolation = isolation;
073: }
074:
075: /**
076: * Whether auto commit should be on. Use null to keep
077: * the connection's default setting. Defaults to null.
078: */
079: public Boolean getAutoCommit() {
080: return _autoCommit;
081: }
082:
083: /**
084: * Whether auto commit should be on. Use null to keep
085: * the connection's default setting. Defaults to null.
086: */
087: public void setAutoCommit(Boolean autoCommit) {
088: _autoCommit = autoCommit;
089: }
090:
091: public Connection decorate(Connection conn) throws SQLException {
092: if (_isolation == Connection.TRANSACTION_NONE
093: || _queryTimeout != -1 || _autoCommit != null)
094: conn = new ConfiguringConnection(conn);
095: if (_isolation != -1
096: && _isolation != Connection.TRANSACTION_NONE)
097: conn.setTransactionIsolation(_isolation);
098: return conn;
099: }
100:
101: /**
102: * Decorator to configure connection components correctly.
103: */
104: private class ConfiguringConnection extends DelegatingConnection {
105:
106: private boolean _curAutoCommit = false;
107:
108: public ConfiguringConnection(Connection conn)
109: throws SQLException {
110: super (conn);
111: if (_autoCommit != null) {
112: _curAutoCommit = ConfiguringConnection.this
113: .getAutoCommit();
114: if (_curAutoCommit != _autoCommit.booleanValue())
115: setAutoCommit(_autoCommit.booleanValue());
116: }
117: }
118:
119: public void setAutoCommit(boolean auto) throws SQLException {
120: if (_isolation != TRANSACTION_NONE) {
121: super .setAutoCommit(auto);
122: _curAutoCommit = auto;
123: }
124: }
125:
126: public void commit() throws SQLException {
127: if (_isolation != TRANSACTION_NONE)
128: super .commit();
129: if (_autoCommit != null
130: && _autoCommit.booleanValue() != _curAutoCommit)
131: setAutoCommit(_autoCommit.booleanValue());
132: }
133:
134: public void rollback() throws SQLException {
135: if (_isolation != TRANSACTION_NONE)
136: super .rollback();
137: if (_autoCommit != null
138: && _autoCommit.booleanValue() != _curAutoCommit)
139: setAutoCommit(_autoCommit.booleanValue());
140: }
141:
142: protected PreparedStatement prepareStatement(String sql,
143: boolean wrap) throws SQLException {
144: PreparedStatement stmnt = super .prepareStatement(sql, wrap);
145: if (_queryTimeout != -1)
146: stmnt.setQueryTimeout(_queryTimeout);
147: return stmnt;
148: }
149:
150: protected PreparedStatement prepareStatement(String sql,
151: int rsType, int rsConcur, boolean wrap)
152: throws SQLException {
153: PreparedStatement stmnt = super .prepareStatement(sql,
154: rsType, rsConcur, wrap);
155: if (_queryTimeout != -1)
156: stmnt.setQueryTimeout(_queryTimeout);
157: return stmnt;
158: }
159:
160: protected Statement createStatement(boolean wrap)
161: throws SQLException {
162: Statement stmnt = super .createStatement(wrap);
163: if (_queryTimeout != -1)
164: stmnt.setQueryTimeout(_queryTimeout);
165: return stmnt;
166: }
167:
168: protected Statement createStatement(int rsType, int rsConcur,
169: boolean wrap) throws SQLException {
170: Statement stmnt = super .createStatement(rsType, rsConcur,
171: wrap);
172: if (_queryTimeout != -1)
173: stmnt.setQueryTimeout(_queryTimeout);
174: return stmnt;
175: }
176: }
177: }
|