KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > ConnectionImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29 package com.caucho.db.jdbc;
30
31 import com.caucho.db.Database;
32 import com.caucho.db.sql.Query;
33 import com.caucho.db.store.Transaction;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import java.sql.CallableStatement JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.SQLWarning JavaDoc;
40 import java.sql.Savepoint JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * The JDBC connection implementation.
49  */

50 public class ConnectionImpl implements java.sql.Connection JavaDoc {
51   private static final L10N L = new L10N(ConnectionImpl.class);
52   private static final Logger JavaDoc log = Log.open(ConnectionImpl.class);
53
54   private Database _db;
55   private PooledConnectionImpl _pooledConnection;
56   
57   private boolean _isClosed;
58   private boolean _isAutoCommit = true;
59
60   private Transaction _xa;
61
62   private StatementImpl _statement;
63   private ArrayList JavaDoc<StatementImpl> _statements;
64   
65   public ConnectionImpl(PooledConnectionImpl pooledConnection)
66   {
67     _pooledConnection = pooledConnection;
68     _db = pooledConnection.getDatabase();
69
70     if (_db == null)
71       throw new NullPointerException JavaDoc();
72   }
73   
74   public ConnectionImpl(Database db)
75   {
76     _db = db;
77
78     if (_db == null)
79       throw new NullPointerException JavaDoc();
80   }
81
82   Database getDatabase()
83   {
84     return _db;
85   }
86
87   public void clearWarnings()
88   {
89   }
90
91   public void setTransaction(Transaction xa)
92   {
93     _xa = xa;
94   }
95
96   public Transaction getTransaction()
97   {
98     if (_isAutoCommit) {
99       Transaction xa = Transaction.create(this);
100       // XXX: value?
101
// xa.setTransactionTimeout(15000);
102
xa.setAutoCommit(true);
103       return xa;
104     }
105     else if (_xa == null) {
106       _xa = Transaction.create(this);
107       
108       if (log.isLoggable(Level.FINER))
109     log.finer("start transaction " + this + " " + _xa);
110     }
111
112     _xa.setAutoCommit(false);
113
114     return _xa;
115   }
116   
117   public void commit()
118     throws SQLException JavaDoc
119   {
120     if (log.isLoggable(Level.FINER))
121       log.finer("commit " + this + " " + _xa);
122     
123     Transaction xa = _xa;
124     _xa = null;
125     
126     if (xa != null)
127       xa.commit();
128   }
129
130   public void rollback()
131     throws SQLException JavaDoc
132   {
133     Transaction xa = _xa;
134     _xa = null;
135
136     if (xa != null) {
137       if (log.isLoggable(Level.FINER))
138     log.finer("rollback " + this + " " + _xa);
139     
140       xa.rollback();
141     }
142   }
143
144   public java.sql.Statement JavaDoc createStatement()
145     throws SQLException JavaDoc
146   {
147     if (_db == null)
148       throw new SQLException JavaDoc(L.l("Connection is already closed"));
149     
150     StatementImpl stmt = new StatementImpl(this);
151
152     if (_statement == null)
153       _statement = stmt;
154     else {
155       if (_statements == null)
156     _statements = new ArrayList JavaDoc<StatementImpl>();
157       _statements.add(stmt);
158     }
159     
160     return stmt;
161   }
162
163   public java.sql.Statement JavaDoc createStatement(int resultSetType,
164                         int resultSetConcurrency)
165     throws SQLException JavaDoc
166   {
167     return createStatement();
168   }
169
170   public boolean getAutoCommit()
171   {
172     return _isAutoCommit;
173   }
174
175   public void setAutoCommit(boolean autoCommit)
176     throws SQLException JavaDoc
177   {
178     if (! _isAutoCommit && autoCommit) {
179       Transaction xa = _xa;
180       _xa = null;
181     
182       if (xa != null)
183     xa.commit();
184     }
185     
186     _isAutoCommit = autoCommit;
187   }
188
189   public String JavaDoc getCatalog()
190   {
191     return null;
192   }
193
194   public void setCatalog(String JavaDoc catalog)
195     throws SQLException JavaDoc
196   {
197   }
198
199   public java.sql.DatabaseMetaData JavaDoc getMetaData()
200     throws SQLException JavaDoc
201   {
202     return new DatabaseMetaDataImpl(this);
203   }
204
205   public int getTransactionIsolation()
206   {
207     return TRANSACTION_NONE;
208   }
209
210   public void setTransactionIsolation(int level)
211   {
212   }
213
214   public Map JavaDoc getTypeMap()
215   {
216     return null;
217   }
218
219   public void setTypeMap(Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
220   {
221   }
222
223   public SQLWarning JavaDoc getWarnings()
224   {
225     return null;
226   }
227
228   public boolean isClosed()
229   {
230     return _isClosed;
231   }
232
233   public boolean isReadOnly()
234   {
235     return false;
236   }
237
238   public void setReadOnly(boolean readOnly)
239   {
240   }
241
242   public String JavaDoc nativeSQL(String JavaDoc sql)
243   {
244     return null;
245   }
246
247   public CallableStatement JavaDoc prepareCall(String JavaDoc sql)
248     throws SQLException JavaDoc
249   {
250     return null;
251   }
252
253   public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
254                                        int resultSetConcurrency)
255     throws SQLException JavaDoc
256   {
257     return null;
258   }
259
260   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql)
261     throws SQLException JavaDoc
262   {
263     return prepareStatementImpl(sql);
264   }
265   
266   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
267                                                      int autoGeneratedKeys)
268     throws SQLException JavaDoc
269   {
270     PreparedStatementImpl pstmt = prepareStatementImpl(sql);
271
272     if (autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS)
273       pstmt.setReturnGeneratedKeys(true);
274     
275     return pstmt;
276   }
277
278   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
279                                                      int []columnIndices)
280     throws SQLException JavaDoc
281   {
282     PreparedStatementImpl pstmt = prepareStatementImpl(sql);
283
284     pstmt.setReturnGeneratedKeys(true);
285     
286     return pstmt;
287   }
288
289   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
290                                                      String JavaDoc []columnNames)
291     throws SQLException JavaDoc
292   {
293     PreparedStatementImpl pstmt = prepareStatementImpl(sql);
294
295     pstmt.setReturnGeneratedKeys(true);
296     
297     return pstmt;
298   }
299
300   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
301                                                      int resultSetType,
302                                                      int resultSetConcurrency)
303     throws SQLException JavaDoc
304   {
305     return prepareStatement(sql);
306   }
307
308   public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
309                                                      int resultSetType,
310                                                      int resultSetConcurrency,
311                                                      int resultSetHoldability)
312     throws SQLException JavaDoc
313   {
314     return prepareStatement(sql);
315   }
316   
317   /**
318    * Prepares the statement implementation.
319    */

320   private PreparedStatementImpl prepareStatementImpl(String JavaDoc sql)
321     throws SQLException JavaDoc
322   {
323     Query query = _db.parseQuery(sql);
324     
325     PreparedStatementImpl stmt = new PreparedStatementImpl(this, query);
326
327     if (_statement == null)
328       _statement = stmt;
329     else {
330       if (_statements == null)
331     _statements = new ArrayList JavaDoc<StatementImpl>();
332       _statements.add(stmt);
333     }
334     
335     return stmt;
336   }
337
338   public void rollback(Savepoint JavaDoc savepoint)
339     throws SQLException JavaDoc
340   {
341   }
342
343   public void releaseSavepoint(Savepoint JavaDoc savepoint)
344     throws SQLException JavaDoc
345   {
346   }
347
348   public Savepoint JavaDoc setSavepoint(String JavaDoc savepoint)
349     throws SQLException JavaDoc
350   {
351     return null;
352   }
353
354   public Savepoint JavaDoc setSavepoint()
355     throws SQLException JavaDoc
356   {
357     return null;
358   }
359
360   public int getHoldability()
361     throws SQLException JavaDoc
362   {
363     return 0;
364   }
365
366   public void setHoldability(int hold)
367     throws SQLException JavaDoc
368   {
369   }
370
371   public java.sql.Statement JavaDoc createStatement(int resultSetType,
372                                             int resultSetConcurrency,
373                                             int resultSetHoldability)
374     throws SQLException JavaDoc
375   {
376     return createStatement();
377   }
378
379   public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
380                                        int resultSetConcurrency,
381                                        int holdability)
382     throws SQLException JavaDoc
383   {
384     return null;
385   }
386
387   void closeStatement(StatementImpl stmt)
388   {
389     if (_statement == stmt)
390       _statement = null;
391
392     if (_statements != null)
393       _statements.remove(stmt);
394   }
395
396   public void close()
397     throws SQLException JavaDoc
398   {
399     synchronized (this) {
400       if (_isClosed)
401     return;
402       
403       _isClosed = true;
404       _db = null;
405     }
406
407     StatementImpl stmt = _statement;
408     _statement = null;
409     
410     if (stmt != null)
411       _statement = null;
412
413     if (_statements != null) {
414       for (int i = 0; i < _statements.size(); i++) {
415     stmt = _statements.get(i);
416
417     stmt.close();
418       }
419     }
420
421     if (_pooledConnection != null)
422       _pooledConnection.closeEvent(this);
423   }
424 }
425
Popular Tags