|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.connection.Connection; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +/** |
| 24 | + * Exception thrown by a {@link Connection} when an automatic DML batch detects that one or more of |
| 25 | + * the update counts that it returned during the buffering of DML statements does not match with the |
| 26 | + * actual update counts that were returned after execution. |
| 27 | + */ |
| 28 | +public class DmlBatchUpdateCountVerificationFailedException extends AbortedException { |
| 29 | + private static final long serialVersionUID = 1L; |
| 30 | + |
| 31 | + private final long[] expected; |
| 32 | + |
| 33 | + private final long[] actual; |
| 34 | + |
| 35 | + /** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */ |
| 36 | + DmlBatchUpdateCountVerificationFailedException( |
| 37 | + DoNotConstructDirectly token, long[] expected, long[] actual) { |
| 38 | + super( |
| 39 | + token, |
| 40 | + String.format( |
| 41 | + "Actual update counts that were returned during execution do not match the previously returned update counts.\n" |
| 42 | + + "Expected: %s\n" |
| 43 | + + "Actual: %s\n" |
| 44 | + + "Set auto_batch_dml_update_count_verification to false to skip this verification.", |
| 45 | + Arrays.stream(expected).mapToObj(Long::toString).collect(Collectors.joining()), |
| 46 | + Arrays.stream(actual).mapToObj(Long::toString).collect(Collectors.joining())), |
| 47 | + /* cause = */ null); |
| 48 | + this.expected = expected; |
| 49 | + this.actual = actual; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The expected update counts. These were returned to the client application when the DML |
| 54 | + * statements were buffered. |
| 55 | + */ |
| 56 | + public long[] getExpected() { |
| 57 | + return Arrays.copyOf(this.expected, this.expected.length); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * The actual update counts. These were returned by Spanner to the client when the DML statements |
| 62 | + * were actually executed, and are the update counts that the client application would have |
| 63 | + * received if auto-batching had not been enabled. |
| 64 | + */ |
| 65 | + public long[] getActual() { |
| 66 | + return Arrays.copyOf(this.actual, this.actual.length); |
| 67 | + } |
| 68 | +} |
0 commit comments