Skip to content

Package: EntityTransactionImpl

EntityTransactionImpl

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