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.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
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
88
89
90
91 public void setGrantee(final Grantee grantee) {
92 connectionContext.set(new ConnectionContext(databaseConnectionManager::getUsedDataSourceNames, grantee));
93 }
94
95
96
97
98
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
109
110
111
112 public ConnectionContext getConnectionContext() {
113 return connectionContext.get();
114 }
115
116
117
118
119
120
121 public String getUsedDatabaseName() {
122 return null == queryContext || queryContext.getUsedDatabaseNames().isEmpty() ? currentDatabaseName : queryContext.getUsedDatabaseNames().iterator().next();
123 }
124
125
126
127
128
129
130 public Optional<TransactionIsolationLevel> getIsolationLevel() {
131 return Optional.ofNullable(isolationLevel);
132 }
133
134
135
136
137 public void clearQueryContext() {
138 queryContext = null;
139 }
140 }