Skip to content

Method: transactionFinished(EntityTransaction)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.HashMap;
34: import java.util.Map;
35: import java.util.Objects;
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 HashMap<>();
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: AbstractEntityManager em = runningTransactions.remove(t);
106:• if (em == null) {
107: return;
108: }
109: UnitOfWorkImpl uow = (UnitOfWorkImpl) em.getCurrentPersistenceContext();
110:• if (uow != null && uow.hasChanges()) {
111: getLiveObjectCache().clearInferredObjects();
112: }
113: }
114:
115: /**
116: * Close the server session and all connections to the underlying data source.
117: */
118: public void close() {
119: if (!runningTransactions.isEmpty()) {
120: LOG.warn("There are still transactions running. Marking them for rollback.");
121: runningTransactions.keySet().stream().filter(EntityTransaction::isActive)
122: .forEach(EntityTransaction::setRollbackOnly);
123: }
124: if (storageAccessor != null && storageAccessor.isOpen()) {
125: try {
126: storageAccessor.close();
127: } catch (OntoDriverException e) {
128: LOG.error("Exception caught when closing the storage accessor.", e);
129: }
130: }
131: liveObjectCache.close();
132: }
133:
134: @Override
135: public void removeObjectFromCache(Object object, URI context) {
136: // do nothing
137: }
138:
139: @Override
140: public MetamodelImpl getMetamodel() {
141: return metamodel;
142: }
143:
144: @Override
145: public boolean isEntityType(Class<?> cls) {
146: return metamodel.isEntityType(cls);
147: }
148:
149: @Override
150: public NamedQueryManager getNamedQueryManager() {
151: return metamodel.getNamedQueryManager();
152: }
153:
154: @Override
155: public ResultSetMappingManager getResultSetMappingManager() {
156: return metamodel.getResultSetMappingManager();
157: }
158:
159: @Override
160: public <T> T unwrap(Class<T> cls) {
161: Objects.requireNonNull(cls);
162: if (cls.isAssignableFrom(getClass())) {
163: return cls.cast(this);
164: }
165: return storageAccessor.unwrap(cls);
166: }
167: }