1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.engine; |
7 | |
8 | import java.util.ArrayList; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.result.ResultInterface; |
11 | import org.h2.util.New; |
12 | import org.h2.value.Value; |
13 | |
14 | /** |
15 | * The base class for both remote and embedded sessions. |
16 | */ |
17 | abstract class SessionWithState implements SessionInterface { |
18 | |
19 | protected ArrayList<String> sessionState; |
20 | protected boolean sessionStateChanged; |
21 | private boolean sessionStateUpdating; |
22 | |
23 | /** |
24 | * Re-create the session state using the stored sessionState list. |
25 | */ |
26 | protected void recreateSessionState() { |
27 | if (sessionState != null && sessionState.size() > 0) { |
28 | sessionStateUpdating = true; |
29 | try { |
30 | for (String sql : sessionState) { |
31 | CommandInterface ci = prepareCommand(sql, Integer.MAX_VALUE); |
32 | ci.executeUpdate(); |
33 | } |
34 | } finally { |
35 | sessionStateUpdating = false; |
36 | sessionStateChanged = false; |
37 | } |
38 | } |
39 | } |
40 | |
41 | /** |
42 | * Read the session state if necessary. |
43 | */ |
44 | public void readSessionState() { |
45 | if (!sessionStateChanged || sessionStateUpdating) { |
46 | return; |
47 | } |
48 | sessionStateChanged = false; |
49 | sessionState = New.arrayList(); |
50 | CommandInterface ci = prepareCommand( |
51 | "SELECT * FROM INFORMATION_SCHEMA.SESSION_STATE", |
52 | Integer.MAX_VALUE); |
53 | ResultInterface result = ci.executeQuery(0, false); |
54 | while (result.next()) { |
55 | Value[] row = result.currentRow(); |
56 | sessionState.add(row[1].getString()); |
57 | } |
58 | } |
59 | |
60 | } |