comparison src/main/java/nl/cwi/monetdb/jdbc/MonetSavepoint.java @ 283:92e882feea95

Small improvements to MonetSavepoint. Made class final. Also raise an Exception when savepoint name is empty. Disabled unused method getHighestId().
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 24 Jul 2019 17:46:24 +0200 (2019-07-24)
parents d4baf8a4b43a
children 2ad7f42f141d 003ae6d881db
comparison
equal deleted inserted replaced
282:368078840ddf 283:92e882feea95
24 * an id. Each instance of this class always has an id, which is used for 24 * an id. Each instance of this class always has an id, which is used for
25 * internal representation of the save point. 25 * internal representation of the save point.
26 * 26 *
27 * Because the IDs which get generated are a logical sequence, application 27 * Because the IDs which get generated are a logical sequence, application
28 * wide, two concurrent transactions are guaranteed to not to have the same 28 * wide, two concurrent transactions are guaranteed to not to have the same
29 * save point identifiers. In this implementation the validaty of save points 29 * save point identifiers. In this implementation the validity of save points
30 * is determined by the server, which makes this a light implementation. 30 * is determined by the server, which makes this a light implementation.
31 * 31 *
32 * @author Fabian Groffen 32 * @author Fabian Groffen
33 * @version 1.0 33 * @version 1.0
34 */ 34 */
35 public class MonetSavepoint implements Savepoint { 35 public final class MonetSavepoint implements Savepoint {
36 /** The id of the last created Savepoint */ 36 /** The id of the last created Savepoint */
37 private static AtomicInteger highestId = new AtomicInteger(0); 37 private static AtomicInteger highestId = new AtomicInteger(0);
38 38
39 /** The name of this Savepoint */ 39 /** The name of this Savepoint */
40 private final String name; 40 private final String name;
41 /** The id of this Savepoint */ 41 /** The id of this Savepoint */
42 private final int id; 42 private final int id;
43 43
44 /**
45 * Creates a named MonetSavepoint object
46 */
44 public MonetSavepoint(String name) throws IllegalArgumentException { 47 public MonetSavepoint(String name) throws IllegalArgumentException {
45 if (name == null) throw new IllegalArgumentException("null not allowed"); 48 if (name == null || name.isEmpty())
49 throw new IllegalArgumentException("missing savepoint name string");
46 50
47 this.id = getNextId(); 51 this.id = getNextId();
48 this.name = name; 52 this.name = name;
49 } 53 }
50 54
55 /**
56 * Creates an unnamed MonetSavepoint object
57 */
51 public MonetSavepoint() { 58 public MonetSavepoint() {
52 this.id = getNextId(); 59 this.id = getNextId();
53 this.name = null; 60 this.name = null;
54 } 61 }
55 62
61 * @return the numeric ID of this savepoint 68 * @return the numeric ID of this savepoint
62 * @throws SQLException if this is a named savepoint 69 * @throws SQLException if this is a named savepoint
63 */ 70 */
64 @Override 71 @Override
65 public int getSavepointId() throws SQLException { 72 public int getSavepointId() throws SQLException {
66 if (name != null) throw 73 if (name != null)
67 new SQLException("Cannot getID for named savepoint", "3B000"); 74 throw new SQLException("Cannot getID for named savepoint", "3B000");
68 75
69 return getId(); 76 return id;
70 } 77 }
71 78
72 /** 79 /**
73 * Retrieves the name of the savepoint that this Savepoint object 80 * Retrieves the name of the savepoint that this Savepoint object
74 * represents. 81 * represents.
76 * @return the name of this savepoint 83 * @return the name of this savepoint
77 * @throws SQLException if this is an un-named savepoint 84 * @throws SQLException if this is an un-named savepoint
78 */ 85 */
79 @Override 86 @Override
80 public String getSavepointName() throws SQLException { 87 public String getSavepointName() throws SQLException {
81 if (name == null) throw 88 if (name == null)
82 new SQLException("Unable to retrieve name of unnamed savepoint", "3B000"); 89 throw new SQLException("Unable to retrieve name of unnamed savepoint", "3B000");
83 90
84 return name; 91 return name;
85 } 92 }
86 93
87 //== end of methods from Savepoint interface 94 //== end of methods from Savepoint interface
96 int getId() { 103 int getId() {
97 return id; 104 return id;
98 } 105 }
99 106
100 /** 107 /**
101 * Returns the name to use when referencing this save point to the MonetDB 108 * Returns the constructed internal name to use when referencing this save point to the
102 * database. The returned value is guaranteed to be unique and consists of 109 * MonetDB database. The returned value is guaranteed to be unique and consists of
103 * a prefix string and a sequence number. 110 * a prefix string and a sequence number.
104 * 111 *
105 * @return the unique savepoint name 112 * @return the unique savepoint name
106 */ 113 */
107 String getName() { 114 String getName() {
128 * synchronized to prevent race conditions and thus guaranteed to be 135 * synchronized to prevent race conditions and thus guaranteed to be
129 * thread-safe. 136 * thread-safe.
130 * 137 *
131 * @return the highest id returned by a call to getNextId() 138 * @return the highest id returned by a call to getNextId()
132 */ 139 */
133 private static int getHighestId() { 140 // private static int getHighestId() {
134 return highestId.get(); 141 // return highestId.get();
135 } 142 // }
136 } 143 }