Skip to content

Package: EntityTransactionImpl

EntityTransactionImpl

nameinstructionbranchcomplexitylinemethod
EntityTransactionImpl(EntityTransactionWrapper)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
begin()
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
cleanup()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
commit()
M: 0 C: 49
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
finalize()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isActive()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isRollbackOnly()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
setRollbackOnly()
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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