Skip to content

Method: close()

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions;
16:
17: import cz.cvut.kbss.jopa.accessors.DefaultStorageAccessor;
18: import cz.cvut.kbss.jopa.accessors.StorageAccessor;
19: import cz.cvut.kbss.jopa.model.AbstractEntityManager;
20: import cz.cvut.kbss.jopa.model.MetamodelImpl;
21: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
22: import cz.cvut.kbss.jopa.query.NamedQueryManager;
23: import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
24: import cz.cvut.kbss.jopa.sessions.cache.CacheFactory;
25: import cz.cvut.kbss.jopa.transactions.EntityTransaction;
26: import cz.cvut.kbss.jopa.utils.Configuration;
27: import cz.cvut.kbss.jopa.utils.Wrapper;
28: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
29: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
30:
31: import java.net.URI;
32: import java.util.Collections;
33: import java.util.Map;
34: import java.util.Objects;
35: import java.util.concurrent.ConcurrentHashMap;
36:
37: /**
38: * The ServerSession is the primary interface for accessing the ontology.
39: * <p>
40: * It manages an accessor object, which performs the queries.
41: */
42: public class ServerSession extends AbstractSession implements Wrapper {
43:
44: private final MetamodelImpl metamodel;
45:
46: private CacheManager liveObjectCache;
47: private StorageAccessor storageAccessor;
48:
49: private Map<EntityTransaction, AbstractEntityManager> runningTransactions;
50:
51: ServerSession() {
52: super(new Configuration(Collections.emptyMap()));
53: this.metamodel = null;
54: }
55:
56: public ServerSession(OntologyStorageProperties storageProperties, Configuration configuration,
57: MetamodelImpl metamodel) {
58: super(configuration);
59: this.metamodel = metamodel;
60: initialize(storageProperties, configuration, metamodel);
61: }
62:
63: /**
64: * Initializes this ServerSession. This in particular means initialization of the ontology accessor and live object
65: * cache.
66: *
67: * @param storageProperties Storage properties
68: * @param configuration Session configuration
69: * @param metamodel Metamodel of the managed classes and their attributes.
70: */
71: private void initialize(OntologyStorageProperties storageProperties, Configuration configuration,
72: Metamodel metamodel) {
73: assert configuration != null;
74: assert metamodel != null;
75: this.runningTransactions = new ConcurrentHashMap<>();
76: this.liveObjectCache = CacheFactory.createCache(configuration.getProperties());
77: liveObjectCache.setInferredClasses(metamodel.getInferredClasses());
78: this.storageAccessor = new DefaultStorageAccessor(storageProperties, configuration.getProperties());
79: }
80:
81: @Override
82: protected ConnectionWrapper acquireConnection() {
83: return new ConnectionWrapper(storageAccessor.acquireConnection());
84: }
85:
86: @Override
87: public CacheManager getLiveObjectCache() {
88: return liveObjectCache;
89: }
90:
91: public void transactionStarted(EntityTransaction t, AbstractEntityManager em) {
92: assert t.isActive();
93: runningTransactions.put(t, em);
94: }
95:
96: public void transactionFinished(EntityTransaction t) {
97: if (t == null) {
98: return;
99: }
100: runningTransactions.remove(t);
101: }
102:
103: /**
104: * Close the server session and all connections to the underlying data source.
105: */
106: public void close() {
107:• if (!runningTransactions.isEmpty()) {
108: LOG.warn("There are still transactions running. Marking them for rollback.");
109: runningTransactions.keySet().stream().filter(EntityTransaction::isActive)
110: .forEach(EntityTransaction::setRollbackOnly);
111: }
112:• if (storageAccessor != null && storageAccessor.isOpen()) {
113: try {
114: storageAccessor.close();
115: } catch (OntoDriverException e) {
116: LOG.error("Exception caught when closing the storage accessor.", e);
117: }
118: }
119: liveObjectCache.close();
120: }
121:
122: @Override
123: public void removeObjectFromCache(Object object, URI context) {
124: // do nothing
125: }
126:
127: @Override
128: public MetamodelImpl getMetamodel() {
129: return metamodel;
130: }
131:
132: @Override
133: public boolean isEntityType(Class<?> cls) {
134: return metamodel.isEntityType(cls);
135: }
136:
137: @Override
138: public NamedQueryManager getNamedQueryManager() {
139: return metamodel.getNamedQueryManager();
140: }
141:
142: @Override
143: public ResultSetMappingManager getResultSetMappingManager() {
144: return metamodel.getResultSetMappingManager();
145: }
146:
147: @Override
148: public <T> T unwrap(Class<T> cls) {
149: Objects.requireNonNull(cls);
150: if (cls.isAssignableFrom(getClass())) {
151: return cls.cast(this);
152: }
153: return storageAccessor.unwrap(cls);
154: }
155: }