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