-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTransactionOptions.java
More file actions
333 lines (305 loc) · 12.1 KB
/
TransactionOptions.java
File metadata and controls
333 lines (305 loc) · 12.1 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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.
*/
package com.mongodb;
import com.mongodb.annotations.Alpha;
import com.mongodb.annotations.Immutable;
import com.mongodb.annotations.Reason;
import com.mongodb.lang.Nullable;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.TimeoutSettings.convertAndValidateTimeoutNullable;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
/**
* Options to apply to transactions. The default values for the options depend on context. For options specified per-transaction, the
* default values come from the default transaction options. For the default transaction options themselves, the default values come from
* the MongoClient on which the session was started.
*
* @see com.mongodb.session.ClientSession
* @see ClientSessionOptions
* @since 3.8
* @mongodb.server.release 4.0
*/
@Immutable
public final class TransactionOptions {
private final ReadConcern readConcern;
private final WriteConcern writeConcern;
private final ReadPreference readPreference;
private final Long maxCommitTimeMS;
private final Long timeoutMS;
/**
* Gets the read concern.
*
* @return the read concern
*/
@Nullable
public ReadConcern getReadConcern() {
return readConcern;
}
/**
* Gets the write concern.
*
* @return the write concern
*/
@Nullable
public WriteConcern getWriteConcern() {
return writeConcern;
}
/**
* Gets the read preference.
*
* @return the write concern
*/
@Nullable
public ReadPreference getReadPreference() {
return readPreference;
}
/**
* Gets the maximum amount of time to allow a single commitTransaction command to execute. The default is null, which places no
* limit on the execution time.
*
* @param timeUnit the time unit to return the result in
* @return the maximum execution time in the given time unit
* @mongodb.server.release 4.2
* @since 3.11
*/
@Nullable
public Long getMaxCommitTime(final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
if (maxCommitTimeMS == null) {
return null;
}
return timeUnit.convert(maxCommitTimeMS, MILLISECONDS);
}
/**
* The time limit for the full execution of the transaction.
*
* <p>If set the following deprecated options will be ignored:
* {@code waitQueueTimeoutMS}, {@code socketTimeoutMS}, {@code wTimeoutMS}, {@code maxTimeMS} and {@code maxCommitTimeMS}</p>
*
* <ul>
* <li>{@code null} means that the timeout mechanism for operations will defer to using
* {@link ClientSessionOptions#getDefaultTimeout(TimeUnit)} or {@link MongoClientSettings#getTimeout(TimeUnit)}
* </li>
* <li>{@code 0} means infinite timeout.</li>
* <li>{@code > 0} The time limit to use for the full execution of an operation.</li>
* </ul>
*
* <p>Note: When using synchronous API, this timeout does not limit socket writes, therefore there is a possibility that the
* operation might not be timed out when expected. This limitation does not apply to the reactive streams API.
*
* @param timeUnit the time unit
* @return the timeout in the given time unit
* @since 5.2
*/
@Nullable
@Alpha(Reason.CLIENT)
public Long getTimeout(final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
if (timeoutMS == null) {
return null;
}
return timeUnit.convert(timeoutMS, MILLISECONDS);
}
/**
* Gets an instance of a builder
*
* @return a builder instance
*/
public static Builder builder() {
return new Builder();
}
/**
* Merge the two provided transaction options, with the first taking precedence over the second.
*
* @param options the transaction options, which take precedence for any property that is non-null
* @param defaultOptions the default transaction options
* @return the merged transaction options
*/
public static TransactionOptions merge(final TransactionOptions options, final TransactionOptions defaultOptions) {
notNull("options", options);
notNull("defaultOptions", defaultOptions);
return TransactionOptions.builder()
.writeConcern(options.getWriteConcern() == null
? defaultOptions.getWriteConcern() : options.getWriteConcern())
.readConcern(options.getReadConcern() == null
? defaultOptions.getReadConcern() : options.getReadConcern())
.readPreference(options.getReadPreference() == null
? defaultOptions.getReadPreference() : options.getReadPreference())
.maxCommitTime(options.getMaxCommitTime(MILLISECONDS) == null
? defaultOptions.getMaxCommitTime(MILLISECONDS) : options.getMaxCommitTime(MILLISECONDS),
MILLISECONDS)
.timeout(options.getTimeout(MILLISECONDS) == null
? defaultOptions.getTimeout(MILLISECONDS) : options.getTimeout(MILLISECONDS),
MILLISECONDS)
.build();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TransactionOptions that = (TransactionOptions) o;
if (!Objects.equals(timeoutMS, that.timeoutMS)) {
return false;
}
if (!Objects.equals(maxCommitTimeMS, that.maxCommitTimeMS)) {
return false;
}
if (!Objects.equals(readConcern, that.readConcern)) {
return false;
}
if (!Objects.equals(writeConcern, that.writeConcern)) {
return false;
}
if (!Objects.equals(readPreference, that.readPreference)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = readConcern != null ? readConcern.hashCode() : 0;
result = 31 * result + (writeConcern != null ? writeConcern.hashCode() : 0);
result = 31 * result + (readPreference != null ? readPreference.hashCode() : 0);
result = 31 * result + (maxCommitTimeMS != null ? maxCommitTimeMS.hashCode() : 0);
result = 31 * result + (timeoutMS != null ? timeoutMS.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "TransactionOptions{"
+ "readConcern=" + readConcern
+ ", writeConcern=" + writeConcern
+ ", readPreference=" + readPreference
+ ", maxCommitTimeMS=" + maxCommitTimeMS
+ ", timeoutMS=" + timeoutMS
+ '}';
}
/**
* The builder for transaction options
*/
public static final class Builder {
private ReadConcern readConcern;
private WriteConcern writeConcern;
private ReadPreference readPreference;
private Long maxCommitTimeMS;
@Nullable
private Long timeoutMS;
/**
* Sets the read concern.
*
* @param readConcern the read concern
* @return this
*/
public Builder readConcern(@Nullable final ReadConcern readConcern) {
this.readConcern = readConcern;
return this;
}
/**
* Sets the write concern.
*
* @param writeConcern the write concern, which must be acknowledged
* @return this
*/
public Builder writeConcern(@Nullable final WriteConcern writeConcern) {
this.writeConcern = writeConcern;
return this;
}
/**
* Sets the read preference.
*
* @param readPreference the read preference, which currently must be primary. This restriction may be relaxed in future versions.
* @return this
*/
public Builder readPreference(@Nullable final ReadPreference readPreference) {
this.readPreference = readPreference;
return this;
}
/**
* Sets the maximum execution time on the server for the commitTransaction operation.
*
* @param maxCommitTime the max commit time, which must be either null or greater than zero, in the given time unit
* @param timeUnit the time unit, which may not be null
* @return this
* @mongodb.server.release 4.2
* @since 3.11
*/
public Builder maxCommitTime(@Nullable final Long maxCommitTime, final TimeUnit timeUnit) {
if (maxCommitTime == null) {
this.maxCommitTimeMS = null;
} else {
notNull("timeUnit", timeUnit);
isTrueArgument("maxCommitTime > 0", maxCommitTime > 0);
this.maxCommitTimeMS = MILLISECONDS.convert(maxCommitTime, timeUnit);
}
return this;
}
/**
* Sets the time limit for the full execution of the operations for this transaction.
*
* <ul>
* <li>{@code null} means that the timeout mechanism for operations will defer to using:
* <ul>
* <li>{@code waitQueueTimeoutMS}: The maximum wait time in milliseconds that a thread may wait for a connection to become
* available</li>
* <li>{@code socketTimeoutMS}: How long a send or receive on a socket can take before timing out.</li>
* <li>{@code wTimeoutMS}: How long the server will wait for the write concern to be fulfilled before timing out.</li>
* <li>{@code maxTimeMS}: The cumulative time limit for processing operations on a cursor.
* See: <a href="https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS">cursor.maxTimeMS</a>.</li>
* <li>{@code maxCommitTimeMS}: The maximum amount of time to allow a single {@code commitTransaction} command to execute.</li>
* </ul>
* </li>
* <li>{@code 0} means infinite timeout.</li>
* <li>{@code > 0} The time limit to use for the full execution of an operation.</li>
* </ul>
*
* <p>Note: When using synchronous API, this timeout does not limit socket writes, therefore there is a possibility that the
* operation might not be timed out when expected. This limitation does not apply to the reactive streams API.
*
* @param timeout the timeout
* @param timeUnit the time unit
* @return this
* @since 5.2
*/
@Alpha(Reason.CLIENT)
public Builder timeout(@Nullable final Long timeout, final TimeUnit timeUnit) {
this.timeoutMS = convertAndValidateTimeoutNullable(timeout, timeUnit);
return this;
}
/**
* Build the transaction options instance.
*
* @return The {@code TransactionOptions}
*/
public TransactionOptions build() {
return new TransactionOptions(this);
}
private Builder() {
}
}
private TransactionOptions(final Builder builder) {
readConcern = builder.readConcern;
writeConcern = builder.writeConcern;
readPreference = builder.readPreference;
maxCommitTimeMS = builder.maxCommitTimeMS;
timeoutMS = builder.timeoutMS;
}
}