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

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