1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
88
89
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
99
100
101
102 public String getDatabaseName() {
103 return null == queryContext ? databaseName : queryContext.getDatabaseNameFromSQLStatement().orElse(databaseName);
104 }
105
106
107
108
109
110
111 public String getDefaultDatabaseName() {
112 return databaseName;
113 }
114
115
116
117
118 public void clearQueryContext() {
119 queryContext = null;
120 }
121 }