File: SavepointSapDB.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (247 lines) | stat: -rw-r--r-- 9,135 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
 */


package  com.sap.dbtech.jdbc;

import  java.sql.*;


import  com.sap.dbtech.util.MessageKey;
import  com.sap.dbtech.util.MessageTranslator;
import  com.sap.dbtech.jdbc.exceptions.SQLExceptionSapDB;

/**
 * The representation of a savepoint, which is a point within
 * the current transaction that can be referenced from the
 * <code>Connection.rollback</code> method. When a transaction
 * is rolled back to a savepoint all changes made after that
 * savepoint are undone.
 * <p>
 * Savepoints can be either named or unnamed. Unnamed savepoints
 * are identified by an ID generated by the underlying data source.
 * @since 1.4 JDBC 3.0
 *
 */
public class SavepointSapDB
        implements Savepoint {
    private int SavepointId = 0;
    private String SavepointName = null;
    private boolean released = false;
    private static int savepointIndex = 0;
    private static final String savepointPrefix_C = "JDBC_SP_";
    private com.sap.dbtech.jdbc.InternalStatementSapDB internalStmt = null;
   
    /**
     * Creates a new un-named savepoint.
     * @since 1.4 JDBC 3.0
     *
     */
    public SavepointSapDB (ConnectionSapDB conn) throws SQLException{
        this.SavepointId = SavepointSapDB.nextSavepointID();
        this.internalStmt = new  com.sap.dbtech.jdbc.InternalStatementSapDB (conn, com.sap.dbtech.vsp001.SqlMode.Oracle_C);
    }

    /**
     * Creates a new named savepoint.
     * @param aSavepointName name of savepoint
     * @since 1.4 JDBC 3.0
     *
     */
    public SavepointSapDB (String aSavepointName, ConnectionSapDB conn) throws SQLException{
        this.SavepointName = aSavepointName;
        this.internalStmt = new  com.sap.dbtech.jdbc.InternalStatementSapDB (conn, com.sap.dbtech.vsp001.SqlMode.Oracle_C);
    }

    /**
     * Retrieves the generated ID for the savepoint that this
     * <code>Savepoint</code> object represents.
     * @return the numeric ID of this savepoint
     * @throws java.sql.SQLException if this is a named savepoint
     * @since 1.4 JDBC 3.0
     *
     */
    public int getSavepointId () throws java.sql.SQLException {
        if (this.SavepointId == 0) {
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_NAMED_SAVEPOINT));
        } else {
            return  this.SavepointId;
        }
    }

    /**
     * Retrieves the name of the savepoint that this <code>Savepoint</code>
     * object represents.
     * @return the name of this savepoint
     * @throws java.sql.SQLException if this is an un-named savepoint
     * @since 1.4 JDBC 3.0
     *
     */
    public String getSavepointName () throws java.sql.SQLException {
        if (this.SavepointName == null) {
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_UNNAMED_SAVEPOINT));
        } else {
            return  this.SavepointName;
        }
    }

    /**
     * Generates a new unique SavepointId
     * @return a new unique SavepointId
     */
    final static synchronized int nextSavepointID () {
        savepointIndex++;
        return  savepointIndex;
    }

    /**
     * Generates a savepointname from the current savepointid for internal use, because
     * the database kernel cannot handle un-named savepoints.
     * @return the generated savepointname
     */
    final String getInternalSavepointName () {
        if (this.SavepointName == null)
            return  savepointPrefix_C + String.valueOf(this.SavepointId);
        else
            return  this.SavepointName;
    }

    /**
     * Makes a "rollback to savepoint" command and sends it to the database kernel
     * @param stmt a valid <code>java.sql.statement</code>
     * @throws java.sql.SQLException if a database access error occurs
     */
    private void rollbackSavepoint () throws java.sql.SQLException {
        this.internalStmt.execute("ROLLBACK TO SAVEPOINT " + this.getInternalSavepointName());
    }

    /**
     * Sets a savepoint in state released
     */
    private void releaseSavepoint () throws SQLException {
        this.released = true;
        if(this.internalStmt.getConnectionSapDB().isReleaseSavePointSupported()) {
        	try {
        		this.internalStmt.execute("RELEASE SAVEPOINT " + this.getInternalSavepointName());
        	} catch(SQLException sqlEx) {
        		if(sqlEx.getErrorCode() == -3005) {
        			this.internalStmt.getConnectionSapDB().setReleaseSavePointSupported(false);
        		} else {
        			throw sqlEx;
        		}
        	}
        }
        		   
    }

    /**
     * Checks if a savepoint is in state released
     * @return true - if savepoint is released and false - if checkpoint isn`t released
     */
    private boolean isrelease () {
        return  this.released;
    }
    /**
   * Creates an unnamed savepoint in the current transaction and
   * returns the new <code>Savepoint</code> object that represents it.
   * @param conn a ConnectionSapDB object
   * @return the new <code>Savepoint</code> object
   * @see Savepoint
   * @since 1.4
   *
   */
    public static java.sql.Savepoint setSavepoint (ConnectionSapDB conn) throws java.sql.SQLException {
        SavepointSapDB sp = new SavepointSapDB(conn);
        sp.internalStmt.execute("SAVEPOINT " + sp.getInternalSavepointName());
        return  (java.sql.Savepoint)sp;
    }

    /**
   * Creates an unnamed savepoint in the current transaction and
   * returns the new <code>Savepoint</code> object that represents it.
   * @param SavepointName a savepointname
   * @param conn a ConnectionSapDB object
   * @return the new <code>Savepoint</code> object
   * @see Savepoint
   * @since 1.4
   *
   */
    public static java.sql.Savepoint setSavepoint (String SavepointName, ConnectionSapDB conn) throws java.sql.SQLException {
        SavepointSapDB sp = new SavepointSapDB(SavepointName, conn);
        sp.internalStmt.execute("SAVEPOINT " + sp.getInternalSavepointName());
        return  (java.sql.Savepoint)sp;
    }

    /**
     * Undoes all changes made after the given <code>Savepoint</code> object
     * was set.
     * <P>
     * This method should be used only when auto-commit has been disabled.
     *
     * @param savepoint the <code>Savepoint</code> object to roll back to
     * @exception SQLException if a database access error occurs,
     *            the <code>Savepoint</code> object is no longer valid,
     *            or this <code>Connection</code> object is currently in
     *            auto-commit mode
     * @see Savepoint
     * @see #rollback
     * @since 1.4
     */
    public static void rollback (java.sql.Savepoint savepoint) throws java.sql.SQLException {
        SavepointSapDB sp;
        try {
            sp = (SavepointSapDB)savepoint;
        } catch (ClassCastException classCastEx) {
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_NO_SAVEPOINTSAPDB,
                                                                    savepoint.getClass()));
        }
        if (sp.isrelease()) {
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_SAVEPOINT_RELEASED));
        }
        sp.rollbackSavepoint();
    }

    /**
     * Removes the given <code>Savepoint</code> object from the current
     * transaction. Any reference to the savepoint after it have been removed
     * will cause an <code>SQLException</code> to be thrown.
     *
     * @param savepoint the <code>Savepoint</code> object to be removed
     * @exception SQLException if a database access error occurs or
     *            the given <code>Savepoint</code> object is not a valid
     *            savepoint in the current transaction
     * @since 1.4
     */
    public static void releaseSavepoint (java.sql.Savepoint savepoint) throws java.sql.SQLException {
        SavepointSapDB sp;
        try {
            sp = (SavepointSapDB)savepoint;
        } catch (java.lang.ClassCastException _exc) {
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_NO_SAVEPOINTSAPDB,
                                                                    savepoint.getClass()));
        }
        if (sp.isrelease())
            throw new SQLExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_SAVEPOINT_RELEASED));
        sp.releaseSavepoint();
    }
}