~mark-matthews-a/connectorj/trunk

« back to all changes in this revision

Viewing changes to src/com/mysql/jdbc/StandardLoadBalanceExceptionChecker.java

  • Committer: mark.matthews at oracle
  • Date: 2010-05-12 15:38:46 UTC
  • mfrom: (847.1.94 branch_5_1-local)
  • Revision ID: mark.matthews@oracle.com-20100512153846-bdyw3aezw5o1szmu
Merged from 5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 
3
 
 
4
  The MySQL Connector/J is licensed under the terms of the GPL,
 
5
  like most MySQL Connectors. There are special exceptions to the
 
6
  terms and conditions of the GPL as it is applied to this software,
 
7
  see the FLOSS License Exception available on mysql.com.
 
8
 
 
9
  This program is free software; you can redistribute it and/or
 
10
  modify it under the terms of the GNU General Public License as
 
11
  published by the Free Software Foundation; version 2 of the
 
12
  License.
 
13
 
 
14
  This program is distributed in the hope that it will be useful,  
 
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
  GNU General Public License for more details.
 
18
 
 
19
  You should have received a copy of the GNU General Public License
 
20
  along with this program; if not, write to the Free Software
 
21
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
22
  02110-1301 USA
 
23
 
 
24
 */
 
25
 
 
26
package com.mysql.jdbc;
 
27
 
 
28
import java.sql.SQLException;
 
29
import java.util.ArrayList;
 
30
import java.util.Iterator;
 
31
import java.util.List;
 
32
import java.util.Properties;
 
33
 
 
34
public class StandardLoadBalanceExceptionChecker implements
 
35
                LoadBalanceExceptionChecker {
 
36
        
 
37
        private List sqlStateList;
 
38
        private List sqlExClassList;
 
39
        
 
40
        public boolean shouldExceptionTriggerFailover(SQLException ex) {
 
41
                String sqlState = ex.getSQLState();
 
42
 
 
43
                if (sqlState != null) {
 
44
                        if (sqlState.startsWith("08")) {
 
45
                                // connection error
 
46
                                return true;
 
47
                        }
 
48
                        if(this.sqlStateList != null){
 
49
                                // check against SQLState list
 
50
                                for(Iterator i = sqlStateList.iterator(); i.hasNext(); ){
 
51
                                        if(sqlState.startsWith(i.next().toString())){
 
52
                                                return true;
 
53
                                        }
 
54
                                }
 
55
                        }
 
56
                }
 
57
                
 
58
                // always handle CommunicationException
 
59
                if(ex instanceof CommunicationsException){
 
60
                        return true;
 
61
                }
 
62
                if(this.sqlExClassList != null){
 
63
                        // check against configured class lists
 
64
                        for(Iterator i = sqlExClassList.iterator(); i.hasNext(); ){
 
65
                                if(((Class)i.next()).isInstance(ex)){
 
66
                                        return true;
 
67
                                }
 
68
                        }
 
69
                }
 
70
                // no matches
 
71
                return false;
 
72
 
 
73
        }
 
74
 
 
75
        public void destroy() {
 
76
                // TODO Auto-generated method stub
 
77
 
 
78
        }
 
79
 
 
80
        public void init(Connection conn, Properties props) throws SQLException {
 
81
                configureSQLStateList(props.getProperty("loadBalanceSQLStateFailover", null));
 
82
                configureSQLExceptionSubclassList(props.getProperty("loadBalanceSQLExceptionSubclassFailover", null));
 
83
 
 
84
        }
 
85
        
 
86
        private void configureSQLStateList(String sqlStates){
 
87
                if(sqlStates == null || "".equals(sqlStates)){
 
88
                        return;
 
89
                }
 
90
                List states = StringUtils.split(sqlStates, ",", true);
 
91
                List newStates = new ArrayList();
 
92
                Iterator i = states.iterator();
 
93
                
 
94
                while(i.hasNext()){
 
95
                        String state = i.next().toString();
 
96
                        if(state.length() > 0){
 
97
                                newStates.add(state);
 
98
                        }
 
99
                }
 
100
                if(newStates.size() > 0){
 
101
                        this.sqlStateList = newStates;
 
102
                }
 
103
                
 
104
        }
 
105
        private void configureSQLExceptionSubclassList(String sqlExClasses){
 
106
                if(sqlExClasses == null || "".equals(sqlExClasses)){
 
107
                        return;
 
108
                }
 
109
                List classes = StringUtils.split(sqlExClasses, ",", true);
 
110
                List newClasses = new ArrayList();
 
111
                Iterator i = classes.iterator();
 
112
                
 
113
                while(i.hasNext()){
 
114
                        String exClass = i.next().toString();
 
115
                        try{
 
116
                                Class c = Class.forName(exClass);
 
117
                                newClasses.add(c);
 
118
                        } catch (Exception e){ 
 
119
                                // ignore and don't check, class doesn't exist
 
120
                        }
 
121
                }
 
122
                if(newClasses.size() > 0){
 
123
                        this.sqlExClassList = newClasses;
 
124
                }
 
125
                
 
126
        }
 
127
 
 
128
}