1 24 25 package com.mysql.jdbc; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.Properties ; 30 import java.util.StringTokenizer ; 31 32 40 public class NonRegisteringReplicationDriver extends NonRegisteringDriver { 41 public NonRegisteringReplicationDriver() throws SQLException { 42 super(); 43 } 44 45 50 public Connection connect(String url, Properties info) throws SQLException { 51 Properties parsedProps = parseURL(url, info); 52 53 Properties masterProps = parsedProps; 54 Properties slavesProps = parsedProps; 55 56 String hostValues = parsedProps.getProperty(HOST_PROPERTY_KEY); 57 58 if (hostValues != null) { 59 StringTokenizer st = new StringTokenizer (hostValues, ","); 60 61 StringBuffer masterHost = new StringBuffer (); 62 StringBuffer slaveHosts = new StringBuffer (); 63 64 if (st.hasMoreTokens()) { 65 String [] hostPortPair = parseHostPortPair(st.nextToken()); 66 67 if (hostPortPair[HOST_NAME_INDEX] != null) { 68 masterHost.append(hostPortPair[HOST_NAME_INDEX]); 69 } 70 71 if (hostPortPair[PORT_NUMBER_INDEX] != null) { 72 masterHost.append(":"); 73 masterHost.append(hostPortPair[PORT_NUMBER_INDEX]); 74 } 75 } 76 77 boolean firstSlaveHost = true; 78 79 while (st.hasMoreTokens()) { 80 String [] hostPortPair = parseHostPortPair(st.nextToken()); 81 82 if (!firstSlaveHost) { 83 slaveHosts.append(","); 84 } else { 85 firstSlaveHost = false; 86 } 87 88 if (hostPortPair[HOST_NAME_INDEX] != null) { 89 slaveHosts.append(hostPortPair[HOST_NAME_INDEX]); 90 } 91 92 if (hostPortPair[PORT_NUMBER_INDEX] != null) { 93 slaveHosts.append(":"); 94 slaveHosts.append(hostPortPair[PORT_NUMBER_INDEX]); 95 } 96 } 97 98 if (slaveHosts.length() == 0) { 99 throw new SQLException ( 100 "Must specify at least one slave host to connect to for master/slave replication load-balancing functionality", 101 SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); 102 } 103 104 masterProps.setProperty(HOST_PROPERTY_KEY, masterHost.toString()); 105 slavesProps.setProperty(HOST_PROPERTY_KEY, slaveHosts.toString()); 106 } 107 108 return new ReplicationConnection(masterProps, slavesProps); 109 } 110 } 111 | Popular Tags |