1 /***
2 * IsolationLevel.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * contact information: dbregeon@sourceforge.net
26 */
27
28 package org.jcreme.sql;
29
30 import java.security.InvalidParameterException;
31 import java.sql.Connection;
32 import java.util.Hashtable;
33
34 import org.jcreme.enumerations.Enumeration;
35
36 /***
37 * This class represents the IsolationLevel of a database transaction.
38 *
39 * The value associated to the objects can be used in the java.sql.Connection
40 * interface to set the transactions isolation level.
41 *
42 * @author $Author: dbregeon $
43 * @version $Revision: 1.1 $
44 */
45 public class IsolationLevel extends Enumeration {
46 /***
47 * The IsolationLevel instances by their name.
48 */
49 private static final Hashtable fromName = new Hashtable();
50
51 /***
52 * The IsolationLevel instances by their value.
53 */
54 private static final Hashtable fromValue = new Hashtable();
55
56 /***
57 * The value of the IsolationLevel as defined in the java.sql.Connection
58 * interface.
59 */
60 protected Integer value;
61
62 /***
63 * The name for the 'NONE' isolation level.
64 */
65 public static final String S_NONE = "NONE";
66
67 /***
68 * The value for the 'NONE' isolation level.
69 */
70 public static final Integer I_NONE = new Integer(
71 Connection.TRANSACTION_NONE);
72
73 /***
74 * The 'NONE' isolation level.
75 */
76 public static final IsolationLevel NONE = new IsolationLevel(S_NONE, I_NONE);
77
78 /***
79 * The name for the 'READ_COMMITTED' isolation level.
80 */
81 public static final String S_READ_COMMITTED = "READ_COMMITTED";
82
83 /***
84 * The value for the 'READ_COMMITTED' isolation level.
85 */
86 public static final Integer I_READ_COMMITTED = new Integer(
87 Connection.TRANSACTION_READ_COMMITTED);
88
89 /***
90 * The 'READ_COMMITTED' isolation level.
91 */
92 public static final IsolationLevel READ_COMMITTED = new IsolationLevel(
93 S_READ_COMMITTED, I_READ_COMMITTED);
94
95 /***
96 * The name for the 'READ_UNCOMMITTED' isolation level.
97 */
98 public static final String S_READ_UNCOMMITTED = "READ_UNCOMMITTED";
99
100 /***
101 * The value for the 'READ_UNCOMMITTED' isolation level.
102 */
103 public static final Integer I_READ_UNCOMMITTED = new Integer(
104 Connection.TRANSACTION_READ_UNCOMMITTED);
105
106 /***
107 * The 'READ_UNCOMMITTED' isolation level.
108 */
109 public static final IsolationLevel READ_UNCOMMITTED = new IsolationLevel(
110 S_READ_UNCOMMITTED, I_READ_UNCOMMITTED);
111
112 /***
113 * The name for the 'REPEATABLE_READ' isolation level.
114 */
115 public static final String S_REPEATABLE_READ = "REPEATABLE_READ";
116
117 /***
118 * The value for the 'REPEATABLE_READ' isolation level.
119 */
120 public static final Integer I_REPEATABLE_READ = new Integer(
121 Connection.TRANSACTION_REPEATABLE_READ);
122
123 /***
124 * The 'REPEATABLE_READ' isolation level.
125 */
126 public static final IsolationLevel REPEATABLE_READ = new IsolationLevel(
127 S_REPEATABLE_READ, I_REPEATABLE_READ);
128
129 /***
130 * The name for the 'SERIALIZABLE' isolation level.
131 */
132 public static final String S_SERIALIZABLE = "SERIALIZABLE";
133
134 /***
135 * The value for the 'SERIALIZABLE' isolation level.
136 */
137 public static final Integer I_SERIALIZABLE = new Integer(
138 Connection.TRANSACTION_SERIALIZABLE);
139
140 /***
141 * The 'SERIALIZABLE' isolation level.
142 */
143 public static final IsolationLevel SERIALIZABLE = new IsolationLevel(
144 S_SERIALIZABLE, I_SERIALIZABLE);
145
146 /***
147 * Creates a new instance of IsolationLevel
148 *
149 * @param name
150 * the name of this enumerated value. It cannot be null.
151 * @param value
152 * the id for this enmerated value. It cannot be null.
153 * @throws InvalidParameterException
154 * if one of the parameters is null.
155 */
156 protected IsolationLevel(String name, Integer value)
157 throws InvalidParameterException {
158 super(name);
159 String message = null;
160 if (value == null) {
161 message = "Null is not an authorized id";
162 } else if (getFromValue().get(value) != null) {
163 message = value + " value is already in use";
164 }
165 if (message != null) {
166 getFromName().remove(name);
167 throw new InvalidParameterException(message);
168 }
169 this.value = value;
170 getFromValue().put(this.value, this);
171 }
172
173 /***
174 * This method is for internal use only.
175 *
176 * @return the Hashtable that links the enumerated values names with the
177 * actual enumerated value.
178 *
179 */
180 protected Hashtable getFromName() {
181 return fromName;
182 }
183
184 /***
185 * This method is for internal use only.
186 *
187 * @return the Hashtable that links the enumerated values id with the actual
188 * enumerated value.
189 *
190 */
191 protected Hashtable getFromValue() {
192 return fromValue;
193 }
194
195 /***
196 * Retrieves the IsolationLevel that corresponds to the given name.
197 *
198 * @param name
199 * the name of the IsolationLevel to retrieve.
200 * @return the IsolationLevel that corresponds to the given name. Null if it
201 * does not exist.
202 */
203 public static IsolationLevel get(String name) {
204 IsolationLevel level = null;
205 if (name != null) {
206 level = (IsolationLevel) fromName.get(name);
207 }
208 return level;
209 }
210
211 /***
212 * Retrieves the IsolationLevel that corresponds to the given id.
213 *
214 * @param value
215 * the id of the IsolationLevel to retrieve.
216 * @return the IsolationLevel that corresponds to the given id. Null if it
217 * does not exist.
218 */
219 public static IsolationLevel get(Integer value) {
220 IsolationLevel level = null;
221 if (value != null) {
222 level = (IsolationLevel) fromValue.get(value);
223 }
224 return level;
225 }
226
227 /***
228 * Gives access to the value of the IsolationLevel.
229 *
230 * @return the value associated to the IsolationLevel (the int given in the
231 * Connection interface).
232 */
233 public Integer getValue() {
234 return this.value;
235 }
236 }