comparison src/main/java/org/monetdb/jdbc/MonetSavepoint.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/jdbc/MonetSavepoint.java@54137aeb1f92
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
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 - 2020 MonetDB B.V.
7 */
8
9 package org.monetdb.jdbc;
10
11 import java.sql.SQLException;
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 validity of save points
29 * is determined by the server, which makes this a light implementation.
30 *
31 * @author Fabian Groffen
32 * @version 1.1
33 */
34 public final class MonetSavepoint implements java.sql.Savepoint {
35 /** The id of the last created Savepoint */
36 private static final 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 /**
44 * Creates a named MonetSavepoint object
45 *
46 * @param name of savepoint
47 * @throws IllegalArgumentException if no or empty name is given
48 */
49 public MonetSavepoint(final String name) throws IllegalArgumentException {
50 if (name == null || name.isEmpty())
51 throw new IllegalArgumentException("Missing savepoint name");
52
53 this.id = getNextId();
54 this.name = name;
55 }
56
57 /**
58 * Creates an unnamed MonetSavepoint object
59 */
60 public MonetSavepoint() {
61 this.id = getNextId();
62 this.name = null;
63 }
64
65
66 /**
67 * Retrieves the generated ID for the savepoint that this Savepoint object
68 * represents.
69 *
70 * @return the numeric ID of this savepoint
71 * @throws SQLException if this is a named savepoint
72 */
73 @Override
74 public int getSavepointId() throws SQLException {
75 if (name != null)
76 throw new SQLException("Cannot get ID of named savepoint", "3B000");
77
78 return id;
79 }
80
81 /**
82 * Retrieves the name of the savepoint that this Savepoint object
83 * represents.
84 *
85 * @return the name of this savepoint
86 * @throws SQLException if this is an un-named savepoint
87 */
88 @Override
89 public String getSavepointName() throws SQLException {
90 if (name == null)
91 throw new SQLException("Cannot get name of un-named savepoint", "3B000");
92
93 return name;
94 }
95
96 //== end of methods from Savepoint interface
97
98 /**
99 * Retrieves the savepoint id, like the getSavepointId method with the only
100 * difference that this method will always return the id, regardless of
101 * whether it is named or not.
102 *
103 * @return the numeric ID of this savepoint
104 */
105 final int getId() {
106 return id;
107 }
108
109 /**
110 * Returns the constructed internal name to use when referencing this save point to the
111 * MonetDB database. The returned value is guaranteed to be unique and consists of
112 * a prefix string and a sequence number.
113 *
114 * @return the unique savepoint name
115 */
116 final String getName() {
117 return "JDBCSP" + id;
118 }
119
120
121 /**
122 * Returns the next id, which is larger than the last returned id. This
123 * method is synchronized to prevent race conditions. Since this is static
124 * code, this method is shared by requests from multiple Connection objects.
125 * Therefore two successive calls to this method need not to have a
126 * difference of 1.
127 *
128 * @return the next int which is guaranteed to be higher than the one
129 * at the last call to this method.
130 */
131 private static final int getNextId() {
132 return highestId.incrementAndGet();
133 }
134
135 /**
136 * Returns the highest id returned by getNextId(). This method is also
137 * synchronized to prevent race conditions and thus guaranteed to be
138 * thread-safe.
139 *
140 * @return the highest id returned by a call to getNextId()
141 */
142 // private static int getHighestId() {
143 // return highestId.get();
144 // }
145 }