-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJDBCConnection.java
154 lines (129 loc) · 5.37 KB
/
JDBCConnection.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
/*-------------------------------------------------------------------------
*
* foreign-data wrapper for JDBC
*
* Portions Copyright (c) 2023, TOSHIBA CORPORATION
*
* This software is released under the PostgreSQL Licence
*
* IDENTIFICATION
* jdbc_fdw/JDBCConnection.java
*
*-------------------------------------------------------------------------
*/
import java.io.File;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
public class JDBCConnection {
private Connection conn = null;
private boolean invalidate;
private long server_hashvalue; // keep the uint32 val
private long mapping_hashvalue; // keep the uint32 val
private int queryTimeoutValue;
private static JDBCDriverLoader jdbcDriverLoader;
/* JDBC connection hash map */
private static ConcurrentHashMap<Integer, JDBCConnection> ConnectionHash = new ConcurrentHashMap<Integer, JDBCConnection>();
public JDBCConnection(Connection conn, boolean invalidate, long server_hashvalue, long mapping_hashvalue, int queryTimeoutValue) {
this.conn = conn;
this.invalidate = invalidate;
this.server_hashvalue = server_hashvalue;
this.mapping_hashvalue = mapping_hashvalue;
this.queryTimeoutValue = queryTimeoutValue;
}
/* finalize all actived connection */
public static void finalizeAllConns(long hashvalue) throws Exception {
for (JDBCConnection Jconn : ConnectionHash.values()) {
Jconn.invalidate = true;
if (Jconn.conn != null) {
Jconn.conn.close();
Jconn.conn = null;
}
}
}
/* finalize connection have given server_hashvalue */
public static void finalizeAllServerConns(long hashvalue) throws Exception {
for (JDBCConnection Jconn : ConnectionHash.values()) {
if (Jconn.server_hashvalue == hashvalue) {
Jconn.invalidate = true;
System.out.println("Finalizing " + Jconn);
if (Jconn.conn != null) {
Jconn.conn.close();
Jconn.conn = null;
}
break;
}
}
}
/* finalize connection have given mapping_hashvalue */
public static void finalizeAllUserMapingConns(long hashvalue) throws Exception {
for (JDBCConnection Jconn : ConnectionHash.values()) {
if (Jconn.mapping_hashvalue == hashvalue) {
Jconn.invalidate = true;
System.out.println("Finalizing " + Jconn);
if (Jconn.conn != null) {
Jconn.conn.close();
Jconn.conn = null;
}
break;
}
}
}
/* get query timeout value */
public int getQueryTimeout() {
return queryTimeoutValue;
}
public Connection getConnection() {
return this.conn;
}
/* get jdbc connection, create new one if not cached before */
public static JDBCConnection getConnection(int key, long server_hashvalue, long mapping_hashvalue, String[] options) throws Exception {
if (ConnectionHash.containsKey(key)) {
JDBCConnection Jconn = ConnectionHash.get(key);
if (Jconn.invalidate == false) {
System.out.println("got connection " + Jconn.getConnection());
return Jconn;
}
}
return createConnection(key, server_hashvalue, mapping_hashvalue, options);
}
/* Make new connection */
public static JDBCConnection createConnection(int key, long server_hashvalue, long mapping_hashvalue, String[] options) throws Exception {
Properties jdbcProperties;
Class<?> jdbcDriverClass = null;
Driver jdbcDriver = null;
String driverClassName = options[0];
String url = options[1];
String userName = options[2];
String password = options[3];
String qTimeoutValue = options[4];
String fileName = options[5];
try {
File JarFile = new File(fileName);
String jarfile_path = JarFile.toURI().toURL().toString();
if (jdbcDriverLoader == null) {
/* If jdbcDriverLoader is being created. */
jdbcDriverLoader = new JDBCDriverLoader(new URL[] {JarFile.toURI().toURL()});
} else if (jdbcDriverLoader.CheckIfClassIsLoaded(driverClassName) == null) {
jdbcDriverLoader.addPath(jarfile_path);
}
/* Make connection */
jdbcDriverClass = jdbcDriverLoader.loadClass(driverClassName);
jdbcDriver = (Driver) jdbcDriverClass.newInstance();
jdbcProperties = new Properties();
jdbcProperties.put("user", userName);
jdbcProperties.put("password", password);
Connection conn = jdbcDriver.connect(url, jdbcProperties);
if (conn == null)
throw new SQLException("Cannot connect server: " + url);
JDBCConnection Jconn = new JDBCConnection(conn, false, server_hashvalue, mapping_hashvalue, Integer.parseInt(qTimeoutValue));
/* cache new connection */
System.out.println("Create new connection " + key);
ConnectionHash.put(key, Jconn);
return Jconn;
} catch (Throwable e) {
throw e;
}
}
}