亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? loadbalancingconnectionproxy.java

?? mysql jdbc驅(qū)動程序 mysql jdbc驅(qū)動程序 mysql jdbc驅(qū)動程序 mysql jdbc驅(qū)動程序
?? JAVA
字號:
/*
 Copyright (C) 2007 MySQL AB

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */

package com.mysql.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * An implementation of java.sql.Connection that load balances requests across a
 * series of MySQL JDBC connections, where the balancing takes place at
 * transaction commit.
 * 
 * Therefore, for this to work (at all), you must use transactions, even if only
 * reading data.
 * 
 * This implementation will invalidate connections that it detects have had
 * communication errors when processing a request. A new connection to the
 * problematic host will be attempted the next time it is selected by the load
 * balancing algorithm.
 * 
 * This implementation is thread-safe, but it's questionable whether sharing a
 * connection instance amongst threads is a good idea, given that transactions
 * are scoped to connections in JDBC.
 * 
 * @version $Id: $
 * 
 */
public class LoadBalancingConnectionProxy implements InvocationHandler, PingTarget {

	private static Method getLocalTimeMethod;

	static {
		try {
			getLocalTimeMethod = System.class.getMethod("nanoTime",
					new Class[0]);
		} catch (SecurityException e) {
			// ignore
		} catch (NoSuchMethodException e) {
			// ignore
		}
	}

	interface BalanceStrategy {
		abstract Connection pickConnection() throws SQLException;
	}

	class BestResponseTimeBalanceStrategy implements BalanceStrategy {

		public Connection pickConnection() throws SQLException {
			long minResponseTime = Long.MAX_VALUE;

			int bestHostIndex = 0;

			long[] localResponseTimes = new long[responseTimes.length];
			
			synchronized (responseTimes) {
				System.arraycopy(responseTimes, 0, localResponseTimes, 0, responseTimes.length);
			}
			
			SQLException ex = null;
			
			for (int attempts = 0; attempts < 1200 /* 5 minutes */; attempts++) {
				for (int i = 0; i < localResponseTimes.length; i++) {
					long candidateResponseTime = localResponseTimes[i];
	
					if (candidateResponseTime < minResponseTime) {
						if (candidateResponseTime == 0) {
							bestHostIndex = i;
	
							break;
						}
	
						bestHostIndex = i;
						minResponseTime = candidateResponseTime;
					}
				}
	
				if (bestHostIndex == localResponseTimes.length - 1) {
					// try again, assuming that the previous list was mostly
					// correct as far as distribution of response times went
					
					synchronized (responseTimes) {
						System.arraycopy(responseTimes, 0, localResponseTimes, 0, responseTimes.length);
					}
					
					continue;
				}
				String bestHost = (String) hostList.get(bestHostIndex);
	
				Connection conn = (Connection) liveConnections.get(bestHost);
	
				if (conn == null) {
					try {
						conn = createConnectionForHost(bestHost);
					} catch (SQLException sqlEx) {
						ex = sqlEx;
						
						if (sqlEx instanceof CommunicationsException || "08S01".equals(sqlEx.getSQLState())) {
							localResponseTimes[bestHostIndex] = Long.MAX_VALUE;
							
							try {
								Thread.sleep(250);
							} catch (InterruptedException e) {
							}
							
							continue;
						} else {
							throw sqlEx;
						}
					}
				}
	
				return conn;
			}
			
			if (ex != null) {
				throw ex;
			}
			
			return null; // we won't get here, compiler can't tell
		}
	}

	// Lifted from C/J 5.1's JDBC-2.0 connection pool classes, let's merge this
	// if/when this gets into 5.1
	protected class ConnectionErrorFiringInvocationHandler implements
			InvocationHandler {
		Object invokeOn = null;

		public ConnectionErrorFiringInvocationHandler(Object toInvokeOn) {
			invokeOn = toInvokeOn;
		}

		public Object invoke(Object proxy, Method method, Object[] args)
				throws Throwable {
			Object result = null;

			try {
				result = method.invoke(invokeOn, args);

				if (result != null) {
					result = proxyIfInterfaceIsJdbc(result, result.getClass());
				}
			} catch (InvocationTargetException e) {
				dealWithInvocationException(e);
			}

			return result;
		}
	}

	class RandomBalanceStrategy implements BalanceStrategy {

		public Connection pickConnection() throws SQLException {
			int random = (int) (Math.random() * hostList.size());

			if (random == hostList.size()) {
				random--;
			}

			String hostPortSpec = (String) hostList.get(random);

			SQLException ex = null;
			
			for (int attempts = 0; attempts < 1200 /* 5 minutes */; attempts++) {
				Connection conn = (Connection) liveConnections.get(hostPortSpec);
				
				if (conn == null) {
					try {
						conn = createConnectionForHost(hostPortSpec);
					} catch (SQLException sqlEx) {
						ex = sqlEx;
						
						if (sqlEx instanceof CommunicationsException || "08S01".equals(sqlEx.getSQLState())) {
							
							try {
								Thread.sleep(250);
							} catch (InterruptedException e) {
							}
							
							continue;
						} else {
							throw sqlEx;
						}
					}
				}
	
				return conn;
			}
			
			if (ex != null) {
				throw ex;
			}
			
			return null; // we won't get here, compiler can't tell
		}

	}

	private Connection currentConn;

	private List hostList;

	private Map liveConnections;

	private Map connectionsToHostsMap;

	private long[] responseTimes;

	private Map hostsToListIndexMap;

	boolean inTransaction = false;

	long transactionStartTime = 0;

	Properties localProps;

	boolean isClosed = false;

	BalanceStrategy balancer;

	/**
	 * Creates a proxy for java.sql.Connection that routes requests between the
	 * given list of host:port and uses the given properties when creating
	 * connections.
	 * 
	 * @param hosts
	 * @param props
	 * @throws SQLException
	 */
	LoadBalancingConnectionProxy(List hosts, Properties props)
			throws SQLException {
		this.hostList = hosts;

		int numHosts = this.hostList.size();

		this.liveConnections = new HashMap(numHosts);
		this.connectionsToHostsMap = new HashMap(numHosts);
		this.responseTimes = new long[numHosts];
		this.hostsToListIndexMap = new HashMap(numHosts);

		for (int i = 0; i < numHosts; i++) {
			this.hostsToListIndexMap.put(this.hostList.get(i), new Integer(i));
		}

		this.localProps = (Properties) props.clone();
		this.localProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
		this.localProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);
		this.localProps.setProperty("useLocalSessionState", "true");

		String strategy = this.localProps.getProperty("loadBalanceStrategy",
				"random");

		if ("random".equals(strategy)) {
			this.balancer = new RandomBalanceStrategy();
		} else if ("bestResponseTime".equals(strategy)) {
			this.balancer = new BestResponseTimeBalanceStrategy();
		} else {
			throw SQLError.createSQLException(Messages.getString(
					"InvalidLoadBalanceStrategy", new Object[] { strategy }),
					SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
		}

		pickNewConnection();
	}

	/**
	 * Creates a new physical connection for the given host:port and updates
	 * required internal mappings and statistics for that connection.
	 * 
	 * @param hostPortSpec
	 * @return
	 * @throws SQLException
	 */
	private synchronized Connection createConnectionForHost(String hostPortSpec)
			throws SQLException {
		Properties connProps = (Properties) this.localProps.clone();

		String[] hostPortPair = NonRegisteringDriver
				.parseHostPortPair(hostPortSpec);

		if (hostPortPair[1] == null) {
			hostPortPair[1] = "3306";
		}

		connProps.setProperty(NonRegisteringDriver.HOST_PROPERTY_KEY,
				hostPortSpec);
		connProps.setProperty(NonRegisteringDriver.PORT_PROPERTY_KEY,
				hostPortPair[1]);

		Connection conn = new Connection(hostPortSpec, Integer
				.parseInt(hostPortPair[1]), connProps, connProps
				.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY),
				"jdbc:mysql://" + hostPortPair[0] + ":" + hostPortPair[1] + "/");

		this.liveConnections.put(hostPortSpec, conn);
		this.connectionsToHostsMap.put(conn, hostPortSpec);

		return conn;
	}

	/**
	 * @param e
	 * @throws SQLException
	 * @throws Throwable
	 * @throws InvocationTargetException
	 */
	void dealWithInvocationException(InvocationTargetException e)
			throws SQLException, Throwable, InvocationTargetException {
		Throwable t = e.getTargetException();

		if (t != null) {
			if (t instanceof SQLException) {
				String sqlState = ((SQLException) t).getSQLState();

				if (sqlState != null) {
					if (sqlState.startsWith("08")) {
						// connection error, close up shop on current
						// connection
						invalidateCurrentConnection();
					}
				}
			}

			throw t;
		}

		throw e;
	}

	/**
	 * Closes current connection and removes it from required mappings.
	 * 
	 * @throws SQLException
	 */
	synchronized void invalidateCurrentConnection() throws SQLException {
		try {
			if (!this.currentConn.isClosed()) {
				this.currentConn.close();
			}

		} finally {
			this.liveConnections.remove(this.connectionsToHostsMap
					.get(this.currentConn));
			this.connectionsToHostsMap.remove(this.currentConn);
		}
	}

	/**
	 * Proxies method invocation on the java.sql.Connection interface, trapping
	 * "close", "isClosed" and "commit/rollback" (to switch connections for load
	 * balancing).
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {

		String methodName = method.getName();

		if ("close".equals(methodName)) {
			synchronized (this.liveConnections) {
				// close all underlying connections
				Iterator allConnections = this.liveConnections.values()
						.iterator();

				while (allConnections.hasNext()) {
					((Connection) allConnections.next()).close();
				}

				this.liveConnections.clear();
				this.connectionsToHostsMap.clear();
			}

			return null;
		}

		if ("isClosed".equals(methodName)) {
			return Boolean.valueOf(this.isClosed);
		}

		if (this.isClosed) {
			throw SQLError.createSQLException(
					"No operations allowed after connection closed.",
					SQLError.SQL_STATE_CONNECTION_NOT_OPEN);
		}

		if (!inTransaction) {
			this.inTransaction = true;
			this.transactionStartTime = getLocalTimeBestResolution();
		}

		Object result = null;

		try {
			result = method.invoke(this.currentConn, args);

			if (result != null) {
				if (result instanceof com.mysql.jdbc.Statement) {
					((com.mysql.jdbc.Statement)result).setPingTarget(this);
				}
				
				result = proxyIfInterfaceIsJdbc(result, result.getClass());
			}
		} catch (InvocationTargetException e) {
			dealWithInvocationException(e);
		} finally {
			if ("commit".equals(methodName) || "rollback".equals(methodName)) {
				this.inTransaction = false;

				// Update stats
				int hostIndex = ((Integer) this.hostsToListIndexMap
						.get(this.connectionsToHostsMap.get(this.currentConn)))
						.intValue();

				synchronized (this.responseTimes) {
					this.responseTimes[hostIndex] = getLocalTimeBestResolution()
							- this.transactionStartTime;
				}

				pickNewConnection();
			}
		}

		return result;
	}

	/**
	 * Picks the "best" connection to use for the next transaction based on the
	 * BalanceStrategy in use.
	 * 
	 * @throws SQLException
	 */
	private synchronized void pickNewConnection() throws SQLException {
		if (this.currentConn == null) {
			this.currentConn = this.balancer.pickConnection();

			return;
		}

		Connection newConn = this.balancer.pickConnection();

		newConn.setTransactionIsolation(this.currentConn
				.getTransactionIsolation());
		newConn.setAutoCommit(this.currentConn.getAutoCommit());
		this.currentConn = newConn;
	}

	/**
	 * Recursively checks for interfaces on the given object to determine if it
	 * implements a java.sql interface, and if so, proxies the instance so that
	 * we can catch and fire SQL errors.
	 * 
	 * @param toProxy
	 * @param clazz
	 * @return
	 */
	Object proxyIfInterfaceIsJdbc(Object toProxy, Class clazz) {
		Class[] interfaces = clazz.getInterfaces();

		for (int i = 0; i < interfaces.length; i++) {
			String packageName = interfaces[i].getPackage().getName();

			if ("java.sql".equals(packageName)
					|| "javax.sql".equals(packageName)) {
				return Proxy.newProxyInstance(toProxy.getClass()
						.getClassLoader(), interfaces,
						new ConnectionErrorFiringInvocationHandler(toProxy));
			}

			return proxyIfInterfaceIsJdbc(toProxy, interfaces[i]);
		}

		return toProxy;
	}

	/**
	 * Returns best-resolution representation of local time, using nanoTime() if
	 * availble, otherwise defaulting to currentTimeMillis().
	 */
	private static long getLocalTimeBestResolution() {
		if (getLocalTimeMethod != null) {
			try {
				return ((Long) getLocalTimeMethod.invoke(null, null))
						.longValue();
			} catch (IllegalArgumentException e) {
				// ignore - we fall through to currentTimeMillis()
			} catch (IllegalAccessException e) {
				// ignore - we fall through to currentTimeMillis()
			} catch (InvocationTargetException e) {
				// ignore - we fall through to currentTimeMillis()
			}
		}

		return System.currentTimeMillis();
	}

	public synchronized void doPing() throws SQLException {
		Iterator allConns = this.liveConnections.values().iterator();
		
		while (allConns.hasNext()) {
			((Connection)allConns.next()).ping();
		}
	}
}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
中文一区二区在线观看| 91国偷自产一区二区三区观看 | 久久国产尿小便嘘嘘尿| 欧美日本国产一区| 婷婷成人激情在线网| 91精品国产一区二区三区蜜臀 | 日韩欧美你懂的| 国产一区二区女| 日韩一区欧美小说| 日本韩国欧美国产| 美女视频一区二区三区| 久久亚洲捆绑美女| 99re热视频这里只精品| 亚洲电影激情视频网站| 91精品国产美女浴室洗澡无遮挡| 久久精品72免费观看| 国产视频一区在线观看| 91理论电影在线观看| 日韩国产欧美一区二区三区| 2022国产精品视频| eeuss鲁片一区二区三区在线看| 亚洲夂夂婷婷色拍ww47| 日韩色视频在线观看| eeuss鲁片一区二区三区在线看| 亚洲国产综合视频在线观看| 精品免费日韩av| 波多野结衣精品在线| 午夜一区二区三区在线观看| 精品成人私密视频| av福利精品导航| 日韩vs国产vs欧美| 国产精品美日韩| 精品污污网站免费看| 国产精品一区二区果冻传媒| 一区二区三区四区乱视频| 日韩三级在线观看| 99视频精品在线| 精品在线观看视频| 亚洲精品视频一区二区| 精品国产乱子伦一区| 欧美自拍丝袜亚洲| 国产盗摄精品一区二区三区在线| 婷婷成人激情在线网| 国产精品久久久久久久浪潮网站| 欧美一区二区在线免费播放 | 色综合久久久久网| 极品美女销魂一区二区三区 | 精品久久久久久亚洲综合网| 色8久久人人97超碰香蕉987| 国产精品自在在线| 日韩中文欧美在线| 综合色中文字幕| 久久欧美中文字幕| 欧美区视频在线观看| 99综合影院在线| 国产麻豆91精品| 蜜桃精品视频在线| 亚洲国产成人av网| 国产精品久久一卡二卡| 精品va天堂亚洲国产| 4438成人网| 色婷婷激情一区二区三区| av不卡在线播放| 国产jizzjizz一区二区| 激情久久五月天| 蜜臀99久久精品久久久久久软件| 亚洲一区成人在线| 亚洲精品写真福利| 17c精品麻豆一区二区免费| 国产色产综合产在线视频| 精品久久久久久无| 日韩精品中文字幕在线一区| 欧美精品粉嫩高潮一区二区| 欧美在线啊v一区| 在线这里只有精品| 色偷偷成人一区二区三区91| 91一区二区在线观看| 91麻豆产精品久久久久久| 国产不卡免费视频| 国产呦萝稀缺另类资源| 久久精品99久久久| 久久精品国产精品亚洲精品| 老司机精品视频在线| 激情亚洲综合在线| 国产酒店精品激情| 成人免费va视频| 成人av集中营| 日本韩国视频一区二区| 欧美日韩一区二区三区四区五区| 在线观看91视频| 欧美喷水一区二区| 91精品国产高清一区二区三区| 在线综合亚洲欧美在线视频| 7777精品伊人久久久大香线蕉| 在线播放欧美女士性生活| 欧美一区二区精品久久911| 精品日韩成人av| 国产日韩欧美一区二区三区乱码 | 国产制服丝袜一区| 国产成人啪午夜精品网站男同| 成人h精品动漫一区二区三区| 成人黄色电影在线| 日本道色综合久久| 制服视频三区第一页精品| 日韩免费观看2025年上映的电影| 精品成人在线观看| 国产精品国产自产拍高清av| 一区二区三区四区在线免费观看| 丝袜国产日韩另类美女| 精品亚洲成a人| 99re热这里只有精品免费视频| 欧美日韩成人综合| 久久免费视频色| 最新日韩av在线| 日本免费在线视频不卡一不卡二| 国精产品一区一区三区mba桃花| 波多野洁衣一区| 正在播放一区二区| 国产精品久久久久久亚洲毛片| 亚洲成人av一区二区三区| 韩国成人精品a∨在线观看| 91一区二区三区在线观看| 精品人在线二区三区| 亚洲少妇中出一区| 久久精品国产**网站演员| 91国在线观看| 亚洲国产精品传媒在线观看| 亚洲成人精品一区二区| 成人国产精品免费| 精品国产一区二区在线观看| 一色桃子久久精品亚洲| 久久er99精品| 欧美日本在线视频| 亚洲色图第一区| 国产精品99久久久久久宅男| 欧美久久久久中文字幕| 综合欧美亚洲日本| 国产毛片精品视频| 555www色欧美视频| 亚洲一级二级三级| 91亚洲国产成人精品一区二区三 | 3atv一区二区三区| 一区二区三区四区在线| 成人高清免费观看| 精品久久久久久最新网址| 视频在线在亚洲| 色婷婷av一区二区三区gif| 久久亚洲一区二区三区四区| 日韩电影在线观看一区| 日本韩国欧美在线| 亚洲人精品一区| 成人美女视频在线看| 国产性天天综合网| 久久精品国产99久久6| 91精品国产综合久久福利软件 | 国产日韩精品一区| 精品在线一区二区| 日韩手机在线导航| 免播放器亚洲一区| 884aa四虎影成人精品一区| 亚洲中国最大av网站| 欧美综合色免费| 亚洲18色成人| 欧美日韩亚洲综合在线 欧美亚洲特黄一级| 中文字幕在线一区| 99久免费精品视频在线观看 | 91成人在线免费观看| 亚洲人妖av一区二区| 国产精品18久久久久| 日韩色在线观看| 精一区二区三区| 国产亚洲1区2区3区| 成人福利电影精品一区二区在线观看| 久久久精品免费观看| 国产麻豆精品在线| 国产精品午夜春色av| k8久久久一区二区三区| 国产精品福利电影一区二区三区四区| 国产99久久久久久免费看农村| 国产欧美日韩在线视频| av在线综合网| 一区二区三区丝袜| 欧美久久久一区| 精品一区二区三区视频| 久久久久久免费毛片精品| 粉嫩aⅴ一区二区三区四区| 中文字幕一区二区三区在线播放| 99久久精品国产麻豆演员表| 亚洲激情一二三区| 欧美日韩精品系列| 久久精品国产一区二区| 中文字幕av一区二区三区免费看| 99国产精品一区| 亚洲成人免费观看| 久久久久久久综合| 成人av网站在线观看| 亚洲电影中文字幕在线观看| 日韩欧美黄色影院| 成人av先锋影音| 日韩电影免费在线看|