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: 44
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
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: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
setRollbackOnly()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
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%
verifyTransactionActive(String)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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 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: EntityTransactionImpl(EntityTransactionWrapper wrapper) {
34: this.wrapper = Objects.requireNonNull(wrapper);
35: }
36:
37: @Override
38: public void begin() {
39:• if (isActive()) {
40: throw new IllegalStateException("Transaction already active!");
41: }
42: wrapper.begin();
43: this.active = true;
44: wrapper.getEntityManager().transactionStarted(this);
45: LOG.trace("EntityTransaction begin.");
46: }
47:
48: @Override
49: public void commit() {
50: verifyTransactionActive("commit");
51: try {
52: LOG.trace("EntityTransaction commit started.");
53:• if (rollbackOnly) {
54: throw new RollbackException("Trying to commit transaction marked as rollback only.");
55: } else {
56: try {
57: wrapper.getTransactionUOW().commit();
58: } catch (RuntimeException ex) {
59: wrapper.getEntityManager().removeCurrentPersistenceContext();
60: throw new RollbackException(ex);
61: }
62: }
63: } finally {
64:• if (wrapper.getTransactionUOW().shouldReleaseAfterCommit()) {
65: wrapper.getEntityManager().removeCurrentPersistenceContext();
66: }
67: cleanup();
68: LOG.trace("EntityTransaction commit finished.");
69: }
70: }
71:
72: private void verifyTransactionActive(String method) {
73:• if (!isActive()) {
74: throw new IllegalStateException("Cannot invoke " + method + " on an inactive transaction!");
75: }
76: }
77:
78: private void cleanup() {
79: this.active = false;
80: this.rollbackOnly = false;
81: wrapper.setTransactionUOW(null);
82: wrapper.getEntityManager().transactionFinished(this);
83: }
84:
85: @Override
86: public void rollback() {
87: verifyTransactionActive("rollback");
88: wrapper.getTransactionUOW().rollback();
89: wrapper.getEntityManager().removeCurrentPersistenceContext();
90: cleanup();
91: LOG.trace("EntityTransaction rolled back.");
92: }
93:
94: @Override
95: public void setRollbackOnly() {
96: verifyTransactionActive("setRollbackOnly");
97: this.rollbackOnly = true;
98: }
99:
100: @Override
101: public boolean isRollbackOnly() {
102: verifyTransactionActive("isRollbackOnly");
103: return this.rollbackOnly;
104: }
105:
106: @Override
107: public boolean isActive() {
108: return active;
109: }
110: }