1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21
22 package org.opencastproject.db;
23
24 import org.opencastproject.util.function.ThrowingConsumer;
25 import org.opencastproject.util.function.ThrowingFunction;
26
27 import java.util.function.Consumer;
28 import java.util.function.Function;
29
30 import javax.persistence.EntityManager;
31
32 /**
33 * DBSession implements common DB query execution handlers.
34 */
35 public interface DBSession extends AutoCloseable {
36 /**
37 * Execute given function without opening a new transaction. It should be assumed that the EntityManager was newly
38 * created.
39 *
40 * @param fn Function to execute.
41 */
42 void exec(Consumer<EntityManager> fn);
43
44 /**
45 * Execute given function that can throw a checked exception without opening a new transaction. It should be assumed
46 * that the EntityManager was newly created.
47 *
48 * @param fn Function to execute.
49 * @param <E> Exception type that could be thrown by fn.
50 * @throws E Exception thrown by fn.
51 */
52 <E extends Throwable> void execChecked(ThrowingConsumer<EntityManager, E> fn) throws E;
53
54 /**
55 * Execute given function without opening a new transaction. It should be assumed that the EntityManager was newly
56 * created. The return value of given function is returned.
57 *
58 * @param fn Function to execute.
59 * @return Object fn has returned.
60 * @param <T> Return type of fn.
61 */
62 <T> T exec(Function<EntityManager, T> fn);
63
64 /**
65 * Execute given function that can throw a checked exception without opening a new transaction. It should be assumed
66 * that the EntityManager was newly created. The return value of given function is returned.
67 *
68 * @param fn Function to execute.
69 * @return Object fn has returned.
70 * @param <T> Return type of fn.
71 * @param <E> Exception type that could be thrown by fn.
72 * @throws E Exception thrown by fn.
73 */
74 <T, E extends Throwable> T execChecked(ThrowingFunction<EntityManager, T, E> fn) throws E;
75
76 /**
77 * Execute given function within a transaction. There can only be a single transaction per DBSession object. It should
78 * be assumed that the EntityManager and transaction are reused if this method is executed again. Further, should be
79 * assumed that fn could be executed multiple times if the transaction fails and should be retried.
80 *
81 * @param fn Function to execute.
82 */
83 void execTx(Consumer<EntityManager> fn);
84
85 /**
86 * Execute given function that can throw a checked exception within a transaction. There can only be a single
87 * transaction per DBSession object. It should be assumed that the EntityManager and transaction are reused if this
88 * method is executed again. Further, should be assumed that fn could be executed multiple times if the transaction
89 * fails and should be retried.
90 *
91 * @param fn Function to execute.
92 * @param <E> Exception type that could be thrown by fn.
93 * @throws E Exception thrown by fn.
94 */
95 <E extends Throwable> void execTxChecked(ThrowingConsumer<EntityManager, E> fn) throws E;
96
97 /**
98 * Execute given function within a transaction. There can only be a single transaction per DBSession object. It should
99 * be assumed that the EntityManager and transaction are reused if this method is executed again. Further, should be
100 * assumed that fn could be executed multiple times if the transaction fails and should be retried.
101 *
102 * @param maxTransactionRetries Maximal number of times fn can be executed again in case of transaction errors.
103 * @param fn Function to execute.
104 */
105 void execTx(int maxTransactionRetries, Consumer<EntityManager> fn);
106
107 /**
108 * Execute given function that can throw a checked exception within a transaction. There can only be a single
109 * transaction per DBSession object. It should be assumed that the EntityManager and transaction are reused if this
110 * method is executed again. Further, should be assumed that fn could be executed multiple times if the transaction
111 * fails and should be retried.
112 *
113 * @param maxTransactionRetries Maximal number of times fn can be executed again in case of transaction errors.
114 * @param fn Function to execute.
115 * @param <E> Exception type that could be thrown by fn.
116 * @throws E Exception thrown by fn.
117 */
118 <E extends Throwable> void execTxChecked(int maxTransactionRetries, ThrowingConsumer<EntityManager, E> fn) throws E;
119
120 /**
121 * Execute given function within a transaction. There can only be a single transaction per DBSession object. It should
122 * be assumed that the EntityManager and transaction are reused if this method is executed again. Further, should be
123 * assumed that fn could be executed multiple times if the transaction fails and should be retried. The return value
124 * of given function is returned.
125 *
126 * @param fn Function to execute.
127 * @return Object fn has returned.
128 * @param <T> Return type of fn.
129 */
130 <T> T execTx(Function<EntityManager, T> fn);
131
132 /**
133 * Execute given function that can throw a checked exception within a transaction. There can only be a single
134 * transaction per DBSession object. It should be assumed that the EntityManager and transaction are reused if this
135 * method is executed again. Further, should be assumed that fn could be executed multiple times if the transaction
136 * fails and should be retried. The return value of given function is returned.
137 *
138 * @param fn Function to execute.
139 * @return Object fn has returned.
140 * @param <T> Return type of fn.
141 * @param <E> Exception type that could be thrown by fn.
142 * @throws E Exception thrown by fn.
143 */
144 <T, E extends Throwable> T execTxChecked(ThrowingFunction<EntityManager, T, E> fn) throws E;
145
146 /**
147 * Execute given function within a transaction. There can only be a single transaction per DBSession object. It should
148 * be assumed that the EntityManager and transaction are reused if this method is executed again. Further, should be
149 * assumed that fn could be executed multiple times if the transaction fails and should be retried. The return value
150 * of given function is returned.
151 *
152 * @param maxTransactionRetries Maximal number of times fn can be executed again in case of transaction errors.
153 * @param fn Function to execute.
154 * @return Object fn has returned.
155 * @param <T> Return type of fn.
156 */
157 <T> T execTx(int maxTransactionRetries, Function<EntityManager, T> fn);
158
159 /**
160 * Execute given function that can throw a checked exception within a transaction. There can only be a single
161 * transaction per DBSession object. It should be assumed that the EntityManager and transaction are reused if this
162 * method is executed again. Further, should be assumed that fn could be executed multiple times if the transaction
163 * fails and should be retried. The return value of given function is returned.
164 *
165 * @param maxTransactionRetries Maximal number of times fn can be executed again in case of transaction errors.
166 * @param fn Function to execute.
167 * @return Object fn has returned.
168 * @param <T> Return type of fn.
169 * @param <E> Exception type that could be thrown by fn.
170 * @throws E Exception thrown by fn.
171 */
172 <T, E extends Throwable> T execTxChecked(int maxTransactionRetries, ThrowingFunction<EntityManager, T, E> fn) throws E;
173
174 /**
175 * Closes this DBSession and cleans up related objects.
176 */
177 void close();
178 }