|
5 | 5 | package com.github.sttk.sabi; |
6 | 6 |
|
7 | 7 | import com.github.sttk.errs.Err; |
| 8 | +import java.util.List; |
8 | 9 |
|
9 | 10 | /** |
10 | | - * The interface that abstracts a connection per session to an external data service, such as a |
11 | | - * database, file system, or messaging service. |
| 11 | + * Represents a data connection participating in transaction lifecycles managed by a {@link |
| 12 | + * DataHub}. |
12 | 13 | * |
13 | | - * <p>Its primary purpose is to enable cohesive transaction operations across multiple external data |
14 | | - * services within a single transaction context. Implementations of this interface provide the |
15 | | - * concrete input/output operations for their respective data services. |
16 | | - * |
17 | | - * <p>Methods declared within this interface are designed to handle transactional logic. The |
18 | | - * AsyncGroup parameter in various methods allows for asynchronous processing when commit or |
19 | | - * rollback operations are time-consuming. |
| 14 | + * <p>Implementations encapsulate connection state and operations for external resources such as |
| 15 | + * relational databases, NoSQL stores, or web services. During a transaction, connections progress |
| 16 | + * through pre-commit, commit, and post-commit phases, and support rollback and failure reporting |
| 17 | + * when errors occur. |
20 | 18 | */ |
21 | 19 | public interface DataConn { |
| 20 | + |
22 | 21 | /** |
23 | | - * Commits the changes made within the current session to the external data service. This method |
24 | | - * is responsible for finalizing all operations performed since the last commit or rollback. |
| 22 | + * Represents an error when one or more data connections fail during the pre-commit phase. |
25 | 23 | * |
26 | | - * @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the |
27 | | - * commit process is time-consuming. |
28 | | - * @throws Err if an error occurs during the commit operation. |
| 24 | + * @param errors the list of error entries detailing connection names and failure causes |
29 | 25 | */ |
30 | | - void commit(AsyncGroup ag) throws Err; |
| 26 | + public record FailToPreCommitDataConn(List<ErrEntry> errors) {} |
31 | 27 |
|
32 | 28 | /** |
33 | | - * Performs any necessary pre-commit operations. This method is called before the {@link |
34 | | - * #commit(AsyncGroup)} method. |
| 29 | + * Represents an error when one or more data connections fail during the commit phase. |
35 | 30 | * |
36 | | - * @param ag An {@link AsyncGroup} that can be used for asynchronous pre-commit tasks. |
37 | | - * @throws Err if an error occurs during the pre-commit operation. |
| 31 | + * @param errors the list of error entries detailing connection names and failure causes |
38 | 32 | */ |
39 | | - default void preCommit(AsyncGroup ag) throws Err {} |
| 33 | + public record FailToCommitDataConn(List<ErrEntry> errors) {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Represents an error when one or more data connections fail during the post-commit phase. |
| 37 | + * |
| 38 | + * @param errors the list of error entries detailing connection names and failure causes |
| 39 | + */ |
| 40 | + public record FailToPostCommitDataConn(List<ErrEntry> errors) {} |
| 41 | + |
| 42 | + /// |
| 43 | + |
| 44 | + /** |
| 45 | + * Commits changes made through this connection. |
| 46 | + * |
| 47 | + * <p>Async background tasks may be registered to the provided {@link AsyncGroup} during commit |
| 48 | + * processing. |
| 49 | + * |
| 50 | + * @param ag the asynchronous group for registering background tasks |
| 51 | + * @throws Err if committing the transaction changes fails |
| 52 | + */ |
| 53 | + void commit(AsyncGroup ag) throws Err; |
40 | 54 |
|
41 | 55 | /** |
42 | | - * Performs any necessary post-commit operations. This method is called after the {@link |
43 | | - * #commit(AsyncGroup)} method has successfully completed. |
| 56 | + * Prepares this connection for commit prior to the main commit phase. |
| 57 | + * |
| 58 | + * <p>The default implementation does nothing. Subclasses can override this method to perform |
| 59 | + * pre-commit checks or flush pending writes. |
44 | 60 | * |
45 | | - * @param ag An {@link AsyncGroup} that can be used for asynchronous post-commit tasks. |
| 61 | + * @param ag the asynchronous group for registering background tasks |
| 62 | + * @throws Err if pre-commit preparation or validation fails |
46 | 63 | */ |
47 | | - default void postCommit(AsyncGroup ag) {} |
| 64 | + default void preCommit(AsyncGroup ag) throws Err {} |
48 | 65 |
|
49 | 66 | /** |
50 | | - * Indicates whether a force-back operation should be performed. A force-back is a mechanism to |
51 | | - * revert the committed changes when this connection had been already committed but the other |
52 | | - * connection had failed. |
| 67 | + * Performs post-commit operations after all connections in a transaction have successfully |
| 68 | + * committed. |
53 | 69 | * |
54 | | - * @return {@code true} if a force-back is required, {@code false} otherwise. |
| 70 | + * <p>The default implementation does nothing. Subclasses can override this method to perform |
| 71 | + * cleanup or trigger post-commit notifications. |
| 72 | + * |
| 73 | + * @param ag the asynchronous group for registering background tasks |
| 74 | + * @throws Err if post-commit processing fails |
55 | 75 | */ |
56 | | - default boolean shouldForceBack() { |
57 | | - return false; |
58 | | - } |
| 76 | + default void postCommit(AsyncGroup ag) throws Err {} |
59 | 77 |
|
60 | 78 | /** |
61 | | - * Rolls back the changes made within the current session, discarding all operations performed |
62 | | - * since the last commit or rollback. |
| 79 | + * Checks whether this connection has been successfully committed. |
63 | 80 | * |
64 | | - * @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the |
65 | | - * rollback process is time-consuming. |
| 81 | + * @return {@code true} if this connection was committed; {@code false} otherwise |
66 | 82 | */ |
67 | | - void rollback(AsyncGroup ag); |
| 83 | + boolean isCommitted(); |
68 | 84 |
|
69 | 85 | /** |
70 | | - * Performs a force-back operation to revert the committed changes when this connection had been |
71 | | - * already committed but the other connection had failed. |
| 86 | + * Rolls back changes made through this connection. |
| 87 | + * |
| 88 | + * <p>This method is invoked when a transaction fails and must restore the connection or |
| 89 | + * underlying storage to its pre-transaction state. |
72 | 90 | * |
73 | | - * @param ag An {@link AsyncGroup} that can be used for asynchronous force-back tasks. |
| 91 | + * @param ag the asynchronous group for registering background tasks |
| 92 | + * @throws Err if rolling back connection changes fails |
74 | 93 | */ |
75 | | - default void forceBack(AsyncGroup ag) {} |
| 94 | + void rollback(AsyncGroup ag) throws Err; |
76 | 95 |
|
77 | 96 | /** |
78 | | - * Closes the connection to the external data service, releasing any associated resources. This |
79 | | - * method should be called to ensure proper resource management. |
| 97 | + * Notifies this connection of a transaction failure with detailed failure reports. |
| 98 | + * |
| 99 | + * <p>The default implementation does nothing. Implementations can inspect failure reports to log |
| 100 | + * diagnostics or take recovery actions. |
| 101 | + * |
| 102 | + * @param ag the asynchronous group for registering background tasks |
| 103 | + * @param reports the list of transaction failure reports |
80 | 104 | */ |
| 105 | + default void onTxnFailure(AsyncGroup ag, List<TxnFailureReport> reports) {} |
| 106 | + |
| 107 | + /** Closes and disposes of this data connection, releasing any held resources. */ |
81 | 108 | void close(); |
82 | 109 | } |
0 commit comments