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
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.framework.skynet.core.utility;
import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcConnection;
import org.eclipse.osee.jdbc.JdbcService;
import org.eclipse.osee.jdbc.JdbcStatement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import com.google.common.collect.Iterables;
/**
* Handles connection recovery in the event of database connection being lost
*
* @author Jeff C. Phillips
*/
public final class ConnectionHandler {
private ConnectionHandler() {
// Utility class
}
private static JdbcService jdbcServiceInstance;
public static JdbcClient getJdbcClient() {
if (jdbcServiceInstance == null) {
Bundle bundle = FrameworkUtil.getBundle(ConnectionHandler.class);
BundleContext context = bundle.getBundleContext();
jdbcServiceInstance = findJdbcService(context);
}
return jdbcServiceInstance != null ? jdbcServiceInstance.getClient() : null;
}
private static JdbcService findJdbcService(BundleContext context) {
JdbcService toReturn = null;
try {
Collection<ServiceReference<JdbcService>> references =
context.getServiceReferences(JdbcService.class, "(osgi.binding=skynet.jdbc.service)");
ServiceReference<JdbcService> reference = Iterables.getFirst(references, null);
if (reference != null) {
toReturn = context.getService(reference);
}
} catch (InvalidSyntaxException ex) {
throw new OseeCoreException(ex, "Error finding JdbcService reference with osgi.binding=skynet.jdbc.service");
}
return toReturn;
}
public static JdbcStatement getStatement() throws OseeDataStoreException {
return getJdbcClient().getStatement();
}
public static JdbcStatement getStatement(JdbcConnection connection) throws OseeDataStoreException {
return getJdbcClient().getStatement(connection);
}
/**
* This method should only be used when not contained in a DB transaction
*
* @return number of records updated
*/
public static int runPreparedUpdate(String query, Object... data) throws OseeCoreException {
return getJdbcClient().runPreparedUpdate(query, data);
}
/**
* This method should only be used when not contained in a DB transaction
*
* @return number of records updated
*/
public static int runBatchUpdate(String query, List<Object[]> dataList) throws OseeCoreException {
return getJdbcClient().runBatchUpdate(query, dataList);
}
/**
* This method should only be used when contained in a DB transaction
*
* @return number of records updated
*/
public static int runPreparedUpdate(JdbcConnection connection, String query, Object... data) throws OseeCoreException {
return getJdbcClient().runPreparedUpdate(connection, query, data);
}
public static int runPreparedQueryFetchInt(int defaultValue, String query, Object... data) throws OseeCoreException {
return getJdbcClient().runPreparedQueryFetchObject(defaultValue, query, data);
}
public static String runPreparedQueryFetchString(String defaultValue, String query, Object... data) throws OseeCoreException {
return getJdbcClient().runPreparedQueryFetchObject(defaultValue, query, data);
}
public static long getNextSequence(String sequenceName) {
return getJdbcClient().getNextSequence(sequenceName);
}
}
|