author | lancea |
Wed, 11 Jul 2018 19:36:23 -0400 | |
branch | JDK-8188051-branch |
changeset 56828 | 64304e37e9b1 |
parent 56824 | 62e92191354d |
child 56832 | 4f7713e6a308 |
permissions | -rw-r--r-- |
56824 | 1 |
/* |
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package jdk.incubator.sql2; |
|
26 |
||
27 |
import java.time.Duration; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.concurrent.CompletionStage; |
|
30 |
import java.util.function.Consumer; |
|
31 |
import java.util.function.LongConsumer; |
|
32 |
||
33 |
/** |
|
34 |
* A {@link Session} is an abstraction of a SQL database and |
|
35 |
* a group of {@link Operation}s to be executed by that SQL database. No method |
|
36 |
* on {@link Session} or any of its dependent objects ({@link RowOperation} |
|
37 |
* etc) blocks. Any method that might block must execute any potentially blocking |
|
38 |
* action in a thread other than the calling thread. |
|
39 |
* |
|
40 |
* <p> |
|
41 |
* A {@link Session} is independent of any particular data source. Any data |
|
42 |
* source that meets the specifications set by the {@link Session.Builder} can |
|
43 |
* be used to execute the {@link Operation}s submitted to the {@link Session]. |
|
44 |
* An application is expected to create, use, and close {@link Session}s as |
|
45 |
* needed. An application should hold a {@link Session} only when required by |
|
46 |
* data source semantics. An implementation should cache and reused data source |
|
47 |
* resources as appropriate. {@link Session}s should not be cached. |
|
48 |
* |
|
49 |
* <p> |
|
50 |
* An implementation of this type must be thread safe as result and error |
|
51 |
* handlers running asynchronously may be accessing a {@link Session} in |
|
52 |
* parallel with each other and with a user thread. {@link Session}s are not |
|
53 |
* required to support multiplexed use; a single {@link Session} should be |
|
54 |
* used for only one unit of work at a time. Executing independent units of work |
|
55 |
* on a single {@link Session} in parallel will most likely lead to |
|
56 |
* unpredictable outcomes. As a rule of thumb only one user thread should access |
|
57 |
* a {@link Session} at a time. Such a user thread should execute a complete |
|
58 |
* unit of work before another user thread accesses the {@link Session}. An |
|
59 |
* implementation may support parallel multiplexed use, but it is not required.</p> |
|
60 |
* |
|
61 |
* <p> |
|
62 |
* All methods inherited from OperationGroup throw IllegalStateException if the |
|
63 |
* the {@link Session} is not active.</p> |
|
64 |
*/ |
|
65 |
public interface Session extends AutoCloseable, OperationGroup<Object, Object> { |
|
66 |
||
67 |
/** |
|
68 |
* Identifies the operational state of a {@link Session}. |
|
69 |
*/ |
|
70 |
public enum Lifecycle { |
|
71 |
/** |
|
72 |
* unattached. When a attach {@link Operation} is completed successfully |
|
73 |
* -> {@link OPEN}. If {@link deactivate} is called -> |
|
74 |
* {@link NEW_INACTIVE}. If {@link abort} is called -> {@link ABORTING}. |
|
75 |
* No {@link Operation}s other than attach and close will be performed. A |
|
76 |
Session in this state is both 'open' and 'active'. |
|
77 |
*/ |
|
78 |
NEW, |
|
79 |
/** |
|
80 |
* Unattached and inactive. Any queued attach or close {@link Operation} |
|
81 |
* is performed. No work can be submitted. If the {@link activate} method is |
|
82 |
* called -> {@link NEW}. If a attach {@link Operation} completes -> |
|
83 |
* {@link INACTIVE}. If a close {@link Operation} is executed -> |
|
84 |
* {@link CLOSING}. If {@link abort} is called -> {@link ABORTING}. A |
|
85 |
Session in this state is 'open'. |
|
86 |
*/ |
|
87 |
NEW_INACTIVE, |
|
88 |
/** |
|
89 |
* fully operational. Work is queued and performed. If {@link deactivate} is |
|
90 |
* called -> {@link INACTIVE}. If a close {@link Operation} is executed |
|
91 |
* -> {@link CLOSING}. If {@link abort} is called -> {@link ABORTING}. |
|
92 |
A Session in this state is both 'open' and 'active'. |
|
93 |
*/ |
|
94 |
OPEN, |
|
95 |
/** |
|
96 |
* Not available for new work. Queued work is performed. No work can be |
|
97 |
* submitted. If the {@link activate} method is called -> {@link OPEN}. |
|
98 |
* If a close {@link Operation} is executed -> {@link CLOSING}. If |
|
99 |
* {@link abort} is called -> {@link ABORTING}. A {@link Session} in |
|
100 |
* this state is 'open'. |
|
101 |
*/ |
|
102 |
INACTIVE, |
|
103 |
/** |
|
104 |
* Work in progress is completed but no additional work is started or |
|
105 |
* queued. Attempting to queue work throws {@link IllegalStateException}. |
|
106 |
* When the currently executing {@link Operation}s are completed -> |
|
107 |
* {@link CLOSED}. All other queued Operations are completed exceptionally |
|
108 |
with SqlSkippedException. A Session in this state is 'closed'. |
|
109 |
*/ |
|
110 |
CLOSING, |
|
111 |
/** |
|
112 |
* Work is neither queued nor performed. The currently executing |
|
113 |
* {@link Operation}s, if any, are terminated, exceptionally if necessary. |
|
114 |
* Any queued {@link Operation}s are terminated exceptionally with |
|
115 |
* {@link SqlSkippedException}. Attempting to queue work throws |
|
116 |
* {@link IllegalStateException}. When the queue is empty -< |
|
117 |
* {@link CLOSED}. A Session in this state is 'closed'. |
|
118 |
*/ |
|
119 |
ABORTING, |
|
120 |
/** |
|
121 |
* Work is neither queued nor performed. Attempting to queue work throws |
|
122 |
* {@link IllegalStateException}. A Session in this state is 'closed'. |
|
123 |
*/ |
|
124 |
CLOSED; |
|
125 |
||
126 |
static { |
|
127 |
NEW.init(true, true, NEW, NEW_INACTIVE, OPEN, ABORTING, CLOSING, CLOSED); |
|
128 |
NEW_INACTIVE.init(true, false, NEW, NEW_INACTIVE, INACTIVE, ABORTING, CLOSING, CLOSED); |
|
129 |
OPEN.init(true, true, OPEN, INACTIVE, OPEN, ABORTING, CLOSING, CLOSED); |
|
130 |
INACTIVE.init(true, false, OPEN, INACTIVE, INACTIVE, ABORTING, INACTIVE, INACTIVE); |
|
131 |
CLOSING.init(false, true, CLOSING, CLOSING, CLOSING, ABORTING, CLOSING, CLOSED); |
|
132 |
ABORTING.init(false, true, ABORTING, ABORTING, ABORTING, ABORTING, ABORTING, CLOSED); |
|
133 |
CLOSED.init(false, true, CLOSED, CLOSED, CLOSED, CLOSED, CLOSED, CLOSED); |
|
134 |
} |
|
135 |
||
136 |
private boolean isOpen; |
|
137 |
private boolean isActive; |
|
138 |
private Lifecycle onActivate; |
|
139 |
private Lifecycle onDeactivate; |
|
140 |
private Lifecycle onAttach; |
|
141 |
private Lifecycle onAbort; |
|
142 |
private Lifecycle onClose; |
|
143 |
private Lifecycle onClosed; |
|
144 |
||
145 |
private void init(boolean io, boolean ia, Lifecycle ac, Lifecycle da, Lifecycle cn, Lifecycle ab, Lifecycle cl, Lifecycle cd) { |
|
146 |
isOpen = io; |
|
147 |
isActive = ia; |
|
148 |
onActivate = ac; |
|
149 |
onDeactivate = da; |
|
150 |
onAttach = cn; |
|
151 |
onAbort = ab; |
|
152 |
onClose = cl; |
|
153 |
onClosed = cd; |
|
154 |
} |
|
155 |
public boolean isOpen() { |
|
156 |
return isOpen; |
|
157 |
} |
|
158 |
||
159 |
public boolean isActive() { |
|
160 |
return isActive; |
|
161 |
} |
|
162 |
||
163 |
public Lifecycle activate() { |
|
164 |
return onActivate; |
|
165 |
} |
|
166 |
||
167 |
public Lifecycle deactivate() { |
|
168 |
return onDeactivate; |
|
169 |
} |
|
170 |
||
171 |
public Lifecycle attach() { |
|
172 |
return onAttach; |
|
173 |
} |
|
174 |
||
175 |
public Lifecycle abort() { |
|
176 |
return onAbort; |
|
177 |
} |
|
178 |
||
179 |
public Lifecycle close() { |
|
180 |
return onClose; |
|
181 |
} |
|
182 |
||
183 |
public Lifecycle closed() { |
|
184 |
return onClosed; |
|
185 |
} |
|
186 |
||
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Specifiers for how much effort to put into validating a {@link Session}. |
|
191 |
* The amount of effort put into checking should be non-decreasing from NONE |
|
192 |
* (least effort) to COMPLETE (most effort). Exactly what is checked is |
|
193 |
* implementation dependent. For example, a memory resident database driver |
|
194 |
* might implement SOCKET and NETWORK to be the same as LOCAL. SERVER might |
|
195 |
* verify that a database manager thread is running and COMPLETE might trigger |
|
196 |
* the database manager thread to run a deadlock detection algorithm. |
|
197 |
*/ |
|
198 |
public enum Validation { |
|
199 |
/** |
|
200 |
* isValid fails only if the {@link Session} is closed. |
|
201 |
*/ |
|
202 |
NONE, |
|
203 |
/** |
|
204 |
* {@link NONE} plus check local resources |
|
205 |
*/ |
|
206 |
LOCAL, |
|
207 |
/** |
|
208 |
* {@link LOCAL} plus the server isn't obviously unreachable (dead socket) |
|
209 |
*/ |
|
210 |
SOCKET, |
|
211 |
/** |
|
212 |
* {@link SOCKET} plus the network is intact (network PING) |
|
213 |
*/ |
|
214 |
NETWORK, |
|
215 |
/** |
|
216 |
* {@link NETWORK} plus significant server processes are running |
|
217 |
*/ |
|
218 |
SERVER, |
|
219 |
/** |
|
220 |
* everything that can be checked is working. At least {@link SERVER}. |
|
221 |
*/ |
|
222 |
COMPLETE; |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* A Listener that is notified of changes in a Session's lifecycle. |
|
227 |
*/ |
|
228 |
public interface SessionLifecycleListener extends java.util.EventListener { |
|
229 |
||
230 |
/** |
|
231 |
* If this {@link java.util.EventListener} is registered with a |
|
232 |
* {@link Session} this method is called whenever that |
|
233 |
* {@link Session}'s lifecycle changes. Note that the lifecycle may have |
|
234 |
* changed again by the time this method is called so the |
|
235 |
* {@link Session}'s current lifecycle may be different from the value of |
|
236 |
* {@code current}. |
|
237 |
* |
|
238 |
* @param session the {@link Session} |
|
239 |
* @param previous the previous value of the lifecycle |
|
240 |
* @param current the new value of the lifecycle |
|
241 |
*/ |
|
242 |
public void lifecycleEvent(Session session, Lifecycle previous, Lifecycle current); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* A {@link Session} builder. A {@link Session} is initially in the |
|
247 |
* {@link Session.Lifecycle#NEW} lifecycle state. It transitions to the |
|
248 |
* {@link Session.Lifecycle#OPEN} lifecycle state when fully initialized or |
|
249 |
* to {@link Session.Lifecycle#CLOSED} if initialization fails. |
|
250 |
* |
|
251 |
*/ |
|
252 |
public interface Builder { |
|
253 |
||
254 |
/** |
|
255 |
* Specify a property and its value for the built {@link Session}. |
|
256 |
* |
|
257 |
* @param p {@link SessionProperty} to set. Not {@code null}. |
|
258 |
* @param v value for the property |
|
259 |
* @return this {@link Builder} |
|
260 |
* @throws IllegalArgumentException if {@code p.validate(v)} does not return |
|
261 |
* true, if this method has already been called with the property |
|
262 |
* {@code p}, or the implementation does not support the {@link SessionProperty}. |
|
263 |
*/ |
|
264 |
public Builder property(SessionProperty p, Object v); |
|
265 |
||
266 |
/** |
|
267 |
* Return a {@link Session} with the attributes specified. Note that the |
|
268 |
* {@link Session} may not be attached to a server. Call one of the |
|
269 |
* {@link attach} convenience methods to attach the {@link Session} to |
|
270 |
* a server. The lifecycle of the new {@link Session} is |
|
271 |
* {@link Lifecycle#NEW}. |
|
272 |
||
273 |
This method cannot block. If the DataSource is unable to support a new |
|
274 |
Session when this method is called, this method throws SqlException. |
|
275 |
Note that the implementation does not have to allocate scarce resources to |
|
276 |
the new {@link Session} when this method is called so limiting the |
|
277 |
* number of {@link Session}s is not required to limit the use of |
|
278 |
* scarce resources. It may be appropriate to limit the number of |
|
279 |
* {@link Session}s for other reasons, but that is implementation dependent. |
|
280 |
* |
|
281 |
* @return a {@link Session} |
|
282 |
* @throws IllegalStateException if this method has already been called or |
|
283 |
if the implementation cannot create a Session with the specified |
|
284 |
{@link SessionProperty}s. |
|
285 |
* @throws IllegalStateException if the {@link DataSource} that created this |
|
286 |
* {@link Builder} is closed |
|
287 |
* @throws SqlException if creating a {@link Session} would exceed some |
|
288 |
* limit |
|
289 |
*/ |
|
290 |
public Session build(); |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* Returns an {@link Operation} that attaches this {@link Session} to a |
|
295 |
* data source. If the Operation completes successfully and the lifecycle is |
|
296 |
* {@link Lifecycle#NEW} -> {@link Lifecycle#OPEN}. If lifecycle is |
|
297 |
* {@link Lifecycle#NEW_INACTIVE} -> {@link Lifecycle#INACTIVE}. If the |
|
298 |
* {@link Operation} completes exceptionally the lifecycle -> |
|
299 |
* {@link Lifecycle#CLOSED}. The lifecycle must be {@link Lifecycle#NEW} or |
|
300 |
* {@link Lifecycle#NEW_INACTIVE} when the {@link Operation} is executed. |
|
301 |
* Otherwise the {@link Operation} will complete exceptionally with |
|
302 |
* {@link SqlException}. |
|
303 |
* |
|
304 |
* Note: It is highly recommended to use the {@link attach()} convenience |
|
305 |
* method or to use {@link DataSource#getSession} which itself calls |
|
306 |
* {@link attach()}. Unless there is a specific need, do not call this method |
|
307 |
* directly. |
|
308 |
* |
|
309 |
* @return an {@link Operation} that attaches this {@link Session} to a |
|
310 |
* server. |
|
311 |
* @throws IllegalStateException if this {@link Session} is in a lifecycle |
|
312 |
* state other than {@link Lifecycle#NEW}. |
|
313 |
*/ |
|
314 |
public Operation<Void> attachOperation(); |
|
315 |
||
316 |
/** |
|
317 |
* Convenience method that supports the fluent style of the builder needed by |
|
318 |
* try with resources. |
|
319 |
* |
|
320 |
* Note: A {@link Session} is an {@link OperationGroup} and so has some |
|
321 |
* advanced features that most users do not need. Management of these features |
|
322 |
* is encapsulated in this method and the corresponding {@link close()} |
|
323 |
* convenience method. The vast majority of users should just use these |
|
324 |
* methods and not worry about the advanced features. The convenience methods |
|
325 |
* do the right thing for the overwhelming majority of use cases. A tiny |
|
326 |
* number of users might want to take advantage of the advanced features that |
|
327 |
* {@link OperationGroup} brings to {@link Session} and so would call |
|
328 |
* {@link attachOperation} directly. |
|
329 |
* |
|
330 |
* @return this Session |
|
331 |
* @throws IllegalStateException if this {@link Session} is in a lifecycle |
|
332 |
* state other than {@link Lifecycle#NEW}. |
|
333 |
*/ |
|
334 |
public default Session attach() { |
|
335 |
this.submitHoldingForMoreMembers(); |
|
336 |
this.attachOperation() |
|
337 |
.submit(); |
|
338 |
return this; |
|
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* Convenience method that supports the fluent style of the builder needed by |
|
343 |
* try with resources. |
|
344 |
* |
|
345 |
* @param onError an Exception handler that is called if the attach |
|
346 |
* {@link Operation} completes exceptionally. |
|
347 |
* @return this {@link Session} |
|
348 |
* @throws IllegalStateException if this {@link Session} is in a lifecycle |
|
349 |
* state other than {@link Lifecycle#NEW}. |
|
350 |
*/ |
|
351 |
public default Session attach(Consumer<Throwable> onError) { |
|
352 |
this.submitHoldingForMoreMembers(); |
|
353 |
this.attachOperation() |
|
354 |
.submit() |
|
355 |
.getCompletionStage() |
|
356 |
.exceptionally(t -> { onError.accept(t); return null; } ); |
|
357 |
return this; |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Returns an {@link Operation} that verifies that the resources are available |
|
362 |
* and operational. Successful completion of that {@link Operation} implies |
|
363 |
* that at some point between the beginning and end of the {@link Operation} |
|
364 |
the Session was working properly to the extent specified by {@code depth}. |
|
365 |
* There is no guarantee that the {@link Session} is still working after |
|
366 |
* completion. If the {@link Session} is not valid the Operation completes |
|
367 |
* exceptionally. |
|
368 |
* |
|
369 |
* @param depth how completely to check that resources are available and |
|
370 |
* operational. Not {@code null}. |
|
371 |
* @return an {@link Operation} that will validate this {@link Session} |
|
372 |
* @throws IllegalStateException if this Session is not active |
|
373 |
*/ |
|
374 |
public Operation<Void> validationOperation(Validation depth); |
|
375 |
||
376 |
/** |
|
377 |
* Convenience method to validate a {@link Session}. |
|
378 |
* |
|
379 |
* @param depth how completely to check that resources are available and |
|
380 |
* operational. Not {@code null}. |
|
381 |
* @param minTime how long to wait. If 0, wait forever |
|
382 |
* @param onError called if validation fails or times out. May be |
|
383 |
* {@code null}. |
|
384 |
* @return this {@link Session} |
|
385 |
* @throws IllegalArgumentException if {@code milliseconds} < 0 or |
|
386 |
* {@code depth} is {@code null}. |
|
387 |
* @throws IllegalStateException if this Session is not active |
|
388 |
*/ |
|
389 |
public default Session validate(Validation depth, |
|
390 |
Duration minTime, |
|
391 |
Consumer<Throwable> onError) { |
|
392 |
this.validationOperation(depth) |
|
393 |
.timeout(minTime) |
|
394 |
.onError(onError) |
|
395 |
.submit(); |
|
396 |
return this; |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* Create an {@link Operation} to close this {@link Session}. When the |
|
401 |
* {@link Operation} is executed, if this {@link Session} is open -> |
|
402 |
* {@link Lifecycle#CLOSING}. If this {@link Session} is closed executing |
|
403 |
* the returned {@link Operation} is a no-op. When the queue is empty and all |
|
404 |
* resources released -> {@link Lifecycle#CLOSED}. |
|
405 |
* |
|
406 |
* A close {@link Operation} is never skipped. Even when the |
|
407 |
* {@link Session} is dependent, the default, and an {@link Operation} |
|
408 |
* completes exceptionally, a close {@link Operation} is still executed. If |
|
409 |
* the {@link Session} is parallel, a close {@link Operation} is not |
|
410 |
* executed so long as there are other {@link Operation}s or the |
|
411 |
* {@link Session} is held for more {@link Operation}s. |
|
412 |
* |
|
413 |
* Note: It is highly recommended to use try with resources or the |
|
414 |
* {@link close()} convenience method. Unless there is a specific need, do not |
|
415 |
* call this method directly. |
|
416 |
* |
|
417 |
* @return an {@link Operation} that will close this {@link Session}. |
|
418 |
* @throws IllegalStateException if the Session is not active |
|
419 |
*/ |
|
420 |
public Operation<Void> closeOperation(); |
|
421 |
||
422 |
/** |
|
423 |
* Create and submit an {@link Operation} to close this {@link Session}. |
|
424 |
* Convenience method. |
|
425 |
* |
|
426 |
* Note: A {@link Session} is an {@link OperationGroup} and so has some |
|
427 |
* advanced features; that most users do not need. Management of these |
|
428 |
* features is encapsulated in this method and the corresponding |
|
429 |
* {@link attach()} convenience method. The vast majority of users should |
|
430 |
* just use these methods and not worry about the advanced features. The |
|
431 |
* convenience methods do the right thing for the overwhelming majority of use |
|
432 |
* cases. A tiny number of user might want to take advantage of the advanced |
|
433 |
* features that {@link OperationGroup} brings to {@link Session} and so |
|
434 |
* would call {@link closeOperation} directly. |
|
435 |
* |
|
436 |
* @throws IllegalStateException if the Session is not active |
|
437 |
*/ |
|
438 |
@Override |
|
439 |
public default void close() { |
|
440 |
this.closeOperation() |
|
441 |
.submit(); |
|
442 |
this.releaseProhibitingMoreMembers(); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Create a new {@link OperationGroup} for this {@link Session}. |
|
447 |
* |
|
448 |
* @param <S> the result type of the member {@link Operation}s of the returned |
|
449 |
* {@link OperationGroup} |
|
450 |
* @param <T> the result type of the collected results of the member |
|
451 |
* {@link Operation}s |
|
452 |
* @return a new {@link OperationGroup}. |
|
453 |
* @throws IllegalStateException if this Session is not active |
|
454 |
*/ |
|
455 |
public <S, T> OperationGroup<S, T> operationGroup(); |
|
456 |
||
457 |
/** |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
458 |
* Returns a new {@link TransactionCompletion} that can be used as an argument to an |
56824 | 459 |
* endTransaction Operation. |
460 |
* |
|
461 |
* It is most likely an error to call this within an error handler, or any |
|
462 |
* handler as it is very likely that when the handler is executed the next |
|
463 |
* submitted endTransaction {@link Operation} will have been created with a |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
464 |
different TransactionCompletion. |
56824 | 465 |
|
466 |
ISSUE: Should this be moved to OperationGroup? |
|
467 |
* |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
468 |
* @return a new {@link TransactionCompletion}. Not null. |
56824 | 469 |
* @throws IllegalStateException if this Session is not active |
470 |
*/ |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
471 |
public TransactionCompletion transactionCompletion(); |
56824 | 472 |
|
473 |
/** |
|
474 |
* Unconditionally perform a transaction rollback. Create an endTransaction |
|
475 |
* {@link Operation}, set it to rollback only, and submit it. The endTransaction |
|
476 |
* is never skipped. Convenience method. To execute a commit call |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
477 |
* {@link OperationGroup#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion)}. |
56824 | 478 |
* |
479 |
* @return this {@link OperationGroup} |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
480 |
* @see OperationGroup#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion) |
56824 | 481 |
*/ |
482 |
public default CompletionStage<TransactionOutcome> rollback() { |
|
56828
64304e37e9b1
JDK-8188051-branch javadoc updates and added TransactionCompletion.java
lancea
parents:
56824
diff
changeset
|
483 |
TransactionCompletion t = transactionCompletion(); |
56824 | 484 |
t.setRollbackOnly(); |
485 |
catchErrors(); |
|
486 |
return this.endTransactionOperation(t).submit().getCompletionStage(); |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* Register a listener that will be called whenever there is a change in the |
|
491 |
* lifecycle of this {@link Session}.If the listener is already registered |
|
492 |
this is a no-op. ISSUE: Should lifecycleListener be a SessionProperty so that it is |
|
493 |
always reestablished on Session.activate? |
|
494 |
* |
|
495 |
* @param listener Not {@code null}. |
|
496 |
* @return this Session |
|
497 |
* @throws IllegalStateException if this Session is not active |
|
498 |
*/ |
|
499 |
public Session registerLifecycleListener(SessionLifecycleListener listener); |
|
500 |
||
501 |
/** |
|
502 |
* Removes a listener that was registered by calling |
|
503 |
* registerLifecycleListener.Sometime after this method is called the listener |
|
504 |
* will stop receiving lifecycle events. If the listener is not registered, |
|
505 |
* this is a no-op. |
|
506 |
* |
|
507 |
* @param listener Not {@code null}. |
|
508 |
* @return this Session |
|
509 |
* @throws IllegalStateException if this Session is not active |
|
510 |
*/ |
|
511 |
public Session deregisterLifecycleListener(SessionLifecycleListener listener); |
|
512 |
||
513 |
/** |
|
514 |
* Return the current lifecycle of this {@link Session}. |
|
515 |
* |
|
516 |
* @return the current lifecycle of this {@link Session}. |
|
517 |
*/ |
|
518 |
public Lifecycle getSessionLifecycle(); |
|
519 |
||
520 |
/** |
|
521 |
* Terminate this {@link Session}. If lifecycle is |
|
522 |
* {@link Lifecycle#NEW}, {@link Lifecycle#OPEN}, {@link Lifecycle#INACTIVE} |
|
523 |
* or {@link Lifecycle#CLOSING} -> {@link Lifecycle#ABORTING} If lifecycle |
|
524 |
* is {@link Lifecycle#ABORTING} or {@link Lifecycle#CLOSED} this is a no-op. |
|
525 |
* If an {@link Operation} is currently executing, terminate it immediately. |
|
526 |
* Remove all remaining {@link Operation}s from the queue. {@link Operation}s |
|
527 |
* are not skipped. They are just removed from the queue. |
|
528 |
* |
|
529 |
* @return this {@link Session} |
|
530 |
*/ |
|
531 |
public Session abort(); |
|
532 |
||
533 |
/** |
|
534 |
* Return the set of properties configured on this {@link Session} |
|
535 |
* excepting any sensitive properties. Neither the key nor the value for |
|
536 |
* sensitive properties are included in the result. Properties (other than |
|
537 |
* sensitive properties) that have default values are included even when not |
|
538 |
* explicitly set. Properties that have no default value and are not set |
|
539 |
* explicitly are not included. |
|
540 |
* |
|
541 |
* @return a {@link Map} of property, value. Not modifiable. May be retained. |
|
542 |
* Not {@code null}. |
|
543 |
* @throws IllegalStateException if this Session is not active |
|
544 |
*/ |
|
545 |
public Map<SessionProperty, Object> getProperties(); |
|
546 |
||
547 |
/** |
|
548 |
* |
|
549 |
* @return a {@link ShardingKey.Builder} for this {@link Session} |
|
550 |
* @throws IllegalStateException if this Session is not active |
|
551 |
*/ |
|
552 |
public ShardingKey.Builder shardingKeyBuilder(); |
|
553 |
||
554 |
/** |
|
555 |
* Provide a method that this {@link Session} will call to control the rate |
|
556 |
* of {@link Operation} submission. This {@link Session} will call |
|
557 |
* {@code request} with a positive argument when the {@link Session} is |
|
558 |
* able to accept more {@link Operation} submissions. The difference between |
|
559 |
* the sum of all arguments passed to {@code request} and the number of |
|
560 |
* {@link Operation}s submitted after this method is called is the |
|
561 |
* <i>demand</i>. The demand must always be non-negative. If an |
|
562 |
* {@link Operation} is submitted that would make the demand negative the call |
|
563 |
* to {@link Operation#submit} throws {@link IllegalStateException}. Prior to |
|
564 |
* a call to {@code requestHook}, the demand is defined to be infinite. |
|
565 |
* After a call to {@code requestHook}, the demand is defined to be |
|
566 |
* zero and is subsequently computed as described previously. |
|
567 |
* {@link Operation}s submitted prior to the call to {@code requestHook} do |
|
568 |
* not affect the demand. |
|
569 |
* |
|
570 |
* @param request accepts calls to increase the demand. Not null. |
|
571 |
* @return this {@link Session} |
|
572 |
* @throws IllegalStateException if this method has been called previously or |
|
573 |
* this {@link Session} is not active. |
|
574 |
*/ |
|
575 |
public Session requestHook(LongConsumer request); |
|
576 |
||
577 |
/** |
|
578 |
* Make this {@link Session} ready for use. A newly created |
|
579 |
* {@link Session} is active. Calling this method on a {@link Session} |
|
580 |
* that is active is a no-op. If the lifecycle is {@link Lifecycle#INACTIVE} |
|
581 |
* -> {@link Lifecycle#OPEN}. If the lifecycle is |
|
582 |
* {@link Lifecycle#NEW_INACTIVE} -> {@link Lifecycle#NEW}. |
|
583 |
* |
|
584 |
* @return this {@link Session} |
|
585 |
* @throws IllegalStateException if this {@link Session} is closed. |
|
586 |
*/ |
|
587 |
public Session activate(); |
|
588 |
||
589 |
/** |
|
590 |
* Makes this {@link Session} inactive. After a call to this method |
|
591 |
* previously submitted Operations will be executed normally. If the lifecycle |
|
592 |
* is {@link Lifecycle#NEW} -> {@link Lifecycle#NEW_INACTIVE}. if the |
|
593 |
* lifecycle is {@link Lifecycle#OPEN} -> {@link Lifecycle#INACTIVE}. If |
|
594 |
* the lifecycle is {@link Lifecycle#INACTIVE} or |
|
595 |
* {@link Lifecycle#NEW_INACTIVE} this method is a no-op. After calling this |
|
596 |
* method or calling any method other than {@link deactivate}, {@link activate}, |
|
597 |
* {@link abort}, or {@link getSessionLifecycle} will throw |
|
598 |
* {@link IllegalStateException}. Data source state not specified |
|
599 |
* by {@link Session.Builder} may not be preserved. |
|
600 |
* |
|
601 |
* <p> |
|
602 |
* In general {@link Session}s should not be pooled as the implementation |
|
603 |
* should cache and reuse the data source resources that back {@link Session}s |
|
604 |
* as appropriate, not cache the {@link Session}s themselves. |
|
605 |
* However, any {@link Session} pool is required by default to |
|
606 |
* call {@code deactivate} when putting a {@link Session} into the pool. The |
|
607 |
* pool is required by default to call {@code activate} when removing a |
|
608 |
* {@link Session} from the pool for use. A pool may have an optional mode where |
|
609 |
* it does not call {@code deactivate}/{@code activate} as required above. The |
|
610 |
* behavior of the pool and {@link Session}s cached in the pool in such a |
|
611 |
* mode is entirely implementation dependent.</p> |
|
612 |
* |
|
613 |
* @return this {@link Session} |
|
614 |
* @throws IllegalStateException if this {@link Session} is closed |
|
615 |
*/ |
|
616 |
public Session deactivate(); |
|
617 |
||
618 |
} |