| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.engine; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.command.CommandInterface; |
| 11 |
import org.h2.jdbc.meta.DatabaseMeta; |
| 12 |
import org.h2.message.Trace; |
| 13 |
import org.h2.result.ResultInterface; |
| 14 |
import org.h2.store.DataHandler; |
| 15 |
import org.h2.util.NetworkConnectionInfo; |
| 16 |
import org.h2.util.TimeZoneProvider; |
| 17 |
import org.h2.util.Utils; |
| 18 |
import org.h2.value.ValueLob; |
| 19 |
|
| 20 |
/** |
| 21 |
* A local or remote session. A session represents a database connection. |
| 22 |
*/ |
| 23 |
public abstract class Session implements CastDataProvider, AutoCloseable { |
| 24 |
|
| 25 |
/** |
| 26 |
* Static settings. |
| 27 |
*/ |
| 28 |
public static final class StaticSettings { |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether unquoted identifiers are converted to upper case. |
| 32 |
*/ |
| 33 |
public final boolean databaseToUpper; |
| 34 |
|
| 35 |
/** |
| 36 |
* Whether unquoted identifiers are converted to lower case. |
| 37 |
*/ |
| 38 |
public final boolean databaseToLower; |
| 39 |
|
| 40 |
/** |
| 41 |
* Whether all identifiers are case insensitive. |
| 42 |
*/ |
| 43 |
public final boolean caseInsensitiveIdentifiers; |
| 44 |
|
| 45 |
/** |
| 46 |
* Creates new instance of static settings. |
| 47 |
* |
| 48 |
* @param databaseToUpper |
| 49 |
* whether unquoted identifiers are converted to upper case |
| 50 |
* @param databaseToLower |
| 51 |
* whether unquoted identifiers are converted to lower case |
| 52 |
* @param caseInsensitiveIdentifiers |
| 53 |
* whether all identifiers are case insensitive |
| 54 |
*/ |
| 55 |
public StaticSettings(boolean databaseToUpper, boolean databaseToLower, boolean caseInsensitiveIdentifiers) { |
| 56 |
this.databaseToUpper = databaseToUpper; |
| 57 |
this.databaseToLower = databaseToLower; |
| 58 |
this.caseInsensitiveIdentifiers = caseInsensitiveIdentifiers; |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Dynamic settings. |
| 65 |
*/ |
| 66 |
public static final class DynamicSettings { |
| 67 |
|
| 68 |
/** |
| 69 |
* The database mode. |
| 70 |
*/ |
| 71 |
public final Mode mode; |
| 72 |
|
| 73 |
/** |
| 74 |
* The current time zone. |
| 75 |
*/ |
| 76 |
public final TimeZoneProvider timeZone; |
| 77 |
|
| 78 |
/** |
| 79 |
* Creates new instance of dynamic settings. |
| 80 |
* |
| 81 |
* @param mode |
| 82 |
* the database mode |
| 83 |
* @param timeZone |
| 84 |
* the current time zone |
| 85 |
*/ |
| 86 |
public DynamicSettings(Mode mode, TimeZoneProvider timeZone) { |
| 87 |
this.mode = mode; |
| 88 |
this.timeZone = timeZone; |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
private ArrayList<String> sessionState; |
| 94 |
|
| 95 |
boolean sessionStateChanged; |
| 96 |
|
| 97 |
private boolean sessionStateUpdating; |
| 98 |
|
| 99 |
volatile StaticSettings staticSettings; |
| 100 |
|
| 101 |
Session() { |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get the list of the cluster servers for this session. |
| 106 |
* |
| 107 |
* @return A list of "ip:port" strings for the cluster servers in this |
| 108 |
* session. |
| 109 |
*/ |
| 110 |
public abstract ArrayList<String> getClusterServers(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Parse a command and prepare it for execution. |
| 114 |
* |
| 115 |
* @param sql the SQL statement |
| 116 |
* @param fetchSize the number of rows to fetch in one step |
| 117 |
* @return the prepared command |
| 118 |
*/ |
| 119 |
public abstract CommandInterface prepareCommand(String sql, int fetchSize); |
| 120 |
|
| 121 |
/** |
| 122 |
* Roll back pending transactions and close the session. |
| 123 |
*/ |
| 124 |
@Override |
| 125 |
public abstract void close(); |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the trace object |
| 129 |
* |
| 130 |
* @return the trace object |
| 131 |
*/ |
| 132 |
public abstract Trace getTrace(); |
| 133 |
|
| 134 |
/** |
| 135 |
* Check if close was called. |
| 136 |
* |
| 137 |
* @return if the session has been closed |
| 138 |
*/ |
| 139 |
public abstract boolean isClosed(); |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the data handler object. |
| 143 |
* |
| 144 |
* @return the data handler |
| 145 |
*/ |
| 146 |
public abstract DataHandler getDataHandler(); |
| 147 |
|
| 148 |
/** |
| 149 |
* Check whether this session has a pending transaction. |
| 150 |
* |
| 151 |
* @return true if it has |
| 152 |
*/ |
| 153 |
public abstract boolean hasPendingTransaction(); |
| 154 |
|
| 155 |
/** |
| 156 |
* Cancel the current or next command (called when closing a connection). |
| 157 |
*/ |
| 158 |
public abstract void cancel(); |
| 159 |
|
| 160 |
/** |
| 161 |
* Check if this session is in auto-commit mode. |
| 162 |
* |
| 163 |
* @return true if the session is in auto-commit mode |
| 164 |
*/ |
| 165 |
public abstract boolean getAutoCommit(); |
| 166 |
|
| 167 |
/** |
| 168 |
* Set the auto-commit mode. This call doesn't commit the current |
| 169 |
* transaction. |
| 170 |
* |
| 171 |
* @param autoCommit the new value |
| 172 |
*/ |
| 173 |
public abstract void setAutoCommit(boolean autoCommit); |
| 174 |
|
| 175 |
/** |
| 176 |
* Add a temporary LOB, which is closed when the session commits. |
| 177 |
* |
| 178 |
* @param v the value |
| 179 |
* @return the specified value |
| 180 |
*/ |
| 181 |
public abstract ValueLob addTemporaryLob(ValueLob v); |
| 182 |
|
| 183 |
/** |
| 184 |
* Check if this session is remote or embedded. |
| 185 |
* |
| 186 |
* @return true if this session is remote |
| 187 |
*/ |
| 188 |
public abstract boolean isRemote(); |
| 189 |
|
| 190 |
/** |
| 191 |
* Set current schema. |
| 192 |
* |
| 193 |
* @param schema the schema name |
| 194 |
*/ |
| 195 |
public abstract void setCurrentSchemaName(String schema); |
| 196 |
|
| 197 |
/** |
| 198 |
* Get current schema. |
| 199 |
* |
| 200 |
* @return the current schema name |
| 201 |
*/ |
| 202 |
public abstract String getCurrentSchemaName(); |
| 203 |
|
| 204 |
/** |
| 205 |
* Sets the network connection information if possible. |
| 206 |
* |
| 207 |
* @param networkConnectionInfo the network connection information |
| 208 |
*/ |
| 209 |
public abstract void setNetworkConnectionInfo(NetworkConnectionInfo networkConnectionInfo); |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns the isolation level. |
| 213 |
* |
| 214 |
* @return the isolation level |
| 215 |
*/ |
| 216 |
public abstract IsolationLevel getIsolationLevel(); |
| 217 |
|
| 218 |
/** |
| 219 |
* Sets the isolation level. |
| 220 |
* |
| 221 |
* @param isolationLevel the isolation level to set |
| 222 |
*/ |
| 223 |
public abstract void setIsolationLevel(IsolationLevel isolationLevel); |
| 224 |
|
| 225 |
/** |
| 226 |
* Returns static settings. These settings cannot be changed during |
| 227 |
* lifecycle of session. |
| 228 |
* |
| 229 |
* @return static settings |
| 230 |
*/ |
| 231 |
public abstract StaticSettings getStaticSettings(); |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns dynamic settings. These settings can be changed during lifecycle |
| 235 |
* of session. |
| 236 |
* |
| 237 |
* @return dynamic settings |
| 238 |
*/ |
| 239 |
public abstract DynamicSettings getDynamicSettings(); |
| 240 |
|
| 241 |
/** |
| 242 |
* Returns database meta information. |
| 243 |
* |
| 244 |
* @return database meta information |
| 245 |
*/ |
| 246 |
public abstract DatabaseMeta getDatabaseMeta(); |
| 247 |
|
| 248 |
/** |
| 249 |
* Returns whether INFORMATION_SCHEMA contains old-style tables. |
| 250 |
* |
| 251 |
* @return whether INFORMATION_SCHEMA contains old-style tables |
| 252 |
*/ |
| 253 |
public abstract boolean isOldInformationSchema(); |
| 254 |
|
| 255 |
/** |
| 256 |
* Re-create the session state using the stored sessionState list. |
| 257 |
*/ |
| 258 |
void recreateSessionState() { |
| 259 |
if (sessionState != null && !sessionState.isEmpty()) { |
| 260 |
sessionStateUpdating = true; |
| 261 |
try { |
| 262 |
for (String sql : sessionState) { |
| 263 |
CommandInterface ci = prepareCommand(sql, Integer.MAX_VALUE); |
| 264 |
ci.executeUpdate(null); |
| 265 |
} |
| 266 |
} finally { |
| 267 |
sessionStateUpdating = false; |
| 268 |
sessionStateChanged = false; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Read the session state if necessary. |
| 275 |
*/ |
| 276 |
public void readSessionState() { |
| 277 |
if (!sessionStateChanged || sessionStateUpdating) { |
| 278 |
return; |
| 279 |
} |
| 280 |
sessionStateChanged = false; |
| 281 |
sessionState = Utils.newSmallArrayList(); |
| 282 |
CommandInterface ci = prepareCommand(!isOldInformationSchema() |
| 283 |
? "SELECT STATE_COMMAND FROM INFORMATION_SCHEMA.SESSION_STATE" |
| 284 |
: "SELECT SQL FROM INFORMATION_SCHEMA.SESSION_STATE", Integer.MAX_VALUE); |
| 285 |
ResultInterface result = ci.executeQuery(0, false); |
| 286 |
while (result.next()) { |
| 287 |
sessionState.add(result.currentRow()[0].getString()); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Sets this session as thread local session, if this session is a local |
| 293 |
* session. |
| 294 |
* |
| 295 |
* @return old thread local session, or {@code null} |
| 296 |
*/ |
| 297 |
public Session setThreadLocalSession() { |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Resets old thread local session. |
| 303 |
* |
| 304 |
* @param oldSession |
| 305 |
* the old thread local session, or {@code null} |
| 306 |
*/ |
| 307 |
public void resetThreadLocalSession(Session oldSession) { |
| 308 |
} |
| 309 |
|
| 310 |
} |