Skip to content

Method: rollback()

1: /**
2: * Copyright (C) 2016 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.jopa.transactions;
16:
17: import cz.cvut.kbss.jopa.exceptions.RollbackException;
18: import cz.cvut.kbss.jopa.utils.ErrorUtils;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: import java.util.Objects;
23:
24: public class EntityTransactionImpl implements EntityTransaction {
25:
26: private static final Logger LOG = LoggerFactory.getLogger(EntityTransactionImpl.class);
27:
28: private boolean active = false;
29:
30: private boolean rollbackOnly = false;
31:
32: private final EntityTransactionWrapper wrapper;
33:
34: public EntityTransactionImpl(EntityTransactionWrapper wrapper) {
35: super();
36: this.wrapper = Objects.requireNonNull(wrapper, ErrorUtils.constructNPXMessage("wrapper"));
37: }
38:
39: /**
40: * Starts the current transaction.
41: *
42: * @throws IllegalStateException if the transaction is already active
43: */
44: @Override
45: public void begin() {
46: if (isActive()) {
47: throw new IllegalStateException("Transaction already active!");
48: }
49: wrapper.begin();
50: this.active = true;
51: wrapper.getEntityManager().transactionStarted(this);
52: LOG.trace("EntityTransaction begin.");
53: }
54:
55: /**
56: * Commit the current transaction.
57: *
58: * @throws IllegalStateException when the transaction is not active
59: */
60: @Override
61: public void commit() {
62: if (!isActive()) {
63: throw new IllegalStateException("Cannot commit inactive transaction!");
64: }
65: try {
66: LOG.trace("EntityTransaction commit started.");
67: if (rollbackOnly) {
68: throw new RollbackException("Trying to commit transaction marked as rollback only.");
69: } else {
70: try {
71: wrapper.getTransactionUOW().commit();
72: } catch (RuntimeException ex) {
73: wrapper.getEntityManager().removeCurrentPersistenceContext();
74: throw new RollbackException(ex);
75: }
76: }
77: } finally {
78: if (wrapper.getTransactionUOW().shouldReleaseAfterCommit()) {
79: wrapper.getEntityManager().removeCurrentPersistenceContext();
80: }
81: cleanup();
82: LOG.trace("EntityTransaction commit finished.");
83: }
84: }
85:
86: private void cleanup() {
87: this.active = false;
88: this.rollbackOnly = false;
89: wrapper.setTransactionUOW(null);
90: wrapper.getEntityManager().transactionFinished(this);
91: }
92:
93: /**
94: * Roll back the current transaction. Dismiss any changes made.
95: *
96: * @throws IllegalStateException when the transaction is not active
97: */
98: @Override
99: public void rollback() {
100:• if (!isActive()) {
101: throw new IllegalStateException("Cannot rollback inactive transaction!");
102: }
103: wrapper.getTransactionUOW().rollback();
104: wrapper.getEntityManager().removeCurrentPersistenceContext();
105: cleanup();
106: LOG.trace("EntityTransaction rolled back.");
107: }
108:
109: /**
110: * Mark this transaction as rollback only. I. e. the only possible outcome of this transaction is rollback.
111: *
112: * @throws IllegalStateException when the transaction is not active
113: */
114: @Override
115: public void setRollbackOnly() {
116: if (!isActive()) {
117: throw new IllegalStateException("Cannot set rollbackOnly on inactive transaction!");
118: }
119: this.rollbackOnly = true;
120: }
121:
122: /**
123: * Is is this transaction marked as rollbackOnly?
124: *
125: * @throws IllegalStateException when the transacion is not active
126: */
127: @Override
128: public boolean isRollbackOnly() {
129: if (!isActive()) {
130: throw new IllegalStateException("Accessing rollbackOnly on inactive transaction!");
131: }
132: return this.rollbackOnly;
133: }
134:
135: @Override
136: public boolean isActive() {
137: return active;
138: }
139:
140: /**
141: * Roll back any changes if we forgot to commit or roll it back manually
142: */
143: @Override
144: public void finalize() throws Throwable {
145: if (isActive()) {
146: rollback();
147: }
148: super.finalize();
149: }
150:
151: }