1 package fi.vtt.simantics.procore.internal;
3 import java.util.concurrent.CopyOnWriteArrayList;
5 import org.simantics.db.SessionManager;
6 import org.simantics.db.SessionReference;
7 import org.simantics.db.common.TransactionPolicyRelease;
8 import org.simantics.db.common.utils.Logger;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.exception.InternalException;
11 import org.simantics.db.exception.TimeoutException;
12 import org.simantics.db.impl.query.QueryProcessor;
13 import org.simantics.db.service.LifecycleSupport;
14 import org.simantics.db.service.TransactionPolicySupport;
16 public class LifecycleSupportImpl implements LifecycleSupport {
18 final private SessionImplSocket session;
19 final private CopyOnWriteArrayList<LifecycleListener> listeners = new CopyOnWriteArrayList<LifecycleListener>();
21 LifecycleSupportImpl(SessionImplSocket session) {
22 this.session = session;
27 return session.graphSession.getSessionId();
31 public boolean isClosing() {
32 return session.state.isClosing();
36 public boolean isClosed() {
37 return session.state.isClosed();
41 final public SessionReference getSessionReference() {
42 if (session.graphSession == null)
44 return session.graphSession.getSessionReference();
48 final public SessionManager getSessionManager() {
49 return session.sessionManagerImpl;
53 public void ping() throws DatabaseException {
54 session.graphSession.execute("");
59 throws DatabaseException {
60 GraphSession graphSession = session.graphSession;
63 } catch (TimeoutException e) {
66 if (null != graphSession)
68 // Graph session expects start and stop.
69 // If we miss stop then server will not know that we disconnected deliberately.
71 } catch (InternalException e) {
72 Logger.defaultLogError("GraphSession.stop failed.", e);
77 final public void close(long timeout, boolean force)
78 throws DatabaseException {
79 session.registerService(TransactionPolicySupport.class, new TransactionPolicyRelease());
80 State.WaitStatus status = session.state.waitClosing(timeout);
87 throw new TimeoutException("Close timeout, use force!");
88 // Intentionally dropping to next case.
91 QueryProcessor prosessor = session.getQueryProvider2();
92 if (prosessor!=null) prosessor.dispose();
93 session.virtualGraphServerSupport.disposeVirtualGraphs();
94 session.removeTemporaryData();
95 // Release the session from the manager.
96 session.graphSession.close();
97 session.serviceLocator.dispose(); // This will close local sessions created when registering services.
99 session.state.close();
100 session.clusterTable.dispose(); session.clusterTable = null;
101 session.sessionManagerImpl.shutdown(session, null);
102 session.sessionManagerImpl = null;
104 session.graphSession = null;
108 throw new DatabaseException();
113 public void addListener(LifecycleListener listener) {
114 listeners.add(listener);
118 public void removeListener(LifecycleListener listener) {
119 listeners.remove(listener);
122 void fireListeners(LifecycleState newState) {
123 for(LifecycleListener listener : listeners) listener.stateChanged(newState);