001: /* ====================================================================
002: * The LateralNZ Software License, Version 1.0
003: *
004: * Copyright (c) 2003 LateralNZ. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by
021: * LateralNZ (http://www.lateralnz.org/) and other third parties."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "LateralNZ" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please
028: * contact oss@lateralnz.org.
029: *
030: * 5. Products derived from this software may not be called "Panther",
031: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
032: * "LATERALNZ" appear in their name, without prior written
033: * permission of LateralNZ.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of LateralNZ. For more
051: * information on Lateral, please see http://www.lateralnz.com/ or
052: * http://www.lateralnz.org
053: *
054: */
055: package org.lateralnz.c3d;
056:
057: import java.io.Serializable;
058: import java.sql.CallableStatement;
059: import java.sql.Connection;
060: import java.sql.DatabaseMetaData;
061: import java.sql.PreparedStatement;
062: import java.sql.Savepoint;
063: import java.sql.SQLException;
064: import java.sql.SQLWarning;
065: import java.sql.Statement;
066: import java.util.Map;
067:
068: import org.apache.log4j.Logger;
069:
070: import org.lateralnz.common.util.DAOUtils;
071:
072: /**
073: * an implementation of database connection. This also implements XAResource so
074: * we can provide some necessary functionality to the simpletrans package -- some of
075: * this stuff is a bit on the hacky side (with the excuse that some of the trans api docs
076: * are less than clear, and hardly what one could call straightforward to implement).
077: * I recommend -not- using this package if you're using any other transaction manager,
078: * because it is not correctly implemented (at the moment).
079: */
080: public class DCConnection implements Connection, Serializable {
081: private static final Logger log = Logger
082: .getLogger(DCConnection.class.getName());
083:
084: private boolean autocommit = false;
085: private boolean closed = false;
086: protected boolean blockCache = false;
087: private Connection conn;
088: private DatabaseEngine dbengine;
089:
090: public DCConnection(Connection conn, DatabaseEngine dbengine)
091: throws SQLException {
092: this .conn = conn;
093: this .dbengine = dbengine;
094: }
095:
096: protected Connection getRealConnection() {
097: return conn;
098: }
099:
100: DatabaseEngine getDatabaseEngine() {
101: return dbengine;
102: }
103:
104: public void clearWarnings() throws SQLException {
105: conn.clearWarnings();
106: }
107:
108: public void close() throws SQLException {
109: blockCache = false;
110: dbengine.commit(this );
111: DAOUtils.close(conn);
112: closed = true;
113: }
114:
115: public void commit() throws SQLException {
116: boolean commit = true;
117: blockCache = false;
118:
119: try {
120: conn.commit();
121: } catch (SQLException se) {
122: commit = false;
123: throw se;
124: } finally {
125: if (commit) {
126: dbengine.commit(this );
127: } else {
128: dbengine.rollback(this );
129: }
130: }
131: }
132:
133: public Statement createStatement() throws SQLException {
134: return createStatement(Integer.MIN_VALUE, Integer.MIN_VALUE,
135: Integer.MIN_VALUE);
136: }
137:
138: public Statement createStatement(int resultsetType,
139: int resultsetConcurrency) throws SQLException {
140: return createStatement(resultsetType, resultsetConcurrency,
141: Integer.MIN_VALUE);
142: }
143:
144: public Statement createStatement(int resultsetType,
145: int resultsetConcurrency, int resultsetHoldability)
146: throws SQLException {
147: return new DCStatement(this , resultsetType,
148: resultsetConcurrency, resultsetHoldability);
149: }
150:
151: public void finalize() throws SQLException {
152: close();
153: }
154:
155: public boolean getAutoCommit() throws SQLException {
156: return conn.getAutoCommit();
157: }
158:
159: public String getCatalog() throws SQLException {
160: return conn.getCatalog();
161: }
162:
163: public int getHoldability() throws SQLException {
164: return conn.getHoldability();
165: }
166:
167: public DatabaseMetaData getMetaData() throws SQLException {
168: return conn.getMetaData();
169: }
170:
171: public int getTransactionIsolation() throws SQLException {
172: return conn.getTransactionIsolation();
173: }
174:
175: public Map getTypeMap() throws SQLException {
176: return conn.getTypeMap();
177: }
178:
179: public SQLWarning getWarnings() throws SQLException {
180: return conn.getWarnings();
181: }
182:
183: public boolean isClosed() throws SQLException {
184: return closed;
185: }
186:
187: public boolean isReadOnly() throws SQLException {
188: return conn.isReadOnly();
189: }
190:
191: public String nativeSQL(String str) throws SQLException {
192: throw new SQLException("operation not supported");
193: }
194:
195: public CallableStatement prepareCall(String str)
196: throws SQLException {
197: return conn.prepareCall(str);
198: //return prepareCall(str, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
199: }
200:
201: public CallableStatement prepareCall(String str, int resultsetType,
202: int resultsetConcurrency) throws SQLException {
203: return conn.prepareCall(str, resultsetType,
204: resultsetConcurrency);
205: //return prepareCall(str, resultsetType, resultsetConcurrency, ResultSet.CLOSE_CURSORS_AT_COMMIT);
206: }
207:
208: public CallableStatement prepareCall(String str, int resultsetType,
209: int resultsetConcurrency, int resultsetHoldability)
210: throws SQLException {
211: return conn.prepareCall(str, resultsetType,
212: resultsetConcurrency, resultsetHoldability);
213: //return new DCCallableStatement(this, str, resultsetType, resultsetConcurrency, resultsetHoldability);
214: }
215:
216: public PreparedStatement prepareStatement(String str)
217: throws SQLException {
218: return new DCPreparedStatement(this , str, Integer.MIN_VALUE,
219: Integer.MIN_VALUE, Integer.MIN_VALUE,
220: Integer.MIN_VALUE, null, null);
221: }
222:
223: public PreparedStatement prepareStatement(String str,
224: int autoGeneratedKeys) throws SQLException {
225: return new DCPreparedStatement(this , str, Integer.MIN_VALUE,
226: Integer.MIN_VALUE, Integer.MIN_VALUE,
227: autoGeneratedKeys, null, null);
228: }
229:
230: public PreparedStatement prepareStatement(String str,
231: int[] columnIndexes) throws SQLException {
232: return new DCPreparedStatement(this , str, Integer.MIN_VALUE,
233: Integer.MIN_VALUE, Integer.MIN_VALUE,
234: Integer.MIN_VALUE, columnIndexes, null);
235: }
236:
237: public PreparedStatement prepareStatement(String str,
238: String[] columnNames) throws SQLException {
239: return new DCPreparedStatement(this , str, Integer.MIN_VALUE,
240: Integer.MIN_VALUE, Integer.MIN_VALUE,
241: Integer.MIN_VALUE, null, columnNames);
242: }
243:
244: public PreparedStatement prepareStatement(String str,
245: int resultsetType, int resultsetConcurrency)
246: throws SQLException {
247: return new DCPreparedStatement(this , str, resultsetType,
248: resultsetConcurrency, Integer.MIN_VALUE,
249: Integer.MIN_VALUE, null, null);
250: }
251:
252: public PreparedStatement prepareStatement(String str,
253: int resultsetType, int resultsetConcurrency,
254: int resultsetHoldability) throws SQLException {
255: return new DCPreparedStatement(this , str, resultsetType,
256: resultsetConcurrency, resultsetHoldability,
257: Integer.MIN_VALUE, null, null);
258: }
259:
260: public void releaseSavepoint(Savepoint savepoint)
261: throws SQLException {
262: throw new SQLException("operation not supported");
263: }
264:
265: public void rollback() throws SQLException {
266: blockCache = false;
267: try {
268: conn.rollback();
269: } finally {
270: dbengine.rollback(this );
271: }
272: }
273:
274: /**
275: * rollback to a savepoint is not supported by this connection
276: */
277: public void rollback(Savepoint savepoint) throws SQLException {
278: throw new SQLException("operation not supported");
279: }
280:
281: public void setAutoCommit(boolean param) throws SQLException {
282: this .autocommit = param;
283: conn.setAutoCommit(param);
284: }
285:
286: public void setCatalog(String str) throws SQLException {
287: conn.setCatalog(str);
288: }
289:
290: public void setHoldability(int param) throws SQLException {
291: conn.setHoldability(param);
292: }
293:
294: public void setReadOnly(boolean param) throws SQLException {
295: conn.setReadOnly(param);
296: }
297:
298: public Savepoint setSavepoint() throws SQLException {
299: throw new SQLException("operation not supported");
300: }
301:
302: public Savepoint setSavepoint(String str) throws SQLException {
303: throw new SQLException("operation not supported");
304: }
305:
306: public void setTransactionIsolation(int param) throws SQLException {
307: conn.setTransactionIsolation(param);
308: }
309:
310: public void setTypeMap(Map map) throws SQLException {
311: conn.setTypeMap(map);
312: }
313:
314: }
|