If you use JDBC to connect to only one CN in the cluster, this CN may be overloaded and other CN resources wasted. It also incurs single-node failure risks.
To avoid these problems, you can use JDBC to connect to multiple CNs. Two modes are available:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private static final String USER_NAME = "dbadmin"; private static final String PASSWORD = "password"; // jdbc:postgresql://ELB_IP:PORT/dbName" private static final String URL = "jdbc:postgresql://100.95.153.169:8000/gaussdb"; private static Properties properties = new Properties(); static { properties.setProperty("user", USER_NAME); properties.setProperty("password", PASSWORD); } /** * Obtain the database connection. */ public static Connection getConnection() { Connection connection = null; try { connection = DriverManager.getConnection(URL, properties); } catch (SQLException e) { e.printStackTrace(); } return connection; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private static final String USER_NAME = "dbadmin"; private static final String PASSWORD = "password"; // jdbc:postgresql://host1:port1,host2:port2/dbName" private static final String URL = "jdbc:postgresql://100.95.146.194:8000,100.95.148.220:8000,100.93.0.221:8000/gaussdb?loadBalanceHosts=true"; private static Properties properties = new Properties(); static { properties.setProperty("user", USER_NAME); properties.setProperty("password", PASSWORD); } /** * Obtain the database connection. */ public static Connection getConnection() { Connection connection = null; try { connection = DriverManager.getConnection(URL, properties); } catch (SQLException e) { e.printStackTrace(); } return connection; } |