KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedConnection30


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedConnection30
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.sql.conn.StatementContext;
26
27 import org.apache.derby.impl.jdbc.EmbedConnection;
28 import org.apache.derby.impl.jdbc.Util;
29 import org.apache.derby.jdbc.InternalDriver;
30
31 import org.apache.derby.iapi.reference.SQLState;
32 import org.apache.derby.iapi.reference.Limits;
33
34 import org.apache.derby.iapi.error.ExceptionSeverity;
35
36 import java.sql.Savepoint JavaDoc;
37 import java.sql.SQLException JavaDoc;
38
39 import java.util.Properties JavaDoc;
40 import java.util.Vector JavaDoc;
41
42
43 /**
44  * This class extends the EmbedConnection20 class in order to support new
45  * methods and classes that come with JDBC 3.0.
46
47    <P><B>Supports</B>
48    <UL>
49    <LI> JSR169 - Subsetting only removes getTypeMap and setTypeMap, which references
50         java.util.Map which exists in Foundation and ee.miniumum. Thus the methods can
51         safely be left in the implementation for JSR169.
52
53   <LI> JDBC 3.0 - Separate from JDBC 2.0 implementation as JDBC 3.0 introduces
54         a new class java.sql.Savepoint, which is referenced by java.sql.Connection.
55    </UL>
56  *
57  * @see org.apache.derby.impl.jdbc.EmbedConnection
58  *
59  */

60 public class EmbedConnection30 extends EmbedConnection
61 {
62
63     //////////////////////////////////////////////////////////
64
// CONSTRUCTORS
65
//////////////////////////////////////////////////////////
66

67     public EmbedConnection30(
68             InternalDriver driver,
69             String JavaDoc url,
70             Properties JavaDoc info)
71         throws SQLException JavaDoc
72     {
73         super(driver, url, info);
74     }
75
76     public EmbedConnection30(
77             EmbedConnection inputConnection)
78     {
79         super(inputConnection);
80     }
81
82     /////////////////////////////////////////////////////////////////////////
83
//
84
// JDBC 3.0 - New public methods
85
//
86
/////////////////////////////////////////////////////////////////////////
87

88     /**
89      * Creates an unnamed savepoint in the current transaction and
90      * returns the new Savepoint object that represents it.
91      *
92      *
93      * @return The new Savepoint object
94      *
95      * @exception SQLException if a database access error occurs or
96      * this Connection object is currently in auto-commit mode
97      */

98     public Savepoint JavaDoc setSavepoint()
99         throws SQLException JavaDoc
100     {
101         return commonSetSavepointCode(null, false);
102     }
103
104     /**
105      * Creates a savepoint with the given name in the current transaction and
106      * returns the new Savepoint object that represents it.
107      *
108      *
109      * @param name A String containing the name of the savepoint
110      *
111      * @return The new Savepoint object
112      *
113      * @exception SQLException if a database access error occurs or
114      * this Connection object is currently in auto-commit mode
115      */

116     public Savepoint JavaDoc setSavepoint(
117             String JavaDoc name)
118         throws SQLException JavaDoc
119     {
120         return commonSetSavepointCode(name, true);
121     }
122
123     /**
124      * Creates a savepoint with the given name(if it is a named savepoint else we will generate a name
125      * becuase Cloudscape only supports named savepoints internally) in the current transaction and
126      * returns the new Savepoint object that represents it.
127      *
128      * @param name A String containing the name of the savepoint. Will be null if this is an unnamed savepoint
129      * @param userSuppliedSavepointName If true means it's a named user defined savepoint.
130      *
131      * @return The new Savepoint object
132      */

133     private Savepoint JavaDoc commonSetSavepointCode(String JavaDoc name, boolean userSuppliedSavepointName) throws SQLException JavaDoc
134     {
135         synchronized (getConnectionSynchronization()) {
136             setupContextStack();
137             try {
138                 verifySavepointCommandIsAllowed();
139                 if (userSuppliedSavepointName && (name == null))//make sure that if it is a named savepoint then supplied name is not null
140
throw newSQLException(SQLState.NULL_NAME_FOR_SAVEPOINT);
141                 //make sure that if it is a named savepoint then supplied name length is not > 128
142
if (userSuppliedSavepointName && (name.length() > Limits.MAX_IDENTIFIER_LENGTH))
143                     throw newSQLException(SQLState.LANG_IDENTIFIER_TOO_LONG, name, String.valueOf(Limits.MAX_IDENTIFIER_LENGTH));
144                 if (userSuppliedSavepointName && name.startsWith("SYS")) //to enforce DB2 restriction which is savepoint name can't start with SYS
145
throw newSQLException(SQLState.INVALID_SCHEMA_SYS, "SYS");
146                 Savepoint JavaDoc savePt = new EmbedSavepoint30(this, name);
147                 return savePt;
148             } catch (StandardException e) {
149                 throw handleException(e);
150             } finally {
151                 restoreContextStack();
152             }
153         }
154     }
155
156     /**
157      * Undoes all changes made after the given Savepoint object was set.
158      * This method should be used only when auto-commit has been disabled.
159      *
160      *
161      * @param savepoint The Savepoint object to rollback to
162      *
163      * @exception SQLException if a database access error occurs,
164      * the Savepoint object is no longer valid, or this Connection
165      * object is currently in auto-commit mode
166      */

167     public void rollback(
168             Savepoint JavaDoc savepoint)
169         throws SQLException JavaDoc
170     {
171         synchronized (getConnectionSynchronization()) {
172             setupContextStack();
173             try {
174                 verifySavepointCommandIsAllowed();
175                 verifySavepointArg(savepoint);
176                 //Need to cast and get the name because JDBC3 spec doesn't support names for
177
//unnamed savepoints but Cloudscape keeps names for named & unnamed savepoints.
178
getLanguageConnection().internalRollbackToSavepoint(((EmbedSavepoint30)savepoint).getInternalName(),true, savepoint);
179             } catch (StandardException e) {
180                 throw handleException(e);
181             } finally {
182                 restoreContextStack();
183             }
184         }
185     }
186
187     /**
188      * Removes the given Savepoint object from the current transaction.
189      * Any reference to the savepoint after it has been removed will cause
190      * an SQLException to be thrown
191      *
192      *
193      * @param savepoint The Savepoint object to be removed
194      *
195      * @exception SQLException if a database access error occurs or the
196      * given Savepoint object is not a valid savepoint in the current transaction
197      */

198     public void releaseSavepoint(
199             Savepoint JavaDoc savepoint)
200         throws SQLException JavaDoc
201     {
202         synchronized (getConnectionSynchronization()) {
203             setupContextStack();
204             try {
205                 verifySavepointCommandIsAllowed();
206                 verifySavepointArg(savepoint);
207                 //Need to cast and get the name because JDBC3 spec doesn't support names for
208
//unnamed savepoints but Cloudscape keeps name for named & unnamed savepoints.
209
getLanguageConnection().releaseSavePoint(((EmbedSavepoint30)savepoint).getInternalName(), savepoint);
210             } catch (StandardException e) {
211                 throw handleException(e);
212             } finally {
213                 restoreContextStack();
214             }
215         }
216     }
217
218     // used by setSavepoint to check autocommit is false and not inside the trigger code
219
private void verifySavepointCommandIsAllowed() throws SQLException JavaDoc
220     {
221         if (autoCommit)
222             throw newSQLException(SQLState.NO_SAVEPOINT_WHEN_AUTO);
223
224         //Bug 4507 - savepoint not allowed inside trigger
225
StatementContext stmtCtxt = getLanguageConnection().getStatementContext();
226         if (stmtCtxt!= null && stmtCtxt.inTrigger())
227             throw newSQLException(SQLState.NO_SAVEPOINT_IN_TRIGGER);
228     }
229
230     // used by release/rollback to check savepoint argument
231
private void verifySavepointArg(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
232     {
233         //bug 4451 - Check for null savepoint
234
EmbedSavepoint30 lsv = (EmbedSavepoint30) savepoint;
235         // bug 4451 need to throw error for null Savepoint
236
if (lsv == null)
237         throw
238             Util.generateCsSQLException(SQLState.XACT_SAVEPOINT_NOT_FOUND, "null");
239
240         //bug 4468 - verify that savepoint rollback is for a savepoint from the current
241
// connection
242
if (!lsv.sameConnection(this))
243             throw newSQLException(SQLState.XACT_SAVEPOINT_RELEASE_ROLLBACK_FAIL);
244       
245         return;
246     }
247 }
248
Popular Tags