001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.EmbedSavepoint30
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.jdbc;
023:
024: import org.apache.derby.impl.jdbc.EmbedConnection;
025: import org.apache.derby.impl.jdbc.ConnectionChild;
026: import org.apache.derby.impl.jdbc.Util;
027:
028: import org.apache.derby.iapi.reference.SQLState;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import java.sql.Savepoint;
033: import java.sql.SQLException;
034:
035: /**
036: * This class implements the Savepoint interface from JDBC3.0
037: * This allows to set, release, or rollback a transaction to
038: * designated Savepoints. Savepoints provide finer-grained
039: * control of transactions by marking intermediate points within
040: * a transaction. Once a savepoint has been set, the transaction
041: * can be rolled back to that savepoint without affecting preceding work.
042: <P><B>Supports</B>
043: <UL>
044: <LI> JSR169 - no subsetting for java.sql.Savepoint
045: <LI> JDBC 3.0 - class introduced in JDBC 3.0
046: </UL>
047: *
048: * @see java.sql.Savepoint
049: *
050: */
051: final class EmbedSavepoint30 extends ConnectionChild implements
052: Savepoint {
053:
054: //In order to avoid name conflict, the external names are prepanded
055: //with "e." and internal names always start with "i." This is for bug 4467
056: private final String savepointName;
057: private final int savepointID;
058:
059: //////////////////////////////////////////////////////////////
060: //
061: // CONSTRUCTORS
062: //
063: //////////////////////////////////////////////////////////////
064: /*
065: Constructor assumes caller will setup context stack
066: and restore it.
067: @exception SQLException on error
068: */
069: EmbedSavepoint30(EmbedConnection conn, String name)
070: throws StandardException {
071: super (conn);
072: if (name == null) //this is an unnamed savepoint
073: {
074: //Generating a unique internal name for unnamed savepoints
075: savepointName = "i."
076: + conn.getLanguageConnection()
077: .getUniqueSavepointName();
078: savepointID = conn.getLanguageConnection()
079: .getUniqueSavepointID();
080: } else {
081: savepointName = "e." + name;
082: savepointID = -1;
083: }
084: conn.getLanguageConnection().languageSetSavePoint(
085: savepointName, this );
086: }
087:
088: /**
089: *
090: * Retrieves the generated ID for the savepoint that this Savepoint object
091: * represents.
092: *
093: * @return the numeric ID of this savepoint
094: * @exception SQLException if this is a named savepoint
095: */
096: public int getSavepointId() throws SQLException {
097: if (savepointID == -1)
098: throw newSQLException(SQLState.NO_ID_FOR_NAMED_SAVEPOINT);
099: return savepointID;
100: }
101:
102: /**
103: *
104: * Retrieves the name of the savepoint that this Savepoint object
105: * represents.
106: *
107: * @return the name of this savepoint
108: * @exception SQLException if this is an un-named savepoint
109: */
110: public String getSavepointName() throws SQLException {
111: if (savepointID != -1)
112: throw newSQLException(SQLState.NO_NAME_FOR_UNNAMED_SAVEPOINT);
113: return savepointName.substring(2);
114: }
115:
116: //Cloudscape internally keeps name for both named and unnamed savepoints
117: String getInternalName() {
118: return savepointName;
119: }
120:
121: //bug 4468 - verify that savepoint rollback/release is for a savepoint from
122: //the current connection
123: boolean sameConnection(EmbedConnection con) {
124: return (getEmbedConnection().getLanguageConnection() == con
125: .getLanguageConnection());
126: }
127: }
|