KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rmijdbc > RJConnectionServer


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  *
6  * @version 1.0
7  * @author Pierre-Yves Gibello (pierreyves.gibello@experlog.com)
8  * Additional SSL Support
9  * Douglas Hammond(djhammond@sympatico.ca)
10  */

11
12 package org.objectweb.rmijdbc;
13
14 import java.sql.*;
15
16 import java.rmi.*;
17 import java.rmi.server.UnicastRemoteObject JavaDoc;
18 import java.rmi.server.Unreferenced JavaDoc;
19
20 /**InstantDB
21 import javax.transaction.xa.*;
22 import org.enhydra.instantdb.jdbc.ConnectionExtensions;
23 InstantDB**/

24
25 /**
26  * <P>A Connection represents a session with a specific
27  * database. Within the context of a Connection, SQL statements are
28  * executed and results are returned.
29  *
30  * <P>A Connection's database is able to provide information
31  * describing its tables, its supported SQL grammar, its stored
32  * procedures, the capabilities of this connection, etc. This
33  * information is obtained with the getMetaData method.
34  *
35  * <P><B>Note:</B> By default the Connection automatically commits
36  * changes after executing each statement. If auto commit has been
37  * disabled, an explicit commit must be done or database changes will
38  * not be saved.
39  *
40  * @see DriverManager#getConnection
41  * @see Statement
42  * @see ResultSet
43  * @see DatabaseMetaData
44  */

45 public class RJConnectionServer
46 extends UnicastRemoteObject JavaDoc
47 implements RJConnectionInterface, Unreferenced JavaDoc
48 {
49
50   java.sql.Connection JavaDoc jdbcConnection_;
51
52   public RJConnectionServer(java.sql.Connection JavaDoc c) throws RemoteException {
53     super(RJJdbcServer.rmiJdbcListenerPort, RJJdbcServer.rmiClientSocketFactory, RJJdbcServer.rmiServerSocketFactory);
54     jdbcConnection_ = c;
55   }
56
57
58   public void unreferenced() {
59     try {
60       if(jdbcConnection_ != null) {
61         // Try to rollback transaction if any...
62
try {
63           if(! jdbcConnection_.getAutoCommit())
64             jdbcConnection_.rollback();
65         } catch(Exception JavaDoc e) {
66           // ignore
67
}
68         jdbcConnection_.close();
69         jdbcConnection_ = null;
70       }
71     } catch(Exception JavaDoc e) {
72       jdbcConnection_ = null;
73     }
74     //System.out.println("RJConnectionServer: unreferenced() -> doing garbage collection");
75
Runtime.getRuntime().gc();
76   }
77
78
79   protected void finalize() throws Throwable JavaDoc {
80     if (jdbcConnection_ != null) {
81       jdbcConnection_.close();
82     }
83     //System.out.println("RJConnectionServer: finalize() -> doing garbage collection");
84
Runtime.getRuntime().gc();
85   }
86
87   /**
88    * SQL statements without parameters are normally
89    * executed using Statement objects. If the same SQL statement
90    * is executed many times, it is more efficient to use a
91    * PreparedStatement
92    *
93    * @return a new Statement object
94    */

95   public RJStatementInterface createStatement()
96   throws RemoteException, SQLException {
97     return new RJStatementServer(jdbcConnection_.createStatement());
98   }
99
100     /**
101      * A SQL statement with or without IN parameters can be
102      * pre-compiled and stored in a PreparedStatement object. This
103      * object can then be used to efficiently execute this statement
104      * multiple times.
105      *
106      * <P><B>Note:</B> This method is optimized for handling
107      * parametric SQL statements that benefit from precompilation. If
108      * the driver supports precompilation, prepareStatement will send
109      * the statement to the database for precompilation. Some drivers
110      * may not support precompilation. In this case, the statement may
111      * not be sent to the database until the PreparedStatement is
112      * executed. This has no direct affect on users; however, it does
113      * affect which method throws certain java.rmi.RemoteExceptions.
114      *
115      * @param sql a SQL statement that may contain one or more '?' IN
116      * parameter placeholders
117      *
118      * @return a new PreparedStatement object containing the
119      * pre-compiled statement
120      */

121   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql)
122   throws RemoteException, SQLException {
123     return new RJPreparedStatementServer(jdbcConnection_.prepareStatement(sql));
124   }
125
126     /**
127      * A SQL stored procedure call statement is handled by creating a
128      * CallableStatement for it. The CallableStatement provides
129      * methods for setting up its IN and OUT parameters, and
130      * methods for executing it.
131      *
132      * <P><B>Note:</B> This method is optimized for handling stored
133      * procedure call statements. Some drivers may send the call
134      * statement to the database when the prepareCall is done; others
135      * may wait until the CallableStatement is executed. This has no
136      * direct affect on users; however, it does affect which method
137      * throws certain java.rmi.RemoteExceptions.
138      *
139      * @param sql a SQL statement that may contain one or more '?'
140      * parameter placeholders. Typically this statement is a JDBC
141      * function call escape string.
142      *
143      * @return a new CallableStatement object containing the
144      * pre-compiled SQL statement
145      */

146   public RJCallableStatementInterface prepareCall(String JavaDoc sql)
147   throws RemoteException, SQLException {
148     return new RJCallableStatementServer(jdbcConnection_.prepareCall(sql));
149   }
150                         
151   /**
152    * A driver may convert the JDBC sql grammar into its system's
153    * native SQL grammar prior to sending it; nativeSQL returns the
154    * native form of the statement that the driver would have sent.
155    *
156    * @param sql a SQL statement that may contain one or more '?'
157    * parameter placeholders
158    *
159    * @return the native form of this statement
160    */

161   public String JavaDoc nativeSQL(String JavaDoc sql) throws RemoteException, SQLException {
162     return jdbcConnection_.nativeSQL(sql);
163   }
164
165   /**
166    * If a connection is in auto-commit mode, then all its SQL
167    * statements will be executed and committed as individual
168    * transactions. Otherwise, its SQL statements are grouped into
169    * transactions that are terminated by either commit() or
170    * rollback(). By default, new connections are in auto-commit
171    * mode.
172    *
173    * The commit occurs when the statement completes or the next
174    * execute occurs, whichever comes first. In the case of
175    * statements returning a ResultSet, the statement completes when
176    * the last row of the ResultSet has been retrieved or the
177    * ResultSet has been closed. In advanced cases, a single
178    * statement may return multiple results as well as output
179    * parameter values. Here the commit occurs when all results and
180    * output param values have been retrieved.
181    *
182    * @param autoCommit true enables auto-commit; false disables
183    * auto-commit.
184    */

185   public void setAutoCommit(boolean autoCommit)
186   throws RemoteException, SQLException {
187     jdbcConnection_.setAutoCommit(autoCommit);
188   }
189
190   /**
191    * Get the current auto-commit state.
192    * @return Current state of auto-commit mode.
193    * @see #setAutoCommit
194    */

195   public boolean getAutoCommit() throws RemoteException, SQLException {
196     return jdbcConnection_.getAutoCommit();
197   }
198
199   /**
200    * Commit makes all changes made since the previous
201    * commit/rollback permanent and releases any database locks
202    * currently held by the Connection. This method should only be
203    * used when auto commit has been disabled.
204    *
205    * @see #setAutoCommit
206    */

207   public void commit() throws RemoteException, SQLException {
208     jdbcConnection_.commit();
209   }
210
211   /**
212    * Rollback drops all changes made since the previous
213    * commit/rollback and releases any database locks currently held
214    * by the Connection. This method should only be used when auto
215    * commit has been disabled.
216    *
217    * @see #setAutoCommit
218    */

219   public void rollback() throws RemoteException, SQLException {
220     jdbcConnection_.rollback();
221   }
222
223   /**
224    * In some cases, it is desirable to immediately release a
225    * Connection's database and JDBC resources instead of waiting for
226    * them to be automatically released; the close method provides this
227    * immediate release.
228    *
229    * <P><B>Note:</B> A Connection is automatically closed when it is
230    * garbage collected. Certain fatal errors also result in a closed
231    * Connection.
232    */

233   public void close() throws RemoteException, SQLException {
234     if(jdbcConnection_ != null) jdbcConnection_.close();
235     Runtime.getRuntime().gc(); // Request for garbage collection
236
}
237
238   /**
239    * Tests to see if a Connection is closed.
240    *
241    * @return true if the connection is closed; false if it's still open
242    */

243   public boolean isClosed() throws RemoteException, SQLException {
244     return jdbcConnection_.isClosed();
245   }
246
247   //======================================================================
248
// Advanced features:
249

250   /**
251     A Connection's database is able to provide information
252    * describing its tables, its supported SQL grammar, its stored
253    * procedures, the capabilities of this connection, etc. This
254    * information is made available through a DatabaseMetaData
255    * object.
256    *
257    * @return a RJDatabaseMetaData object for this Connection
258    */

259   public RJDatabaseMetaDataInterface getMetaData()
260   throws RemoteException, SQLException {
261     return new RJDatabaseMetaDataServer(jdbcConnection_.getMetaData());
262   }
263
264   /**
265    * You can put a connection in read-only mode as a hint to enable
266    * database optimizations.
267    *
268    * <P><B>Note:</B> setReadOnly cannot be called while in the
269    * middle of a transaction.
270    *
271    * @param readOnly true enables read-only mode; false disables
272    * read-only mode.
273    */

274   public void setReadOnly(boolean readOnly)
275   throws RemoteException, SQLException {
276     jdbcConnection_.setReadOnly(readOnly);
277   }
278
279   /**
280    * Tests to see if the connection is in read-only mode.
281    *
282    * @return true if connection is read-only
283    */

284   public boolean isReadOnly() throws RemoteException, SQLException {
285     return jdbcConnection_.isReadOnly();
286   }
287
288   /**
289    * A sub-space of this Connection's database may be selected by setting a
290    * catalog name. If the driver does not support catalogs it will
291    * silently ignore this request.
292    */

293   public void setCatalog(String JavaDoc catalog) throws RemoteException, SQLException {
294     jdbcConnection_.setCatalog(catalog);
295   }
296
297     /**
298      * Return the Connection's current catalog name.
299      *
300      * @return the current catalog name or null
301      */

302   public String JavaDoc getCatalog() throws RemoteException, SQLException {
303     return jdbcConnection_.getCatalog();
304   }
305
306   /**
307    * You can call this method to try to change the transaction
308    * isolation level using one of the TRANSACTION_* values.
309    *
310    * <P><B>Note:</B> setTransactionIsolation cannot be called while
311    * in the middle of a transaction.
312    *
313    * @param level one of the TRANSACTION_* isolation values with the
314    * exception of TRANSACTION_NONE; some databases may not support
315    * other values
316    *
317    * @see DatabaseMetaData#supportsTransactionIsolationLevel
318    */

319   public void setTransactionIsolation(int level)
320   throws RemoteException, SQLException {
321     jdbcConnection_.setTransactionIsolation(level);
322   }
323
324   /**
325    * Get this Connection's current transaction isolation mode.
326    *
327    * @return the current TRANSACTION_* mode value
328    */

329   public int getTransactionIsolation() throws RemoteException, SQLException {
330     return jdbcConnection_.getTransactionIsolation();
331   }
332
333   /**
334    * The first warning reported by calls on this Connection is
335    * returned.
336    *
337    * <P><B>Note:</B> Subsequent warnings will be chained to this
338    * SQLWarning.
339    *
340    * @return the first SQLWarning or null
341    */

342   public SQLWarning getWarnings() throws RemoteException, SQLException {
343     return jdbcConnection_.getWarnings();
344   }
345
346   /**
347    * After this call, getWarnings returns null until a new warning is
348    * reported for this Connection.
349    */

350   public void clearWarnings() throws RemoteException, SQLException {
351     jdbcConnection_.clearWarnings();
352   }
353
354 // JDBC 2.0 methods
355
// Added by Peter Hearty, Aug 2000, peter.hearty@lutris.com.
356

357   public void setTypeMap(java.util.Map JavaDoc map)
358   throws RemoteException, SQLException {
359     jdbcConnection_.setTypeMap(map);
360   }
361
362   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql,
363   int resultSetType, int resultSetConcurrency)
364   throws RemoteException, SQLException {
365      return new RJPreparedStatementServer(
366       jdbcConnection_.prepareStatement(sql, resultSetType,
367       resultSetConcurrency));
368   }
369
370   public RJCallableStatementInterface prepareCall(String JavaDoc sql, int resultSetType,
371   int resultSetConcurrency) throws RemoteException, SQLException {
372     return new RJCallableStatementServer(jdbcConnection_.prepareCall(sql,
373      resultSetType,resultSetConcurrency));
374   }
375
376
377   public java.util.Map JavaDoc getTypeMap() throws RemoteException, SQLException {
378     return jdbcConnection_.getTypeMap();
379   }
380
381   public RJStatementInterface createStatement(int resultSetType,
382   int resultSetConcurrency) throws RemoteException, SQLException {
383     return new RJStatementServer(jdbcConnection_.createStatement(resultSetType,
384      resultSetConcurrency));
385   }
386
387 /**InstantDB
388
389 // InstantDB specific extensions.
390
391 public Properties getProperties () throws java.rmi.RemoteException, SQLException
392   {
393         return ((ConnectionExtensions)jdbcConnection_).getProperties ();
394   }
395
396 public Object getLastValueInserted (
397                         String tableName, String columnName) throws java.rmi.RemoteException, SQLException
398   {
399         return ((ConnectionExtensions)jdbcConnection_).getLastValueInserted (tableName, columnName);
400   }
401
402 public void startGlobalTransaction (Xid xid) throws java.rmi.RemoteException, XAException
403   {
404         ((ConnectionExtensions)jdbcConnection_).startGlobalTransaction (xid);
405   }
406
407 public int prepare() throws java.rmi.RemoteException, XAException
408   {
409         return ((ConnectionExtensions)jdbcConnection_).prepare ();
410   }
411
412 public String getDatabaseId() throws java.rmi.RemoteException, SQLException
413   {
414         return ((ConnectionExtensions)jdbcConnection_).getDatabaseId();
415   }
416 InstantDB**/

417
418
419   //--------------------------JDBC 3.0-----------------------------
420

421   public void setHoldability(int holdability)
422   throws java.rmi.RemoteException JavaDoc, SQLException {
423     jdbcConnection_.setHoldability(holdability);
424   }
425
426   public int getHoldability() throws java.rmi.RemoteException JavaDoc, SQLException {
427     return jdbcConnection_.getHoldability();
428   }
429
430   public RJSavepointInterface setSavepoint()
431   throws java.rmi.RemoteException JavaDoc, SQLException {
432     return new RJSavepointServer(jdbcConnection_.setSavepoint());
433   }
434
435   public RJSavepointInterface setSavepoint(String JavaDoc name)
436   throws java.rmi.RemoteException JavaDoc, SQLException {
437     return new RJSavepointServer(jdbcConnection_.setSavepoint(name));
438   }
439
440   public void rollback(Savepoint savepoint)
441   throws java.rmi.RemoteException JavaDoc, SQLException {
442     jdbcConnection_.rollback(savepoint);
443   }
444
445   public void releaseSavepoint(Savepoint savepoint)
446   throws java.rmi.RemoteException JavaDoc, SQLException {
447     jdbcConnection_.releaseSavepoint(savepoint);
448   }
449
450   public RJStatementInterface createStatement(int resultSetType,
451   int resultSetConcurrency, int resultSetHoldability)
452   throws java.rmi.RemoteException JavaDoc, SQLException {
453     return new RJStatementServer(
454      jdbcConnection_.createStatement(resultSetType, resultSetConcurrency,
455      resultSetHoldability));
456   }
457
458   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql,
459   int resultSetType, int resultSetConcurrency, int resultSetHoldability)
460   throws java.rmi.RemoteException JavaDoc, SQLException {
461     return new RJPreparedStatementServer(
462      jdbcConnection_.prepareStatement(sql, resultSetType,
463      resultSetConcurrency, resultSetHoldability));
464   }
465
466   public RJCallableStatementInterface prepareCall(String JavaDoc sql, int resultSetType,
467   int resultSetConcurrency, int resultSetHoldability)
468   throws java.rmi.RemoteException JavaDoc, SQLException {
469     return new RJCallableStatementServer(
470      jdbcConnection_.prepareCall(sql, resultSetType,
471      resultSetConcurrency, resultSetHoldability));
472   }
473
474   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql,
475   int autoGeneratedKeys) throws java.rmi.RemoteException JavaDoc, SQLException {
476     return new RJPreparedStatementServer(
477      jdbcConnection_.prepareStatement(sql, autoGeneratedKeys));
478   }
479
480   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql,
481   int columnIndexes[]) throws java.rmi.RemoteException JavaDoc, SQLException {
482     return new RJPreparedStatementServer(
483      jdbcConnection_.prepareStatement(sql, columnIndexes));
484   }
485
486   public RJPreparedStatementInterface prepareStatement(String JavaDoc sql,
487   String JavaDoc columnNames[]) throws java.rmi.RemoteException JavaDoc, SQLException {
488     return new RJPreparedStatementServer(
489      jdbcConnection_.prepareStatement(sql, columnNames));
490   }
491
492 };
493
494
Popular Tags