2
Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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
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.
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
26
package com.mysql.jdbc;
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;
34
public class StandardLoadBalanceExceptionChecker implements
35
LoadBalanceExceptionChecker {
37
private List sqlStateList;
38
private List sqlExClassList;
40
public boolean shouldExceptionTriggerFailover(SQLException ex) {
41
String sqlState = ex.getSQLState();
43
if (sqlState != null) {
44
if (sqlState.startsWith("08")) {
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())){
58
// always handle CommunicationException
59
if(ex instanceof CommunicationsException){
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)){
75
public void destroy() {
76
// TODO Auto-generated method stub
80
public void init(Connection conn, Properties props) throws SQLException {
81
configureSQLStateList(props.getProperty("loadBalanceSQLStateFailover", null));
82
configureSQLExceptionSubclassList(props.getProperty("loadBalanceSQLExceptionSubclassFailover", null));
86
private void configureSQLStateList(String sqlStates){
87
if(sqlStates == null || "".equals(sqlStates)){
90
List states = StringUtils.split(sqlStates, ",", true);
91
List newStates = new ArrayList();
92
Iterator i = states.iterator();
95
String state = i.next().toString();
96
if(state.length() > 0){
100
if(newStates.size() > 0){
101
this.sqlStateList = newStates;
105
private void configureSQLExceptionSubclassList(String sqlExClasses){
106
if(sqlExClasses == null || "".equals(sqlExClasses)){
109
List classes = StringUtils.split(sqlExClasses, ",", true);
110
List newClasses = new ArrayList();
111
Iterator i = classes.iterator();
114
String exClass = i.next().toString();
116
Class c = Class.forName(exClass);
118
} catch (Exception e){
119
// ignore and don't check, class doesn't exist
122
if(newClasses.size() > 0){
123
this.sqlExClassList = newClasses;