KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > c3d > DCConnection


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.c3d;
56
57 import java.io.Serializable JavaDoc;
58 import java.sql.CallableStatement JavaDoc;
59 import java.sql.Connection JavaDoc;
60 import java.sql.DatabaseMetaData JavaDoc;
61 import java.sql.PreparedStatement JavaDoc;
62 import java.sql.Savepoint JavaDoc;
63 import java.sql.SQLException JavaDoc;
64 import java.sql.SQLWarning JavaDoc;
65 import java.sql.Statement JavaDoc;
66 import java.util.Map JavaDoc;
67
68 import org.apache.log4j.Logger;
69
70 import org.lateralnz.common.util.DAOUtils;
71
72 /**
73  * an implementation of database connection. This also implements XAResource so
74  * we can provide some necessary functionality to the simpletrans package -- some of
75  * this stuff is a bit on the hacky side (with the excuse that some of the trans api docs
76  * are less than clear, and hardly what one could call straightforward to implement).
77  * I recommend -not- using this package if you're using any other transaction manager,
78  * because it is not correctly implemented (at the moment).
79  */

80 public class DCConnection implements Connection JavaDoc, Serializable JavaDoc {
81   private static final Logger log = Logger.getLogger(DCConnection.class.getName());
82   
83   private boolean autocommit = false;
84   private boolean closed = false;
85   protected boolean blockCache = false;
86   private Connection JavaDoc conn;
87   private DatabaseEngine dbengine;
88   
89   public DCConnection(Connection JavaDoc conn, DatabaseEngine dbengine) throws SQLException JavaDoc {
90     this.conn = conn;
91     this.dbengine = dbengine;
92   }
93
94   protected Connection JavaDoc getRealConnection() {
95     return conn;
96   }
97   
98   DatabaseEngine getDatabaseEngine() {
99     return dbengine;
100   }
101   
102   public void clearWarnings() throws SQLException JavaDoc {
103     conn.clearWarnings();
104   }
105
106   public void close() throws SQLException JavaDoc {
107     blockCache = false;
108     dbengine.commit(this);
109     DAOUtils.close(conn);
110     closed = true;
111   }
112     
113   public void commit() throws SQLException JavaDoc {
114     boolean commit = true;
115     blockCache = false;
116     
117     try {
118       conn.commit();
119     }
120     catch (SQLException JavaDoc se) {
121       commit = false;
122       throw se;
123     }
124     finally {
125       if (commit) {
126         dbengine.commit(this);
127       }
128       else {
129         dbengine.rollback(this);
130       }
131     }
132   }
133   
134   public Statement JavaDoc createStatement() throws SQLException JavaDoc {
135     return createStatement(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
136   }
137   
138   public Statement JavaDoc createStatement(int resultsetType, int resultsetConcurrency) throws SQLException JavaDoc {
139     return createStatement(resultsetType, resultsetConcurrency, Integer.MIN_VALUE);
140   }
141
142   public Statement JavaDoc createStatement(int resultsetType, int resultsetConcurrency, int resultsetHoldability) throws SQLException JavaDoc {
143     return new DCStatement(this, resultsetType, resultsetConcurrency, resultsetHoldability);
144   }
145   
146   public void finalize() throws SQLException JavaDoc {
147     close();
148   }
149   
150   public boolean getAutoCommit() throws SQLException JavaDoc {
151     return conn.getAutoCommit();
152   }
153   
154   public String JavaDoc getCatalog() throws SQLException JavaDoc {
155     return conn.getCatalog();
156   }
157   
158   public int getHoldability() throws SQLException JavaDoc {
159     return conn.getHoldability();
160   }
161   
162   public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
163     return conn.getMetaData();
164   }
165   
166   public int getTransactionIsolation() throws SQLException JavaDoc {
167     return conn.getTransactionIsolation();
168   }
169   
170   public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
171     return conn.getTypeMap();
172   }
173   
174   public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
175     return conn.getWarnings();
176   }
177   
178   public boolean isClosed() throws SQLException JavaDoc {
179     return closed;
180   }
181   
182   public boolean isReadOnly() throws SQLException JavaDoc {
183     return conn.isReadOnly();
184   }
185   
186   public String JavaDoc nativeSQL(String JavaDoc str) throws SQLException JavaDoc {
187     throw new SQLException JavaDoc("operation not supported");
188   }
189   
190   public CallableStatement JavaDoc prepareCall(String JavaDoc str) throws SQLException JavaDoc {
191     return conn.prepareCall(str);
192     //return prepareCall(str, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
193
}
194   
195   public CallableStatement JavaDoc prepareCall(String JavaDoc str, int resultsetType, int resultsetConcurrency) throws SQLException JavaDoc {
196     return conn.prepareCall(str, resultsetType, resultsetConcurrency);
197     //return prepareCall(str, resultsetType, resultsetConcurrency, ResultSet.CLOSE_CURSORS_AT_COMMIT);
198
}
199   
200   public CallableStatement JavaDoc prepareCall(String JavaDoc str, int resultsetType, int resultsetConcurrency, int resultsetHoldability) throws SQLException JavaDoc {
201     return conn.prepareCall(str, resultsetType, resultsetConcurrency, resultsetHoldability);
202     //return new DCCallableStatement(this, str, resultsetType, resultsetConcurrency, resultsetHoldability);
203
}
204   
205   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str) throws SQLException JavaDoc {
206     return new DCPreparedStatement(this, str, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, null, null);
207   }
208
209   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str, int autoGeneratedKeys) throws SQLException JavaDoc {
210     return new DCPreparedStatement(this, str, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, autoGeneratedKeys, null, null);
211   }
212   
213   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str, int[] columnIndexes) throws SQLException JavaDoc {
214     return new DCPreparedStatement(this, str, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, columnIndexes, null);
215   }
216   
217   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str, String JavaDoc[] columnNames) throws SQLException JavaDoc {
218     return new DCPreparedStatement(this, str, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, null, columnNames);
219   }
220   
221   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str, int resultsetType, int resultsetConcurrency) throws SQLException JavaDoc {
222     return new DCPreparedStatement(this, str, resultsetType, resultsetConcurrency, Integer.MIN_VALUE, Integer.MIN_VALUE, null, null);
223   }
224   
225   public PreparedStatement JavaDoc prepareStatement(String JavaDoc str, int resultsetType, int resultsetConcurrency, int resultsetHoldability) throws SQLException JavaDoc {
226     return new DCPreparedStatement(this, str, resultsetType, resultsetConcurrency, resultsetHoldability, Integer.MIN_VALUE, null, null);
227   }
228     
229   public void releaseSavepoint(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
230     throw new SQLException JavaDoc("operation not supported");
231   }
232   
233   public void rollback() throws SQLException JavaDoc {
234     blockCache = false;
235     try {
236       conn.rollback();
237     }
238     finally {
239       dbengine.rollback(this);
240     }
241   }
242   
243  /**
244   * rollback to a savepoint is not supported by this connection
245   */

246   public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
247     throw new SQLException JavaDoc("operation not supported");
248   }
249
250   public void setAutoCommit(boolean param) throws SQLException JavaDoc {
251     this.autocommit = param;
252     conn.setAutoCommit(param);
253   }
254   
255   public void setCatalog(String JavaDoc str) throws SQLException JavaDoc {
256     conn.setCatalog(str);
257   }
258   
259   public void setHoldability(int param) throws SQLException JavaDoc {
260     conn.setHoldability(param);
261   }
262   
263   public void setReadOnly(boolean param) throws SQLException JavaDoc {
264     conn.setReadOnly(param);
265   }
266   
267   public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
268     throw new SQLException JavaDoc("operation not supported");
269   }
270   
271   public Savepoint JavaDoc setSavepoint(String JavaDoc str) throws SQLException JavaDoc {
272     throw new SQLException JavaDoc("operation not supported");
273   }
274   
275   public void setTransactionIsolation(int param) throws SQLException JavaDoc {
276     conn.setTransactionIsolation(param);
277   }
278   
279   public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
280     conn.setTypeMap(map);
281   }
282   
283 }
Popular Tags