View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.session.connection.transaction;
19  
20  import lombok.Getter;
21  import lombok.Setter;
22  
23  import java.util.Optional;
24  import java.util.concurrent.atomic.AtomicReference;
25  
26  /**
27   * Transaction connection context.
28   */
29  @Getter
30  public final class TransactionConnectionContext implements AutoCloseable {
31      
32      private volatile String transactionType;
33      
34      private volatile boolean inTransaction;
35      
36      @Setter
37      private volatile long beginMillis;
38      
39      @Setter
40      private volatile boolean exceptionOccur;
41      
42      @Setter
43      private volatile String readWriteSplitReplicaRoute;
44      
45      private AtomicReference<TransactionManager> transactionManager;
46      
47      /**
48       * Begin transaction.
49       *
50       * @param transactionType transaction type
51       * @param transactionManager transaction manager
52       */
53      public void beginTransaction(final String transactionType, final TransactionManager transactionManager) {
54          this.transactionType = transactionType;
55          inTransaction = true;
56          this.transactionManager = new AtomicReference<>(transactionManager);
57      }
58      
59      /**
60       * Judge is in distributed transaction or not.
61       *
62       * @return in distributed transaction or not
63       */
64      public boolean isInDistributedTransaction() {
65          return inTransaction && ("XA".equals(transactionType) || "BASE".equals(transactionType));
66      }
67      
68      /**
69       * Get transaction type. 
70       *
71       * @return transaction type
72       */
73      public Optional<String> getTransactionType() {
74          return Optional.ofNullable(transactionType);
75      }
76      
77      /**
78       * Get read write split replica route. 
79       *
80       * @return read write split replica route
81       */
82      public Optional<String> getReadWriteSplitReplicaRoute() {
83          return Optional.ofNullable(readWriteSplitReplicaRoute);
84      }
85      
86      /**
87       * Get transaction manager.
88       *
89       * @return transaction manager
90       */
91      public Optional<TransactionManager> getTransactionManager() {
92          return null == transactionManager ? Optional.empty() : Optional.ofNullable(transactionManager.get());
93      }
94      
95      @Override
96      public void close() {
97          transactionType = null;
98          inTransaction = false;
99          beginMillis = 0L;
100         exceptionOccur = false;
101         readWriteSplitReplicaRoute = null;
102         transactionManager = null;
103     }
104 }