/
HibernateUtil.java
254 lines (220 loc) · 9.96 KB
/
HibernateUtil.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.artificer.repository.hibernate;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.io.IOUtils;
import org.artificer.common.ArtificerConfig;
import org.artificer.common.ArtificerException;
import org.artificer.common.error.ArtificerNotFoundException;
import org.artificer.common.error.ArtificerServerException;
import org.artificer.common.ontology.ArtificerOntology;
import org.artificer.repository.hibernate.entity.ArtificerArtifact;
import org.artificer.repository.hibernate.entity.ArtificerStoredQuery;
import org.hibernate.Hibernate;
import org.hibernate.ejb.HibernatePersistence;
import org.hibernate.engine.spi.SessionImplementor;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import java.util.Map;
/**
* @author Brett Meyer.
*/
public class HibernateUtil {
private static String persistenceUnit = "Artificer";
private static EntityManagerFactory entityManagerFactory = null;
/**
* A worker pattern, used for *all* integration with the EntityManager. This should be the only public means to use it.
* @param <T>
*/
public static abstract class HibernateTask<T> {
public T execute() throws ArtificerException {
EntityManager entityManager = null;
try {
entityManager = entityManager();
entityManager.getTransaction().begin();
T rtn = doExecute(entityManager);
entityManager.getTransaction().commit();
return rtn;
} catch (ArtificerException ae) {
if (entityManager != null) {
try {
entityManager.getTransaction().rollback();
} catch (Throwable t1) {
// eat it
}
}
throw ae;
} catch (Throwable t) {
if (entityManager != null) {
try {
entityManager.getTransaction().rollback();
} catch (Throwable t1) {
// eat it
}
}
throw new ArtificerServerException(t);
} finally {
if (entityManager != null) {
entityManager.close();
}
}
}
protected abstract T doExecute(EntityManager entityManager) throws Exception;
}
private static EntityManager entityManager() throws Exception {
if (entityManagerFactory == null) {
// Pass in all hibernate.* settings from artificer.properties
Map<String, Object> properties = ArtificerConfig.getConfigProperties("hibernate");
if (properties.containsKey("hibernate.connection.url")) {
// If a connection is used, we *cannot* rely on Hibernate's built-in connection pool. Instead,
// automatically set up HikariCP.
initHikariCP(properties);
}
entityManagerFactory = new HibernatePersistence().createEntityManagerFactory(persistenceUnit, properties);
EntityManager entityManager = entityManagerFactory.createEntityManager();
initDDL(entityManager, properties);
return entityManager;
} else {
return entityManagerFactory.createEntityManager();
}
}
private static void initHikariCP(Map<String, Object> properties) {
String connectionUrl = (String) properties.remove("hibernate.connection.url");
String username = (String) properties.remove("hibernate.connection.username");
String password = (String) properties.remove("hibernate.connection.password");
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(connectionUrl);
hikariConfig.setUsername(username);
hikariConfig.setPassword(password);
// In case we're using MySQL, these settings are recommended by HikariCP:
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
String dialect = (String) properties.get("hibernate.dialect");
if (dialect != null && dialect.contains("PostgreSQL")) {
// The JDBC jar verion in the IP BOM does not support Connection.isValid(), so need to use this:
hikariConfig.setConnectionTestQuery("SELECT 1");
}
HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
properties.put("hibernate.connection.datasource", hikariDataSource);
}
private static void initDDL(EntityManager entityManager, Map<String, Object> properties) throws Exception {
// If the DDL is not already installed in the DB, automatically do it on first use.
SessionImplementor session = (SessionImplementor) entityManager.getDelegate();
Connection connection = session.connection();
if (!hasTables(connection)) {
// our tables don't exist -- create them
String dialect = (String) properties.get("hibernate.dialect");
if (dialect != null) {
String ddlFile;
if (dialect.contains("PostgreSQL")) {
ddlFile = "postgres9.sql";
} else if (dialect.contains("MySQL")) {
ddlFile = "mysql5.sql";
} else if (dialect.contains("Oracle")) {
ddlFile = "oracle10.sql";
} else if (dialect.contains("SQLServer")) {
ddlFile = "mssql2012.sql";
} else if (dialect.contains("DB2")) {
ddlFile = "db2.sql";
} else {
ddlFile = "h2.sql";
}
Statement statement = null;
try {
URL url = HibernateUtil.class.getClassLoader().getResource("ddl/" + ddlFile);
String ddl = IOUtils.toString(url);
statement = connection.createStatement();
statement.executeUpdate(ddl);
} finally {
if (statement != null) {
statement.close();
}
// do *not* close the connection -- it will still be used by this instance of the EntityManager
}
}
}
}
private static boolean hasTables(Connection connection) throws Exception {
DatabaseMetaData metadata = connection.getMetaData();
// check if "ArtificerArtifact" table exists
ResultSet tables = metadata.getTables(null, null, ArtificerArtifact.class.getSimpleName(), null);
if (tables.next()) {
return true;
}
// also need to check all caps (thanks, Oracle)
tables = metadata.getTables(null, null, ArtificerArtifact.class.getSimpleName().toUpperCase(Locale.ROOT), null);
if (tables.next()) {
return true;
}
// otherwise, nope
return false;
}
public static ArtificerArtifact getArtifact(String uuid, EntityManager entityManager, boolean fullFetch) throws ArtificerException {
Query q = entityManager.createQuery("FROM ArtificerArtifact a WHERE a.trashed = false AND a.uuid = ?");
q.setParameter(1, uuid);
ArtificerArtifact artifact;
try {
artifact = (ArtificerArtifact) q.getSingleResult();
} catch (NoResultException e) {
throw ArtificerNotFoundException.artifactNotFound(uuid);
}
if (fullFetch) {
Hibernate.initialize(artifact.getClassifiers());
Hibernate.initialize(artifact.getComments());
Hibernate.initialize(artifact.getNormalizedClassifiers());
Hibernate.initialize(artifact.getRelationships());
}
return artifact;
}
public static ArtificerOntology getOntology(String uuid, EntityManager entityManager) throws ArtificerException {
Query q = entityManager.createQuery("FROM ArtificerOntology a WHERE a.uuid = ?");
q.setParameter(1, uuid);
ArtificerOntology ontology;
try {
ontology = (ArtificerOntology) q.getSingleResult();
} catch (NoResultException e) {
throw ArtificerNotFoundException.ontologyNotFound(uuid);
}
Hibernate.initialize(ontology.getRootClasses());
return ontology;
}
public static ArtificerStoredQuery getStoredQuery(String queryName, EntityManager entityManager) throws ArtificerException {
ArtificerStoredQuery storedQuery = entityManager.find(ArtificerStoredQuery.class, queryName);
if (storedQuery == null) {
throw ArtificerNotFoundException.storedQueryNotFound(queryName);
}
return storedQuery;
}
/**
* Override the name of the persistence unit used to build the EMF. Mainly used for testing.
* @param name
*/
public static void setPersistenceUnit(String name) {
persistenceUnit = name;
}
}