Skip to contentMethod: close()
1: /**
2: * Copyright (C) 2020 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 UnitOfWork acquireUnitOfWork() {
88: return new UnitOfWorkImpl(this);
89: }
90:
91: @Override
92: public CacheManager getLiveObjectCache() {
93: return liveObjectCache;
94: }
95:
96: public void transactionStarted(EntityTransaction t, AbstractEntityManager em) {
97: assert t.isActive();
98: runningTransactions.put(t, em);
99: }
100:
101: public void transactionFinished(EntityTransaction t) {
102: if (t == null) {
103: return;
104: }
105: runningTransactions.remove(t);
106: }
107:
108: /**
109: * Close the server session and all connections to the underlying data source.
110: */
111: public void close() {
112:• if (!runningTransactions.isEmpty()) {
113: LOG.warn("There are still transactions running. Marking them for rollback.");
114: runningTransactions.keySet().stream().filter(EntityTransaction::isActive)
115: .forEach(EntityTransaction::setRollbackOnly);
116: }
117:• if (storageAccessor != null && storageAccessor.isOpen()) {
118: try {
119: storageAccessor.close();
120: } catch (OntoDriverException e) {
121: LOG.error("Exception caught when closing the storage accessor.", e);
122: }
123: }
124: liveObjectCache.close();
125: }
126:
127: @Override
128: public void removeObjectFromCache(Object object, URI context) {
129: // do nothing
130: }
131:
132: @Override
133: public MetamodelImpl getMetamodel() {
134: return metamodel;
135: }
136:
137: @Override
138: public boolean isEntityType(Class<?> cls) {
139: return metamodel.isEntityType(cls);
140: }
141:
142: @Override
143: public NamedQueryManager getNamedQueryManager() {
144: return metamodel.getNamedQueryManager();
145: }
146:
147: @Override
148: public ResultSetMappingManager getResultSetMappingManager() {
149: return metamodel.getResultSetMappingManager();
150: }
151:
152: @Override
153: public <T> T unwrap(Class<T> cls) {
154: Objects.requireNonNull(cls);
155: if (cls.isAssignableFrom(getClass())) {
156: return cls.cast(this);
157: }
158: return storageAccessor.unwrap(cls);
159: }
160: }