aboutsummaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
blob: 7103c47cec80c92dd2485f3afcda42924b27ad19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * ============LICENSE_START=======================================================
 * feature-session-persistence
 * ================================================================================
 * Copyright (C) 2017-2018, 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.persistence;

import javax.persistence.EntityManager;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.onap.policy.common.utils.jpa.EntityMgrCloser;

/**
 * Wrapper for an <i>EntityManager</i> that creates a JTA transaction that is auto-rolled
 * back when closed. Note: commit() and rollback() actions do nothing if a transaction was
 * already in progress when this object was constructed.
 */
public class EntityMgrTrans extends EntityMgrCloser {

    /**
     * Transaction to be rolled back.
     */
    @Getter(AccessLevel.PROTECTED)
    @Setter(AccessLevel.PROTECTED)
    private static UserTransaction userTrans = com.arjuna.ats.jta.UserTransaction.userTransaction();

    /**
     * {@code True} if a transaction had already been started before this object was
     * constructed, {@code false} otherwise.
     */
    private boolean transInProgress;

    /**
     * Constructor.
     *
     * @param em entity for which a transaction is to be begun
     */
    public EntityMgrTrans(EntityManager em) {
        super(em);

        try {
            transInProgress = (userTrans.getStatus() == Status.STATUS_ACTIVE);
            if (!transInProgress) {
                userTrans.begin();
            }

            em.joinTransaction();

        } catch (RuntimeException | NotSupportedException | SystemException e) {
            em.close();
            throw new EntityMgrException(e);
        }
    }

    /**
     * Commits the transaction.
     */
    public void commit() {
        if (transInProgress) {
            return;
        }

        try {
            userTrans.commit();

        } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException
                        | HeuristicRollbackException | SystemException e) {

            throw new EntityMgrException(e);
        }
    }

    /**
     * Rolls back the transaction.
     */
    public void rollback() {
        if (transInProgress) {
            return;
        }

        try {
            userTrans.rollback();

        } catch (IllegalStateException | SecurityException | SystemException e) {
            throw new EntityMgrException(e);
        }
    }

    @Override
    public void close() {
        try {
            if (!transInProgress && userTrans.getStatus() == Status.STATUS_ACTIVE) {
                userTrans.rollback();
            }

        } catch (IllegalStateException | SecurityException | SystemException e) {
            throw new EntityMgrException(e);

        } finally {
            super.close();
        }
    }

    /**
     * Runtime exceptions generated by this class. Wraps exceptions generated by delegated
     * operations, particularly when they are not, themselves, Runtime exceptions.
     */
    public static class EntityMgrException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        /**
         * Constructor.
         *
         * @param ex exception to be wrapped
         */
        public EntityMgrException(Exception ex) {
            super(ex);
        }
    }
}