1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.jdbc.EmbedSavepoint30 |
4 | |
5 | Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.jdbc; |
22 | |
23 | import org.apache.derby.impl.jdbc.EmbedConnection; |
24 | import org.apache.derby.impl.jdbc.ConnectionChild; |
25 | import org.apache.derby.impl.jdbc.Util; |
26 | |
27 | import org.apache.derby.iapi.reference.SQLState; |
28 | |
29 | import org.apache.derby.iapi.error.StandardException; |
30 | |
31 | import java.sql.Savepoint; |
32 | import java.sql.SQLException; |
33 | |
34 | /** |
35 | * This class implements the Savepoint interface from JDBC3.0 |
36 | * This allows to set, release, or rollback a transaction to |
37 | * designated Savepoints. Savepoints provide finer-grained |
38 | * control of transactions by marking intermediate points within |
39 | * a transaction. Once a savepoint has been set, the transaction |
40 | * can be rolled back to that savepoint without affecting preceding work. |
41 | <P><B>Supports</B> |
42 | <UL> |
43 | <LI> JSR169 - no subsetting for java.sql.Savepoint |
44 | <LI> JDBC 3.0 - class introduced in JDBC 3.0 |
45 | </UL> |
46 | * |
47 | * @see java.sql.Savepoint |
48 | * |
49 | */ |
50 | final class EmbedSavepoint30 extends ConnectionChild |
51 | implements Savepoint { |
52 | |
53 | //In order to avoid name conflict, the external names are prepanded |
54 | //with "e." and internal names always start with "i." This is for bug 4467 |
55 | private final String savepointName; |
56 | private final int savepointID; |
57 | |
58 | ////////////////////////////////////////////////////////////// |
59 | // |
60 | // CONSTRUCTORS |
61 | // |
62 | ////////////////////////////////////////////////////////////// |
63 | /* |
64 | Constructor assumes caller will setup context stack |
65 | and restore it. |
66 | @exception SQLException on error |
67 | */ |
68 | EmbedSavepoint30(EmbedConnection conn, String name) |
69 | throws StandardException { |
70 | super(conn); |
71 | if (name == null) //this is an unnamed savepoint |
72 | { |
73 | //Generating a unique internal name for unnamed savepoints |
74 | savepointName = "i." + conn.getLanguageConnection().getUniqueSavepointName(); |
75 | savepointID = conn.getLanguageConnection().getUniqueSavepointID(); |
76 | } else |
77 | { |
78 | savepointName = "e." + name; |
79 | savepointID = -1; |
80 | } |
81 | conn.getLanguageConnection().languageSetSavePoint(savepointName, this); |
82 | } |
83 | |
84 | /** |
85 | * |
86 | * Retrieves the generated ID for the savepoint that this Savepoint object |
87 | * represents. |
88 | * |
89 | * @return the numeric ID of this savepoint |
90 | * @exception SQLException if this is a named savepoint |
91 | */ |
92 | public int getSavepointId() throws SQLException { |
93 | if (savepointID == -1) |
94 | throw newSQLException(SQLState.NO_ID_FOR_NAMED_SAVEPOINT); |
95 | return savepointID; |
96 | } |
97 | |
98 | /** |
99 | * |
100 | * Retrieves the name of the savepoint that this Savepoint object |
101 | * represents. |
102 | * |
103 | * @return the name of this savepoint |
104 | * @exception SQLException if this is an un-named savepoint |
105 | */ |
106 | public String getSavepointName() throws SQLException { |
107 | if (savepointID != -1) |
108 | throw newSQLException(SQLState.NO_NAME_FOR_UNNAMED_SAVEPOINT); |
109 | return savepointName.substring(2); |
110 | } |
111 | |
112 | //Cloudscape internally keeps name for both named and unnamed savepoints |
113 | String getInternalName() { |
114 | return savepointName; |
115 | } |
116 | |
117 | |
118 | //bug 4468 - verify that savepoint rollback/release is for a savepoint from |
119 | //the current connection |
120 | boolean sameConnection(EmbedConnection con) { |
121 | return (getEmbedConnection().getLanguageConnection() == con.getLanguageConnection()); |
122 | } |
123 | } |
124 | |