Skip to contentMethod: isActive()
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.util;
16:
17:
18: import static cz.cvut.kbss.ontodriver.util.TransactionState.*;
19:
20: public final class Transaction {
21:
22: private TransactionState state;
23:
24: /**
25: * Begins a transaction.
26: */
27: public void begin() {
28: if (state != null && state != ABORTED && state != COMMITTED) {
29: throw new IllegalStateException("Cannot begin transaction. Current state is " + state);
30: }
31: this.state = ACTIVE;
32: }
33:
34: /**
35: * Marks the beginning of transaction commit.
36: *
37: * @see #afterCommit()
38: */
39: public void commit() {
40: verifyActive();
41: this.state = PARTIALLY_COMMITTED;
42: }
43:
44: /**
45: * Marks successful completion of transaction commit.
46: *
47: * @see #commit()
48: */
49: public void afterCommit() {
50: if (state != PARTIALLY_COMMITTED) {
51: throw new IllegalStateException("Cannot finish commit. Current state is " + state);
52: }
53: this.state = COMMITTED;
54: }
55:
56: /**
57: * Commences transaction rollback.
58: *
59: * @see #afterRollback()
60: */
61: public void rollback() {
62: if (state != ACTIVE && state != PARTIALLY_COMMITTED && state != FAILED) {
63: throw new IllegalStateException("Cannot rollback transaction. Current state is "
64: + state);
65: }
66: this.state = FAILED;
67: }
68:
69: /**
70: * Marks successful finish of transaction rollback.
71: *
72: * @see #rollback()
73: */
74: public void afterRollback() {
75: if (state != FAILED) {
76: throw new IllegalStateException("Cannot finish rollback. Current state is " + state);
77: }
78: this.state = ABORTED;
79: }
80:
81: public boolean isActive() {
82:• return state == ACTIVE;
83: }
84:
85: public TransactionState getState() {
86: return state;
87: }
88:
89: /**
90: * Verifies that the transaction is active.
91: *
92: * @throws IllegalStateException When transaction is not active
93: */
94: public void verifyActive() {
95: if (!isActive()) {
96: throw new IllegalStateException("Transaction is not active. Current state is " + state);
97: }
98: }
99:
100: @Override
101: public int hashCode() {
102: final int prime = 31;
103: int result = 1;
104: result = prime * result + ((state == null) ? 0 : state.hashCode());
105: return result;
106: }
107:
108: @Override
109: public boolean equals(Object obj) {
110: if (this == obj)
111: return true;
112: if (obj == null)
113: return false;
114: if (getClass() != obj.getClass())
115: return false;
116: Transaction other = (Transaction) obj;
117: return state == other.state;
118: }
119:
120: @Override
121: public String toString() {
122: return state != null ? state.toString() : "null";
123: }
124: }