aboutsummaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
blob: 47ed221e7f431d8c0942a0539d6f0ab7f1f56753 (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
/*
 * ============LICENSE_START=======================================================
 * feature-session-persistence
 * ================================================================================
 * Copyright (C) 2017-2018 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 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.
 */
public class EntityMgrTrans extends EntityMgrCloser {

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

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

		try {
			userTrans.begin();
			em.joinTransaction();

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

	/**
	 * Gets the user transaction. For use by junit tests.
	 * 
	 * @return the user transaction
	 */
	protected static UserTransaction getUserTrans() {
		return userTrans;
	}

	/**
	 * Sets the user transaction. For use by junit tests.
	 * 
	 * @param userTrans
	 *            the new user transaction
	 */
	protected static void setUserTrans(UserTransaction userTrans) {
		EntityMgrTrans.userTrans = userTrans;
	}

	/**
	 * Commits the transaction.
	 */
	public void commit() {
		try {
			userTrans.commit();

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

			throw new EntityMgrException(e);
		}
	}

	/**
	 * Rolls back the transaction.
	 */
	public void rollback() {
		try {
			userTrans.rollback();

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

	@Override
	public void close() {
		try {
			if (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;

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