source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/RandomBalanceStrategy.java @ 848

Last change on this file since 848 was 766, checked in by npipsl, 11 years ago
File size: 3.9 KB
Line 
1/*
2 Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the FLOSS License Exception
9  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11  This program is free software; you can redistribute it and/or modify it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22 
23 */
24package com.mysql.jdbc;
25
26import java.sql.SQLException;
27import java.util.ArrayList;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.Properties;
32
33public class RandomBalanceStrategy implements BalanceStrategy {
34
35        public RandomBalanceStrategy() {
36        }
37
38        public void destroy() {
39                // we don't have anything to clean up
40        }
41
42        public void init(Connection conn, Properties props) throws SQLException {
43                // we don't have anything to initialize
44        }
45
46        public ConnectionImpl pickConnection(LoadBalancingConnectionProxy proxy,
47                        List<String> configuredHosts, Map<String, ConnectionImpl> liveConnections, long[] responseTimes,
48                        int numRetries) throws SQLException {
49                int numHosts = configuredHosts.size();
50
51                SQLException ex = null;
52
53                List<String> whiteList = new ArrayList<String>(numHosts);
54                whiteList.addAll(configuredHosts);
55               
56                Map<String, Long> blackList = proxy.getGlobalBlacklist();
57
58                whiteList.removeAll(blackList.keySet());
59               
60                Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList);
61               
62
63                for (int attempts = 0; attempts < numRetries;) {
64                        int random = (int) Math.floor((Math.random() * whiteList.size()));
65                        if(whiteList.size() == 0){
66                                throw SQLError.createSQLException("No hosts configured", null);
67                        }
68
69                        String hostPortSpec = whiteList.get(random);
70
71                        ConnectionImpl conn = liveConnections.get(hostPortSpec);
72
73                        if (conn == null) {
74                                try {
75                                        conn = proxy.createConnectionForHost(hostPortSpec);
76                                } catch (SQLException sqlEx) {
77                                        ex = sqlEx;
78
79                                        if (proxy.shouldExceptionTriggerFailover(sqlEx)) {
80
81                                                Integer whiteListIndex = whiteListMap.get(hostPortSpec);
82
83                                                // exclude this host from being picked again
84                                                if (whiteListIndex != null) {
85                                                        whiteList.remove(whiteListIndex.intValue());
86                                                        whiteListMap = this.getArrayIndexMap(whiteList);
87                                                }
88                                                proxy.addToGlobalBlacklist( hostPortSpec );
89
90                                                if (whiteList.size() == 0) {
91                                                        attempts++;
92                                                        try {
93                                                                Thread.sleep(250);
94                                                        } catch (InterruptedException e) {
95                                                        }
96
97                                                        // start fresh
98                                                        whiteListMap = new HashMap<String, Integer>(numHosts);
99                                                        whiteList.addAll(configuredHosts);
100                                                        blackList = proxy.getGlobalBlacklist();
101
102                                                        whiteList.removeAll(blackList.keySet());
103                                                        whiteListMap = this.getArrayIndexMap(whiteList);
104                                                }
105
106                                                continue;
107                                        }
108
109                                        throw sqlEx;
110                                }
111                        }
112                       
113                        return conn;
114                }
115
116                if (ex != null) {
117                        throw ex;
118                }
119
120                return null; // we won't get here, compiler can't tell
121        }
122       
123        private Map<String, Integer> getArrayIndexMap(List<String> l) {
124                Map<String, Integer> m = new HashMap<String, Integer>(l.size());
125                for (int i = 0; i < l.size(); i++) {
126                        m.put(l.get(i), Integer.valueOf(i));
127                }
128                return m;
129               
130        }
131
132}
Note: See TracBrowser for help on using the repository browser.