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.session.query.QueryContext;
25  import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
26  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
27  import org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
28  import org.apache.shardingsphere.infra.metadata.user.Grantee;
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.sql.common.enums.TransactionIsolationLevel;
33  
34  /**
35   * Connection session.
36   */
37  @Getter
38  @Setter
39  public final class ConnectionSession {
40      
41      private final DatabaseType protocolType;
42      
43      @Setter(AccessLevel.NONE)
44      private volatile String databaseName;
45      
46      private volatile int connectionId;
47      
48      private Grantee grantee;
49      
50      private final TransactionStatus transactionStatus;
51      
52      private final AttributeMap attributeMap;
53      
54      private volatile boolean autoCommit = true;
55      
56      private volatile boolean readOnly;
57      
58      private TransactionIsolationLevel defaultIsolationLevel;
59      
60      private TransactionIsolationLevel isolationLevel;
61      
62      private final ProxyDatabaseConnectionManager databaseConnectionManager;
63      
64      @SuppressWarnings("rawtypes")
65      private final ExecutorStatementManager statementManager;
66      
67      private final ServerPreparedStatementRegistry serverPreparedStatementRegistry = new ServerPreparedStatementRegistry();
68      
69      private final ConnectionContext connectionContext;
70      
71      private final RequiredSessionVariableRecorder requiredSessionVariableRecorder = new RequiredSessionVariableRecorder();
72      
73      private volatile String processId;
74      
75      private QueryContext queryContext;
76      
77      public ConnectionSession(final DatabaseType protocolType, final AttributeMap attributeMap) {
78          this.protocolType = protocolType;
79          transactionStatus = new TransactionStatus();
80          this.attributeMap = attributeMap;
81          databaseConnectionManager = new ProxyDatabaseConnectionManager(this);
82          statementManager = new JDBCBackendStatement();
83          connectionContext = new ConnectionContext(databaseConnectionManager::getUsedDataSourceNames);
84      }
85      
86      /**
87       * Change database of current channel.
88       *
89       * @param databaseName database name
90       */
91      public void setCurrentDatabase(final String databaseName) {
92          if (null == databaseName || !databaseName.equals(this.databaseName)) {
93              this.databaseName = databaseName;
94          }
95      }
96      
97      /**
98       * Get database name.
99       *
100      * @return database name
101      */
102     public String getDatabaseName() {
103         return null == queryContext ? databaseName : queryContext.getDatabaseNameFromSQLStatement().orElse(databaseName);
104     }
105     
106     /**
107      * Get default database name.
108      *
109      * @return default database name
110      */
111     public String getDefaultDatabaseName() {
112         return databaseName;
113     }
114     
115     /**
116      * Clear query context.
117      */
118     public void clearQueryContext() {
119         queryContext = null;
120     }
121 }