Debian Codegraph @

src/main/org/h2/engine/IsolationLevel.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import java.sql.Connection;
9
10 import org.h2.message.DbException;
11
12 /**
13 * Level of isolation.
14 */
15 public enum IsolationLevel {
16
17 /**
18 * Dirty reads, non-repeatable reads and phantom reads are allowed.
19 */
20 READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED, Constants.LOCK_MODE_OFF),
21
22 /**
23 * Dirty reads aren't allowed; non-repeatable reads and phantom reads are
24 * allowed.
25 */
26 READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED, Constants.LOCK_MODE_READ_COMMITTED),
27
28 /**
29 * Dirty reads and non-repeatable reads aren't allowed; phantom reads are
30 * allowed.
31 */
32 REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ, Constants.LOCK_MODE_TABLE),
33
34 /**
35 * Dirty reads, non-repeatable reads and phantom reads are'n allowed.
36 */
37 SNAPSHOT(Constants.TRANSACTION_SNAPSHOT, Constants.LOCK_MODE_TABLE),
38
39 /**
40 * Dirty reads, non-repeatable reads and phantom reads are'n allowed.
41 * Concurrent and serial execution of transactions with this isolation level
42 * should have the same effect.
43 */
44 SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE, Constants.LOCK_MODE_TABLE);
45
46 /**
47 * Returns the isolation level from LOCK_MODE equivalent for PageStore and
48 * old versions of H2.
49 *
50 * @param level
51 * the LOCK_MODE value
52 * @return the isolation level
53 */
54 public static IsolationLevel fromJdbc(int level) {
55 switch (level) {
56 case Connection.TRANSACTION_READ_UNCOMMITTED:
57 return IsolationLevel.READ_UNCOMMITTED;
58 case Connection.TRANSACTION_READ_COMMITTED:
59 return IsolationLevel.READ_COMMITTED;
60 case Connection.TRANSACTION_REPEATABLE_READ:
61 return IsolationLevel.REPEATABLE_READ;
62 case Constants.TRANSACTION_SNAPSHOT:
63 return IsolationLevel.SNAPSHOT;
64 case Connection.TRANSACTION_SERIALIZABLE:
65 return IsolationLevel.SERIALIZABLE;
66 default:
67 throw DbException.getInvalidValueException("isolation level", level);
68 }
69 }
70
71 /**
72 * Returns the isolation level from LOCK_MODE equivalent for PageStore and
73 * old versions of H2.
74 *
75 * @param lockMode
76 * the LOCK_MODE value
77 * @return the isolation level
78 */
79 public static IsolationLevel fromLockMode(int lockMode) {
80 switch (lockMode) {
81 case Constants.LOCK_MODE_OFF:
82 return IsolationLevel.READ_UNCOMMITTED;
83 case Constants.LOCK_MODE_READ_COMMITTED:
84 default:
85 return IsolationLevel.READ_COMMITTED;
86 case Constants.LOCK_MODE_TABLE:
87 case Constants.LOCK_MODE_TABLE_GC:
88 return IsolationLevel.SERIALIZABLE;
89 }
90 }
91
92 /**
93 * Returns the isolation level from its SQL name.
94 *
95 * @param sql
96 * the SQL name
97 * @return the isolation level from its SQL name
98 */
99 public static IsolationLevel fromSql(String sql) {
100 switch (sql) {
101 case "READ UNCOMMITTED":
102 return READ_UNCOMMITTED;
103 case "READ COMMITTED":
104 return READ_COMMITTED;
105 case "REPEATABLE READ":
106 return REPEATABLE_READ;
107 case "SNAPSHOT":
108 return SNAPSHOT;
109 case "SERIALIZABLE":
110 return SERIALIZABLE;
111 default:
112 throw DbException.getInvalidValueException("isolation level", sql);
113 }
114 }
115
116 private final String sql;
117
118 private final int jdbc, lockMode;
119
120 private IsolationLevel(int jdbc, int lockMode) {
121 sql = name().replace('_', ' ').intern();
122 this.jdbc = jdbc;
123 this.lockMode = lockMode;
124 }
125
126 /**
127 * Returns the SQL representation of this isolation level.
128 *
129 * @return SQL representation of this isolation level
130 */
131 public String getSQL() {
132 return sql;
133 }
134
135 /**
136 * Returns the JDBC constant for this isolation level.
137 *
138 * @return the JDBC constant for this isolation level
139 */
140 public int getJdbc() {
141 return jdbc;
142 }
143
144 /**
145 * Returns the LOCK_MODE equivalent for PageStore and old versions of H2.
146 *
147 * @return the LOCK_MODE equivalent
148 */
149 public int getLockMode() {
150 return lockMode;
151 }
152
153 /**
154 * Returns whether a non-repeatable read phenomena is allowed.
155 *
156 * @return whether a non-repeatable read phenomena is allowed
157 */
158 public boolean allowNonRepeatableRead() {
159 return ordinal() < REPEATABLE_READ.ordinal();
160 }
161
162 }