comparison src/main/java/nl/cwi/monetdb/jdbc/MonetSavepoint.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 7307caacc2d5 e092fa8d9ab7
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
7 */
8
9 package nl.cwi.monetdb.jdbc;
10
11 import java.sql.*;
12 import java.util.concurrent.atomic.AtomicInteger;
13
14 /**
15 * The representation of a savepoint, which is a point within the current
16 * transaction that can be referenced from the Connection.rollback method.
17 * When a transaction is rolled back to a savepoint all changes made after
18 * that savepoint are undone.
19 * Savepoints can be either named or unnamed. Unnamed savepoints are
20 * identified by an ID generated by the underlying data source.
21 *
22 * This little class is nothing more than a container for a name and/or
23 * an id. Each instance of this class always has an id, which is used for
24 * internal representation of the save point.
25 *
26 * Because the IDs which get generated are a logical sequence, application
27 * wide, two concurrent transactions are guaranteed to not to have the same
28 * save point identifiers. In this implementation the validaty of save points
29 * is determined by the server, which makes this a light implementation.
30 *
31 * @author Fabian Groffen
32 * @version 1.0
33 */
34 public class MonetSavepoint implements Savepoint {
35 /** The id of the last created Savepoint */
36 private static AtomicInteger highestId = new AtomicInteger(0);
37
38 /** The name of this Savepoint */
39 private final String name;
40 /** The id of this Savepoint */
41 private final int id;
42
43 public MonetSavepoint(String name) throws IllegalArgumentException {
44 if (name == null) throw new IllegalArgumentException("null not allowed");
45
46 this.id = getNextId();
47 this.name = name;
48 }
49
50 public MonetSavepoint() {
51 this.id = getNextId();
52 this.name = null;
53 }
54
55
56 /**
57 * Retrieves the generated ID for the savepoint that this Savepoint object
58 * represents.
59 *
60 * @return the numeric ID of this savepoint
61 * @throws SQLException if this is a named savepoint
62 */
63 @Override
64 public int getSavepointId() throws SQLException {
65 if (name != null) throw
66 new SQLException("Cannot getID for named savepoint", "3B000");
67
68 return getId();
69 }
70
71 /**
72 * Retrieves the name of the savepoint that this Savepoint object
73 * represents.
74 *
75 * @return the name of this savepoint
76 * @throws SQLException if this is an un-named savepoint
77 */
78 @Override
79 public String getSavepointName() throws SQLException {
80 if (name == null) throw
81 new SQLException("Unable to retrieve name of unnamed savepoint", "3B000");
82
83 return name;
84 }
85
86 //== end of methods from Savepoint interface
87
88 /**
89 * Retrieves the savepoint id, like the getSavepointId method with the only
90 * difference that this method will always return the id, regardless of
91 * whether it is named or not.
92 *
93 * @return the numeric ID of this savepoint
94 */
95 int getId() {
96 return id;
97 }
98
99 /**
100 * Returns the name to use when referencing this save point to the MonetDB
101 * database. The returned value is guaranteed to be unique and consists of
102 * a prefix string and a sequence number.
103 *
104 * @return the unique savepoint name
105 */
106 String getName() {
107 return "MonetDBSP" + id;
108 }
109
110
111 /**
112 * Returns the next id, which is larger than the last returned id. This
113 * method is synchronized to prevent race conditions. Since this is static
114 * code, this method is shared by requests from multiple Connection objects.
115 * Therefore two successive calls to this method need not to have a
116 * difference of 1.
117 *
118 * @return the next int which is guaranteed to be higher than the one
119 * at the last call to this method.
120 */
121 private static int getNextId() {
122 return highestId.incrementAndGet();
123 }
124
125 /**
126 * Returns the highest id returned by getNextId(). This method is also
127 * synchronized to prevent race conditions and thus guaranteed to be
128 * thread-safe.
129 *
130 * @return the highest id returned by a call to getNextId()
131 */
132 private static int getHighestId() {
133 return highestId.get();
134 }
135 }