summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/IsolationLevel.java
blob: 2c028a6c9dafa1662b5626be9c2a7be5e8418009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright 2006-2010 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado;

/**
 * Describes a transaction isolation level. Transaction levels, in order from
 * lowest to highest are:
 *
 * <ul>
 * <li>{@link #READ_UNCOMMITTED}
 * <li>{@link #READ_COMMITTED}
 * <li>{@link #REPEATABLE_READ}
 * <li>{@link #SNAPSHOT}
 * <li>{@link #SERIALIZABLE}
 * </ul>
 *
 * A transaction's isolation level is usually {@code READ_COMMITTED} or
 * {@code REPEATABLE_READ} by default. Forcing a lower level, like
 * {@code READ_COMMITTED}, is useful when performing a long cursor
 * iteration. It releases locks during iteration rather than holding on to them
 * until the transaction exits.
 *
 * <p>{@code SNAPSHOT} isolation is special in that it uses multiversion
 * concurrency control (MVCC). A commit may fail with an {@link
 * OptimisticLockException}. Few repositories are expected to support this
 * level, however.
 *
 * @author Brian S O'Neill
 * @see Repository#enterTransaction(IsolationLevel)
 * @see Transaction
 */
public enum IsolationLevel {

    /**
     * Indicates that no actual transaction is in progress. If this level is
     * specified when entering a transaction, it uses auto-commit mode.
     */
    NONE,

    /**
     * Indicates that dirty reads, non-repeatable reads and phantom reads can
     * occur. This level allows modifications by one transaction to be read by
     * another transaction before any changes have been committed (a "dirty
     * read"). If any of the changes are rolled back, the second transaction
     * will have retrieved an invalid modification.
     *
     * <p>This level is also known as degree 1 isolation.
     */
    READ_UNCOMMITTED,

    /**
     * Indicates that dirty reads are prevented. Non-repeatable reads and
     * phantom reads can occur. This level only prohibits a transaction from
     * reading modifications with uncommitted changes in it.
     *
     * <p>This level is also known as degree 2 isolation.
     */
    READ_COMMITTED,

    /**
     * Indicates that dirty reads and non-repeatable reads are prevented.
     * Phantom reads can occur. This level prohibits a transaction from reading
     * uncommitted changes, and it also prohibits the situation where one
     * transaction reads a record, a second transaction alters the record, and
     * the first transaction rereads the record, getting different values the
     * second time (a "non-repeatable read").
     */
    REPEATABLE_READ,

    /**
     * Indicates that dirty reads, non-repeatable reads and phantom reads are
     * prevented. Commits can still fail however, as snapshot isolation avoids
     * using locks.
     */
    SNAPSHOT,

    /**
     * Indicates that dirty reads, non-repeatable reads and phantom reads are
     * prevented. Phantoms are records returned as a result of a search, but
     * which were not seen by the same transaction when the identical search
     * criteria was previously used. For example, another transaction may have
     * inserted records which match the original search.
     *
     * <p>This level is also known as degree 3 isolation.
     */
    SERIALIZABLE;

    /**
     * Returns true if this isolation level is at least as high as the one
     * given.
     */
    public boolean isAtLeast(IsolationLevel level) {
        return ordinal() >= level.ordinal();
    }

    /**
     * Returns true if this isolation level is no higher than the one given.
     */
    public boolean isAtMost(IsolationLevel level) {
        return ordinal() <= level.ordinal();
    }

    /**
     * Returns the lowest common isolation level between this and the one
     * given.
     */
    public IsolationLevel lowestCommon(IsolationLevel level) {
        return (ordinal() >= level.ordinal()) ? level : this;
    }

    /**
     * Returns the highest common isolation level between this and the one
     * given.
     */
    public IsolationLevel highestCommon(IsolationLevel level) {
        return (ordinal() <= level.ordinal()) ? level : this;
    }
}