diff --git a/src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java b/src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java index e27b355..90263a4 100644 --- a/src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java +++ b/src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java @@ -87,6 +87,7 @@ public class CUBRIDDriver implements Driver { "jdbc:cubrid(-oracle|-mysql)?:([a-zA-Z_0-9\\.-]*):([0-9]*):([^:]+):([^:]*):([^:]*):(\\?[a-zA-Z_0-9]+=[^&=?]+(&[a-zA-Z_0-9]+=[^&=?]+)*)?"; private static final String CUBRID_JDBC_URL_HEADER = "jdbc:cubrid"; private static final String ENV_JDBC_PROP_NAME = "CUBRID_JDBC_PROP"; + private int conn_count = 0; static { try { @@ -265,9 +266,9 @@ public Connection connect(String url, Properties info) throws SQLException { altHostList.add(st.nextToken()); } - if (connProperties.getConnLoadBal()) { - Collections.shuffle(altHostList); - } + String loadBalValue = connProperties.getConnLoadBal(); + + adjustHostList(loadBalValue, altHostList); try { u_con = (UClientSideConnection) @@ -346,4 +347,24 @@ private boolean existPropertiesFile(String filePath) { File file = new File(filePath); return file.exists(); } + + private void adjustHostList(String loadBalValue, ArrayList altHostList) { + if (ConnectionProperties.LB_VAL_TRUE.equals(loadBalValue) + || ConnectionProperties.LB_VAL_ROUND_ROBIN.equals(loadBalValue)) { + int count = increment_conn_count(); + int dist = (count > altHostList.size()) ? (count - 1) % altHostList.size() : count - 1; + Collections.rotate(altHostList, -dist); + } else if (ConnectionProperties.LB_VAL_SHUFFLE.equals(loadBalValue)) { + Collections.shuffle(altHostList); + } else if (ConnectionProperties.LB_VAL_FALSE.equals(loadBalValue)) { + // do nothing + } else { + throw new IllegalArgumentException("Invalid loadBalValue: " + loadBalValue); + } + } + + private synchronized int increment_conn_count() { + conn_count = (conn_count >= Integer.MAX_VALUE) ? 1 : conn_count + 1; + return conn_count; + } } diff --git a/src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java b/src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java index 33eed86..4a692b0 100644 --- a/src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java +++ b/src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java @@ -43,6 +43,10 @@ public class ConnectionProperties { static ArrayList PROPERTY_LIST = new ArrayList(); + public static final String LB_VAL_TRUE = "true"; + public static final String LB_VAL_FALSE = "false"; + public static final String LB_VAL_SHUFFLE = "sh"; + public static final String LB_VAL_ROUND_ROBIN = "rr"; static { try { @@ -108,8 +112,8 @@ public void setProperties(Properties info) throws SQLException { CUBRIDJDBCErrorCode.invalid_url, " illegal access properties", null); } } - if (this.getConnLoadBal() && this.getAltHosts() == null) { - this.connLoadBal.setValue("false"); + if (!this.getConnLoadBal().equals(LB_VAL_FALSE) && this.getAltHosts() == null) { + this.connLoadBal.setValue(LB_VAL_FALSE); } if (this.getReconnectTime() < (BrokerHealthCheck.MONITORING_INTERVAL / 1000)) { this.rcTime.setValue((Integer) (BrokerHealthCheck.MONITORING_INTERVAL / 1000)); @@ -335,6 +339,30 @@ boolean validateValue(Object o) { } } + class StringLoadBalanceProperty extends ConnectionProperty { + String[] allowableValues = {LB_VAL_TRUE, LB_VAL_FALSE, LB_VAL_SHUFFLE, LB_VAL_ROUND_ROBIN}; + + StringLoadBalanceProperty(String propertyName, Object defaultValue) { + super(propertyName, defaultValue, 0, 0); + } + + String getValueAsString() { + return (String) valueAsObject; + } + + @Override + boolean validateValue(Object o) { + if (o instanceof String) { + for (int i = 0; i < allowableValues.length; i++) { + if (allowableValues[i].equalsIgnoreCase((String) o)) { + return true; + } + } + } + return false; + } + } + BooleanConnectionProperty logOnException = new BooleanConnectionProperty("logOnException", false); @@ -369,7 +397,8 @@ private int getDefaultConnectTimeout() { StringConnectionProperty altHosts = new StringConnectionProperty("altHosts", null); - BooleanConnectionProperty connLoadBal = new BooleanConnectionProperty("loadBalance", false); + StringLoadBalanceProperty connLoadBal = + new StringLoadBalanceProperty("loadBalance", LB_VAL_FALSE); ZeroDateTimeBehaviorConnectionProperty zeroDateTimeBehavior = new ZeroDateTimeBehaviorConnectionProperty( @@ -440,8 +469,8 @@ public String getAltHosts() { return altHosts.getValueAsString(); } - public boolean getConnLoadBal() { - return connLoadBal.getValueAsBoolean(); + public String getConnLoadBal() { + return connLoadBal.getValueAsString(); } public String getZeroDateTimeBehavior() {