KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > SSConnection


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * SSConnection.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.database;
34
35 import java.io.RandomAccessFile JavaDoc;
36 import java.sql.*;
37 import java.util.Map JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Properties JavaDoc;
41
42
43 public class SSConnection implements Connection {
44
45     private Database database;
46     private boolean autoCommit = true;
47     int isolationLevel = TRANSACTION_READ_COMMITTED; // see also getDefaultTransactionIsolation
48
int serializeCount;
49     private List JavaDoc commitPages = new ArrayList JavaDoc();
50     /** The time on which a transaction is starting. */
51     private long transactionTime;
52     private SSDatabaseMetaData metadata;
53     private int holdability;
54     final Logger log = new Logger();
55
56     SSConnection( Properties JavaDoc props ) throws SQLException{
57         String JavaDoc name = props.getProperty("dbpath");
58         boolean create = "true".equals(props.getProperty("create"));
59         database = Database.getDatabase(name, this, create);
60         metadata = new SSDatabaseMetaData(this);
61     }
62     
63     
64     /**
65      * @param returnNull If null is a valid return value for the case of not connected to a database.
66      * @throws SQLException If not connected and returnNull is false.
67      */

68     Database getDatabase(boolean returnNull) throws SQLException{
69         if(!returnNull && database == null) throw Utils.createSQLException("You are not connected with a Database.");
70         return database;
71     }
72
73     public Statement createStatement() throws SQLException {
74         return new SSStatement(this);
75     }
76     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
77         return new SSPreparedStatement( this, sql);
78     }
79     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
80         return new SSCallableStatement( this, sql);
81     }
82     
83     
84     public String JavaDoc nativeSQL(String JavaDoc sql){
85         return sql;
86     }
87     
88     
89     public void setAutoCommit(boolean autoCommit) throws SQLException {
90         if(log.isLogging()) log.println("AutoCommit:"+autoCommit);
91         if(this.autoCommit != autoCommit){
92             commit();
93             this.autoCommit = autoCommit;
94         }
95     }
96     
97     
98     public boolean getAutoCommit(){
99         return autoCommit;
100     }
101     
102     
103     /**
104      * Add a page for later commit or rollback.
105      */

106     void add(StorePage storePage) throws SQLException{
107         testClosedConnection();
108         commitPages.add(storePage);
109     }
110     
111     
112     public void commit() throws SQLException {
113         try{
114             log.println("Commit");
115             testClosedConnection();
116             synchronized(commitPages){
117                 int count = commitPages.size();
118                 for(int i=0; i<count; i++){
119                     StorePage page = (StorePage)commitPages.get(i);
120                     page.commit();
121                 }
122                 for(int i=0; i<count; i++){
123                     StorePage page = (StorePage)commitPages.get(i);
124                     page.freeLock();
125                 }
126                 commitPages.clear();
127                 transactionTime = System.currentTimeMillis();
128             }
129         }catch(Throwable JavaDoc e){
130             rollback();
131             throw Utils.createSQLException(e);
132         }
133     }
134     
135     
136     /**
137      * Discard all changes of a file because it was deleted.
138      */

139     void rollbackFile(RandomAccessFile JavaDoc raFile) throws SQLException{
140         testClosedConnection();
141         // remove the all commits that point to this table
142
for(int i=commitPages.size()-1; i>=0; i--){
143             StorePage page = (StorePage)commitPages.get(i);
144             if(page.raFile == raFile){
145                 page.rollback();
146                 page.freeLock();
147             }
148         }
149     }
150     
151     
152     void rollback(int savepoint) throws SQLException{
153         testClosedConnection();
154         for(int i = commitPages.size()-1; i>=savepoint; i--){
155             StorePage page = (StorePage)commitPages.remove(i);
156             page.rollback();
157             page.freeLock();
158         }
159     }
160     
161     
162     public void rollback() throws SQLException {
163         log.println("Rollback");
164         testClosedConnection();
165         synchronized(commitPages){
166             int count = commitPages.size();
167             for(int i=0; i<count; i++){
168                 StorePage page = (StorePage)commitPages.get(i);
169                 page.rollback();
170                 page.freeLock();
171             }
172             commitPages.clear();
173             transactionTime = System.currentTimeMillis();
174         }
175     }
176     
177     
178     public void close() throws SQLException {
179         rollback();
180         database = null;
181         commitPages = null;
182         Database.closeConnection(this);
183     }
184     
185     
186     final void testClosedConnection() throws SQLException{
187         if(isClosed()) throw Utils.createSQLException("Connection was closed.");
188     }
189     
190     public boolean isClosed(){
191         return (commitPages == null);
192     }
193     
194     
195     public DatabaseMetaData getMetaData(){
196         return metadata;
197     }
198     
199     
200     public void setReadOnly(boolean readOnly){
201         //TODO Connection ReadOnly implementing
202
}
203     
204     
205     public boolean isReadOnly(){
206         return false;
207     }
208     
209     
210     public void setCatalog(String JavaDoc catalog) throws SQLException {
211         if(isClosed()) throw Utils.createSQLException("Connection is already closed");
212         database = Database.getDatabase(catalog, this, false);
213     }
214     
215     
216     public String JavaDoc getCatalog(){
217         if(database == null)
218             return "";
219         return database.getName();
220     }
221     
222     
223     public void setTransactionIsolation(int level) throws SQLException {
224         if(!metadata.supportsTransactionIsolationLevel(level))
225             throw Utils.createSQLException("Unknown Transaction Isolation Level:"+level);
226         isolationLevel = level;
227     }
228     
229     
230     public int getTransactionIsolation(){
231         return isolationLevel;
232     }
233     
234     
235     public SQLWarning getWarnings(){
236         return null;
237     }
238     
239     
240     public void clearWarnings(){
241         //TODO support for Warnings
242
}
243     
244     
245     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
246         return new SSStatement( this, resultSetType, resultSetConcurrency);
247     }
248     
249     
250     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException {
251         return new SSPreparedStatement( this, sql, resultSetType, resultSetConcurrency);
252     }
253     
254     
255     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException {
256         return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency);
257     }
258     
259     
260     public Map JavaDoc getTypeMap(){
261         return null;
262     }
263     
264     
265     public void setTypeMap(Map JavaDoc map){
266         //TODO support for TypeMap
267
}
268     
269     
270     public void setHoldability(int holdability){
271         this.holdability = holdability;
272     }
273     
274     
275     public int getHoldability(){
276         return holdability;
277     }
278     
279     
280     int getSavepoint() throws SQLException{
281         testClosedConnection();
282         return commitPages.size();
283     }
284     
285     
286     public Savepoint setSavepoint() throws SQLException {
287         return new SSSavepoint(getSavepoint(), null, transactionTime);
288     }
289     
290     
291     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
292         return new SSSavepoint(getSavepoint(), name, transactionTime);
293     }
294     
295     
296     public void rollback(Savepoint savepoint) throws SQLException {
297         if(savepoint instanceof SSSavepoint){
298             if(((SSSavepoint)savepoint).transactionTime != transactionTime){
299                 throw Utils.createSQLException("Savepoint is not valid for this transaction.");
300             }
301             rollback( savepoint.getSavepointId() );
302             return;
303         }
304         throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint);
305     }
306     
307     
308     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
309         if(savepoint instanceof SSSavepoint){
310             ((SSSavepoint)savepoint).transactionTime = 0;
311             return;
312         }
313         throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint);
314     }
315     
316     
317     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
318         //TODO resultSetHoldability
319
return new SSStatement( this, resultSetType, resultSetConcurrency);
320     }
321     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
322         //TODO resultSetHoldability
323
return new SSPreparedStatement( this, sql);
324     }
325     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
326         //TODO resultSetHoldability
327
return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency);
328     }
329     
330     
331     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
332         SSPreparedStatement pr = new SSPreparedStatement( this, sql);
333         pr.setNeedGeneratedKeys(autoGeneratedKeys);
334         return pr;
335     }
336     
337     
338     public PreparedStatement prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException {
339         SSPreparedStatement pr = new SSPreparedStatement( this, sql);
340         pr.setNeedGeneratedKeys(columnIndexes);
341         return pr;
342     }
343     
344     
345     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException {
346         SSPreparedStatement pr = new SSPreparedStatement( this, sql);
347         pr.setNeedGeneratedKeys(columnNames);
348         return pr;
349     }
350 }
Popular Tags