|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * foreign-data wrapper for JDBC |
| 4 | + * |
| 5 | + * Portions Copyright (c) 2023, TOSHIBA CORPORATION |
| 6 | + * |
| 7 | + * This software is released under the PostgreSQL Licence |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * jdbc_fdw/JDBCConnection.java |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.net.URL; |
| 17 | +import java.sql.*; |
| 18 | +import java.util.Properties; |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | + |
| 21 | +public class JDBCConnection { |
| 22 | + private Connection conn = null; |
| 23 | + private boolean invalidate; |
| 24 | + private long server_hashvalue; // keep the uint32 val |
| 25 | + private long mapping_hashvalue; // keep the uint32 val |
| 26 | + |
| 27 | + private int queryTimeoutValue; |
| 28 | + private static JDBCDriverLoader jdbcDriverLoader; |
| 29 | + |
| 30 | + /* JDBC connection hash map */ |
| 31 | + private static ConcurrentHashMap<Integer, JDBCConnection> ConnectionHash = new ConcurrentHashMap<Integer, JDBCConnection>(); |
| 32 | + |
| 33 | + public JDBCConnection(Connection conn, boolean invalidate, long server_hashvalue, long mapping_hashvalue, int queryTimeoutValue) { |
| 34 | + this.conn = conn; |
| 35 | + this.invalidate = invalidate; |
| 36 | + this.server_hashvalue = server_hashvalue; |
| 37 | + this.mapping_hashvalue = mapping_hashvalue; |
| 38 | + this.queryTimeoutValue = queryTimeoutValue; |
| 39 | + } |
| 40 | + |
| 41 | + /* finalize all actived connection */ |
| 42 | + public static void finalizeAllConns(long hashvalue) throws Exception { |
| 43 | + for (JDBCConnection Jconn : ConnectionHash.values()) { |
| 44 | + Jconn.invalidate = true; |
| 45 | + |
| 46 | + if (Jconn.conn != null) { |
| 47 | + Jconn.conn.close(); |
| 48 | + Jconn.conn = null; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /* finalize connection have given server_hashvalue */ |
| 54 | + public static void finalizeAllServerConns(long hashvalue) throws Exception { |
| 55 | + for (JDBCConnection Jconn : ConnectionHash.values()) { |
| 56 | + if (Jconn.server_hashvalue == hashvalue) { |
| 57 | + Jconn.invalidate = true; |
| 58 | + System.out.println("Finalizing " + Jconn); |
| 59 | + |
| 60 | + if (Jconn.conn != null) { |
| 61 | + Jconn.conn.close(); |
| 62 | + Jconn.conn = null; |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /* finalize connection have given mapping_hashvalue */ |
| 70 | + public static void finalizeAllUserMapingConns(long hashvalue) throws Exception { |
| 71 | + for (JDBCConnection Jconn : ConnectionHash.values()) { |
| 72 | + if (Jconn.mapping_hashvalue == hashvalue) { |
| 73 | + Jconn.invalidate = true; |
| 74 | + System.out.println("Finalizing " + Jconn); |
| 75 | + |
| 76 | + if (Jconn.conn != null) { |
| 77 | + Jconn.conn.close(); |
| 78 | + Jconn.conn = null; |
| 79 | + } |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /* get query timeout value */ |
| 86 | + public int getQueryTimeout() { |
| 87 | + return queryTimeoutValue; |
| 88 | + } |
| 89 | + |
| 90 | + public Connection getConnection() { |
| 91 | + return this.conn; |
| 92 | + } |
| 93 | + |
| 94 | + /* get jdbc connection, create new one if not cached before */ |
| 95 | + public static JDBCConnection getConnection(int key, long server_hashvalue, long mapping_hashvalue, String[] options) throws Exception { |
| 96 | + if (ConnectionHash.containsKey(key)) { |
| 97 | + JDBCConnection Jconn = ConnectionHash.get(key); |
| 98 | + |
| 99 | + if (Jconn.invalidate == false) { |
| 100 | + System.out.println("got connection " + Jconn.getConnection()); |
| 101 | + return Jconn; |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + return createConnection(key, server_hashvalue, mapping_hashvalue, options); |
| 107 | + } |
| 108 | + |
| 109 | + /* Make new connection */ |
| 110 | + public static JDBCConnection createConnection(int key, long server_hashvalue, long mapping_hashvalue, String[] options) throws Exception { |
| 111 | + Properties jdbcProperties; |
| 112 | + Class<?> jdbcDriverClass = null; |
| 113 | + Driver jdbcDriver = null; |
| 114 | + String driverClassName = options[0]; |
| 115 | + String url = options[1]; |
| 116 | + String userName = options[2]; |
| 117 | + String password = options[3]; |
| 118 | + String qTimeoutValue = options[4]; |
| 119 | + String fileName = options[5]; |
| 120 | + |
| 121 | + try { |
| 122 | + File JarFile = new File(fileName); |
| 123 | + String jarfile_path = JarFile.toURI().toURL().toString(); |
| 124 | + |
| 125 | + if (jdbcDriverLoader == null) { |
| 126 | + /* If jdbcDriverLoader is being created. */ |
| 127 | + jdbcDriverLoader = new JDBCDriverLoader(new URL[] {JarFile.toURI().toURL()}); |
| 128 | + } else if (jdbcDriverLoader.CheckIfClassIsLoaded(driverClassName) == null) { |
| 129 | + jdbcDriverLoader.addPath(jarfile_path); |
| 130 | + } |
| 131 | + |
| 132 | + /* Make connection */ |
| 133 | + jdbcDriverClass = jdbcDriverLoader.loadClass(driverClassName); |
| 134 | + jdbcDriver = (Driver) jdbcDriverClass.newInstance(); |
| 135 | + jdbcProperties = new Properties(); |
| 136 | + jdbcProperties.put("user", userName); |
| 137 | + jdbcProperties.put("password", password); |
| 138 | + Connection conn = jdbcDriver.connect(url, jdbcProperties); |
| 139 | + |
| 140 | + if (conn == null) |
| 141 | + throw new SQLException("Cannot connect server: " + url); |
| 142 | + |
| 143 | + JDBCConnection Jconn = new JDBCConnection(conn, false, server_hashvalue, mapping_hashvalue, Integer.parseInt(qTimeoutValue)); |
| 144 | + |
| 145 | + /* cache new connection */ |
| 146 | + System.out.println("Create new connection " + key); |
| 147 | + ConnectionHash.put(key, Jconn); |
| 148 | + |
| 149 | + return Jconn; |
| 150 | + } catch (Throwable e) { |
| 151 | + throw e; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments