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.proxy.backend.session;
19  
20  import io.netty.util.AttributeMap;
21  import lombok.AccessLevel;
22  import lombok.Getter;
23  import lombok.Setter;
24  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
25  import org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
26  import org.apache.shardingsphere.infra.metadata.user.Grantee;
27  import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
28  import org.apache.shardingsphere.infra.session.query.QueryContext;
29  import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
30  import org.apache.shardingsphere.proxy.backend.connector.jdbc.statement.JDBCBackendStatement;
31  import org.apache.shardingsphere.proxy.backend.session.transaction.TransactionStatus;
32  import org.apache.shardingsphere.sql.parser.statement.core.enums.TransactionIsolationLevel;
33  
34  import java.util.Optional;
35  import java.util.concurrent.atomic.AtomicReference;
36  
37  /**
38   * Connection session.
39   */
40  @Getter
41  @Setter
42  public final class ConnectionSession {
43      
44      private final DatabaseType protocolType;
45      
46      @Setter(AccessLevel.NONE)
47      private volatile String currentDatabaseName;
48      
49      private volatile int connectionId;
50      
51      private final TransactionStatus transactionStatus;
52      
53      private final AttributeMap attributeMap;
54      
55      private volatile boolean autoCommit = true;
56      
57      private volatile boolean readOnly;
58      
59      private TransactionIsolationLevel defaultIsolationLevel;
60      
61      private TransactionIsolationLevel isolationLevel;
62      
63      private final ProxyDatabaseConnectionManager databaseConnectionManager;
64      
65      @SuppressWarnings("rawtypes")
66      private final ExecutorStatementManager statementManager;
67      
68      private final ServerPreparedStatementRegistry serverPreparedStatementRegistry = new ServerPreparedStatementRegistry();
69      
70      private final AtomicReference<ConnectionContext> connectionContext = new AtomicReference<>();
71      
72      private final RequiredSessionVariableRecorder requiredSessionVariableRecorder = new RequiredSessionVariableRecorder();
73      
74      private volatile String processId;
75      
76      private QueryContext queryContext;
77      
78      public ConnectionSession(final DatabaseType protocolType, final AttributeMap attributeMap) {
79          this.protocolType = protocolType;
80          transactionStatus = new TransactionStatus();
81          this.attributeMap = attributeMap;
82          databaseConnectionManager = new ProxyDatabaseConnectionManager(this);
83          statementManager = new JDBCBackendStatement();
84      }
85      
86      /**
87       * Set grantee.
88       *
89       * @param grantee grantee
90       */
91      public void setGrantee(final Grantee grantee) {
92          connectionContext.set(new ConnectionContext(databaseConnectionManager::getUsedDataSourceNames, grantee));
93      }
94      
95      /**
96       * Change database of current channel.
97       *
98       * @param currentDatabaseName current database name
99       */
100     public void setCurrentDatabaseName(final String currentDatabaseName) {
101         if (null == currentDatabaseName || !currentDatabaseName.equals(this.currentDatabaseName)) {
102             this.currentDatabaseName = currentDatabaseName;
103             connectionContext.get().setCurrentDatabaseName(currentDatabaseName);
104         }
105     }
106     
107     /**
108      * Get connection context.
109      *
110      * @return connection context
111      */
112     public ConnectionContext getConnectionContext() {
113         return connectionContext.get();
114     }
115     
116     /**
117      * Get used database name.
118      *
119      * @return used database name
120      */
121     public String getUsedDatabaseName() {
122         return null == queryContext || queryContext.getUsedDatabaseNames().isEmpty() ? currentDatabaseName : queryContext.getUsedDatabaseNames().iterator().next();
123     }
124     
125     /**
126      * Get isolation level.
127      *
128      * @return isolation level
129      */
130     public Optional<TransactionIsolationLevel> getIsolationLevel() {
131         return Optional.ofNullable(isolationLevel);
132     }
133     
134     /**
135      * Clear query context.
136      */
137     public void clearQueryContext() {
138         queryContext = null;
139     }
140 }