KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > InternalJDBCHelper


1 /**
2  * com.mckoi.database.InternalJDBCHelper 16 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import com.mckoi.database.jdbc.MConnection;
30 import com.mckoi.database.jdbc.DatabaseCallBack;
31 import com.mckoi.database.jdbc.DatabaseInterface;
32 import com.mckoi.database.jdbcserver.AbstractJDBCDatabaseInterface;
33
34 /**
35  * Helper and convenience methods and classes for creating a JDBC interface
36  * that has direct access to an open transaction of a DatabaseConnection.
37  * This class allows us to provide JDBC access to stored procedures from
38  * inside the engine.
39  *
40  * @author Tobias Downer
41  */

42
43 class InternalJDBCHelper {
44
45   /**
46    * Returns a java.sql.Connection object that is bound to the given
47    * DatabaseConnection object. Queries executed on the Connection alter
48    * the currently open transaction.
49    * <p>
50    * Note: It is assumed that the DatabaseConnection is locked in exclusive
51    * mode when a query is executed (eg. via the 'executeXXX' methods in
52    * Statement).
53    * <p>
54    * Note: Auto-commit is <b>DISABLED</b> for the SQL connection and can not
55    * be enabled.
56    */

57   static Connection JavaDoc createJDBCConnection(User user,
58                                          DatabaseConnection connection) {
59     InternalDatabaseInterface db_interface =
60                              new InternalDatabaseInterface(user, connection);
61     return new InternalConnection(connection, db_interface, 11, 4092000);
62   }
63
64   /**
65    * Disposes the JDBC Connection object returned by the 'createJDBCConnection'
66    * method. This should be called to free resources associated with the
67    * connection object.
68    * <p>
69    * After this has completed the given Connection object in invalidated.
70    */

71   static void disposeJDBCConnection(Connection JavaDoc jdbc_connection)
72                                                          throws SQLException JavaDoc {
73     InternalConnection connection = (InternalConnection) jdbc_connection;
74     // Dispose the connection.
75
connection.internalClose();
76   }
77
78
79
80   // ---------- Inner classes ----------
81

82   /**
83    * A derived java.sql.Connection class from MConnection. This class disables
84    * auto commit, and inherits case insensitivity from the parent
85    * DatabaseConnection.
86    * <p>
87    * The decision to disable auto-commit was because this connection will
88    * typically be used as a sub-process for executing a complete command.
89    * Disabling auto-commit makes handling an internal connection more user
90    * friendly. Also, toggling this flag in the DatabaseConnection in mid-
91    * command is probably a very bad idea.
92    */

93   private static class InternalConnection extends MConnection {
94
95     /**
96      * The DatabaseInterface for this connection.
97      */

98     private InternalDatabaseInterface internal_db_interface;
99
100     /**
101      * Constructs the internal java.sql.Connection.
102      */

103     public InternalConnection(DatabaseConnection db,
104                               InternalDatabaseInterface jdbc_interface,
105                               int cache_size, int max_size) {
106       super("", jdbc_interface, cache_size, max_size);
107       internal_db_interface = jdbc_interface;
108       setCaseInsensitiveIdentifiers(db.isInCaseInsensitiveMode());
109     }
110
111     /**
112      * Returns the InternalDatabaseInterface that is used in this
113      * connection.
114      */

115     InternalDatabaseInterface getDBInterface() {
116       return internal_db_interface;
117     }
118
119     /**
120      * Overwritten from MConnection - auto-commit is disabled and can not be
121      * enabled.
122      */

123     public void setAutoCommit(boolean status) throws SQLException JavaDoc {
124       if (status == true) {
125         throw new SQLException JavaDoc(
126                "Auto-commit can not be enabled for an internal connection.");
127       }
128     }
129
130     /**
131      * Overwritten from MConnection - auto-commit is disabled and can not be
132      * enabled.
133      */

134     public boolean getAutoCommit() throws SQLException JavaDoc {
135       return false;
136     }
137
138     /**
139      * Overwritten from MConnection - closing an internal connection is a
140      * no-op. An InternalConnection should only close when the underlying
141      * transaction closes.
142      * <p>
143      * To dispose an InternalConnection, use the static
144      * 'disposeJDBCConnection' method.
145      */

146     public void close() {
147       // IDEA: Perhaps we should use this as a hint to clear some caches
148
// and free up some memory.
149
}
150
151   }
152
153   /**
154    * An implementation of DatabaseInterface used to execute queries on the
155    * DatabaseConnection and return results to the JDBC client.
156    * <p>
157    * This is a thin implementation of jdbcserver.AbstractJDBCDatabaseInterface.
158    */

159   private static class InternalDatabaseInterface
160                                      extends AbstractJDBCDatabaseInterface {
161
162     /**
163      * The internal connection to the database.
164      */

165     private DatabaseConnection database;
166
167     /**
168      * Constructor.
169      */

170     public InternalDatabaseInterface(User user, DatabaseConnection db) {
171       super(db.getDatabase());
172       this.database = db;
173       init(user, db);
174     }
175
176     // ---------- Implemented from DatabaseInterface ----------
177

178     public boolean login(String JavaDoc default_schema,
179                          String JavaDoc username, String JavaDoc password,
180                          DatabaseCallBack call_back) throws SQLException JavaDoc {
181       // This should never be used for an internal connection.
182
throw new SQLException JavaDoc(
183                   "'login' is not supported for InterfaceDatabaseInterface");
184     }
185
186     public void dispose() throws SQLException JavaDoc {
187       internalDispose();
188     }
189
190   }
191
192 }
193
Popular Tags