|
| 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.api.core.ApiFuture; |
| 20 | +import com.google.cloud.Timestamp; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +class DelayedTransactionManager implements TransactionManager { |
| 24 | + |
| 25 | + private final ApiFuture<TransactionManager> transactionManagerFuture; |
| 26 | + |
| 27 | + DelayedTransactionManager(ApiFuture<TransactionManager> transactionManagerFuture) { |
| 28 | + this.transactionManagerFuture = transactionManagerFuture; |
| 29 | + } |
| 30 | + |
| 31 | + TransactionManager getTransactionManager() { |
| 32 | + try { |
| 33 | + return this.transactionManagerFuture.get(); |
| 34 | + } catch (ExecutionException executionException) { |
| 35 | + // Propagate the underlying exception as a RuntimeException (SpannerException is also a |
| 36 | + // RuntimeException). |
| 37 | + if (executionException.getCause() instanceof RuntimeException) { |
| 38 | + throw (RuntimeException) executionException.getCause(); |
| 39 | + } |
| 40 | + throw SpannerExceptionFactory.asSpannerException(executionException.getCause()); |
| 41 | + } catch (InterruptedException interruptedException) { |
| 42 | + throw SpannerExceptionFactory.propagateInterrupt(interruptedException); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public TransactionContext begin() { |
| 48 | + return getTransactionManager().begin(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void commit() { |
| 53 | + getTransactionManager().commit(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void rollback() { |
| 58 | + getTransactionManager().rollback(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public TransactionContext resetForRetry() { |
| 63 | + return getTransactionManager().resetForRetry(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Timestamp getCommitTimestamp() { |
| 68 | + return getTransactionManager().getCommitTimestamp(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public CommitResponse getCommitResponse() { |
| 73 | + return getTransactionManager().getCommitResponse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public TransactionState getState() { |
| 78 | + return getTransactionManager().getState(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void close() { |
| 83 | + getTransactionManager().close(); |
| 84 | + } |
| 85 | +} |
0 commit comments