com.mysql.cj.jdbc.ConnectionImpl Java Examples
The following examples show how to use
com.mysql.cj.jdbc.ConnectionImpl.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UtilsTest.java From FoxTelem with GNU General Public License v3.0 | 6 votes |
/** * Tests Util.isJdbcPackage() * * @throws Exception */ public void testGetImplementedInterfaces() throws Exception { Class<?>[] ifaces; ifaces = Util.getImplementedInterfaces(JdbcStatement.class); assertEquals(2, ifaces.length); assertEquals(ifaces[0], java.sql.Statement.class); ifaces = Util.getImplementedInterfaces(StatementImpl.class); assertEquals(1, ifaces.length); assertEquals(ifaces[0], JdbcStatement.class); ifaces = Util.getImplementedInterfaces(ConnectionImpl.class); assertEquals(3, ifaces.length); List<Class<?>> ifacesList = Arrays.asList(ifaces); for (Class<?> clazz : new Class<?>[] { JdbcConnection.class, Serializable.class }) { assertTrue(ifacesList.contains(clazz)); } }
Example #2
Source File: LoadBalancedConnectionProxy.java From FoxTelem with GNU General Public License v3.0 | 6 votes |
/** * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection. * * @param hostInfo * The host info instance. * @return * The new Connection instance. * @throws SQLException * if an error occurs */ @Override public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException { ConnectionImpl conn = super.createConnectionForHost(hostInfo); this.liveConnections.put(hostInfo.getHostPortPair(), conn); this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair()); this.totalPhysicalConnections++; for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) { if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) { ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters(); break; } } return conn; }
Example #3
Source File: LoadBalancedConnectionProxy.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection. * * @param hostInfo * The host info instance. * @return * The new Connection instance. */ @Override public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException { ConnectionImpl conn = super.createConnectionForHost(hostInfo); this.liveConnections.put(hostInfo.getHostPortPair(), conn); this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair()); this.totalPhysicalConnections++; for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) { if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) { ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters(); break; } } return conn; }
Example #4
Source File: ServerAffinityStrategy.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { if (this.affinityOrderedServers == null) { return super.pickConnection(proxy, configuredHosts, liveConnections, responseTimes, numRetries); } Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); for (String host : this.affinityOrderedServers) { if (configuredHosts.contains(host) && !blackList.containsKey(host)) { ConnectionImpl conn = (ConnectionImpl) liveConnections.get(host); if (conn != null) { return conn; } try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(host); return conn; } catch (SQLException sqlEx) { if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(host); } } } } // Failed to connect to all hosts in the affinity list. Delegate to RandomBalanceStrategy. return super.pickConnection(proxy, configuredHosts, liveConnections, responseTimes, numRetries); }
Example #5
Source File: ServerAffinityStrategy.java From FoxTelem with GNU General Public License v3.0 | 5 votes |
@Override public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { if (this.affinityOrderedServers == null) { return super.pickConnection(proxy, configuredHosts, liveConnections, responseTimes, numRetries); } Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); for (String host : this.affinityOrderedServers) { if (configuredHosts.contains(host) && !blackList.containsKey(host)) { ConnectionImpl conn = (ConnectionImpl) liveConnections.get(host); if (conn != null) { return conn; } try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(host); return conn; } catch (SQLException sqlEx) { if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(host); } } } } // Failed to connect to all hosts in the affinity list. Delegate to RandomBalanceStrategy. return super.pickConnection(proxy, configuredHosts, liveConnections, responseTimes, numRetries); }
Example #6
Source File: LoadBalancedConnectionProxy.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Pings live connections. */ public synchronized void doPing() throws SQLException { SQLException se = null; boolean foundHost = false; int pingTimeout = this.currentConnection.getPropertySet().getIntegerReadableProperty(PropertyDefinitions.PNAME_loadBalancePingTimeout).getValue(); synchronized (this) { for (HostInfo hi : this.hostsList) { String host = hi.getHostPortPair(); ConnectionImpl conn = this.liveConnections.get(host); if (conn == null) { continue; } try { if (pingTimeout == 0) { conn.ping(); } else { conn.pingInternal(true, pingTimeout); } foundHost = true; } catch (SQLException e) { // give up if it is the current connection, otherwise NPE faking resultset later. if (host.equals(this.connectionsToHostsMap.get(this.currentConnection))) { // clean up underlying connections, since connection pool won't do it closeAllConnections(); this.isClosed = true; this.closedReason = "Connection closed because ping of current connection failed."; throw e; } // if the Exception is caused by ping connection lifetime checks, don't add to blacklist if (e.getMessage().equals(Messages.getString("Connection.exceededConnectionLifetime"))) { // only set the return Exception if it's null if (se == null) { se = e; } } else { // overwrite the return Exception no matter what se = e; if (isGlobalBlacklistEnabled()) { addToGlobalBlacklist(host); } } // take the connection out of the liveConnections Map this.liveConnections.remove(this.connectionsToHostsMap.get(conn)); } } } // if there were no successful pings if (!foundHost) { closeAllConnections(); this.isClosed = true; this.closedReason = "Connection closed due to inability to ping any active connections."; // throw the stored Exception, if exists if (se != null) { throw se; } // or create a new SQLException and throw it, must be no liveConnections ((ConnectionImpl) this.currentConnection).throwConnectionClosedException(); } }
Example #7
Source File: BestResponseTimeBalanceStrategy.java From lams with GNU General Public License v2.0 | 4 votes |
public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); SQLException ex = null; for (int attempts = 0; attempts < numRetries;) { long minResponseTime = Long.MAX_VALUE; int bestHostIndex = 0; // safety if (blackList.size() == configuredHosts.size()) { blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); } for (int i = 0; i < responseTimes.length; i++) { long candidateResponseTime = responseTimes[i]; if (candidateResponseTime < minResponseTime && !blackList.containsKey(configuredHosts.get(i))) { if (candidateResponseTime == 0) { bestHostIndex = i; break; } bestHostIndex = i; minResponseTime = candidateResponseTime; } } String bestHost = configuredHosts.get(bestHostIndex); ConnectionImpl conn = (ConnectionImpl) liveConnections.get(bestHost); if (conn == null) { try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(bestHost); } catch (SQLException sqlEx) { ex = sqlEx; if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(bestHost); blackList.put(bestHost, null); if (blackList.size() == configuredHosts.size()) { attempts++; try { Thread.sleep(250); } catch (InterruptedException e) { } blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); // try again after a little bit } continue; } throw sqlEx; } } return conn; } if (ex != null) { throw ex; } return null; // we won't get here, compiler can't tell }
Example #8
Source File: RandomBalanceStrategy.java From FoxTelem with GNU General Public License v3.0 | 4 votes |
@Override public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { int numHosts = configuredHosts.size(); SQLException ex = null; List<String> whiteList = new ArrayList<>(numHosts); whiteList.addAll(configuredHosts); Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList); for (int attempts = 0; attempts < numRetries;) { int random = (int) Math.floor((Math.random() * whiteList.size())); if (whiteList.size() == 0) { throw SQLError.createSQLException(Messages.getString("RandomBalanceStrategy.0"), null); } String hostPortSpec = whiteList.get(random); ConnectionImpl conn = (ConnectionImpl) liveConnections.get(hostPortSpec); if (conn == null) { try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(hostPortSpec); } catch (SQLException sqlEx) { ex = sqlEx; if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { Integer whiteListIndex = whiteListMap.get(hostPortSpec); // exclude this host from being picked again if (whiteListIndex != null) { whiteList.remove(whiteListIndex.intValue()); whiteListMap = this.getArrayIndexMap(whiteList); } ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(hostPortSpec); if (whiteList.size() == 0) { attempts++; try { Thread.sleep(250); } catch (InterruptedException e) { } // start fresh whiteListMap = new HashMap<>(numHosts); whiteList.addAll(configuredHosts); blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); whiteListMap = this.getArrayIndexMap(whiteList); } continue; } throw sqlEx; } } return conn; } if (ex != null) { throw ex; } return null; // we won't get here, compiler can't tell }
Example #9
Source File: RandomBalanceStrategy.java From lams with GNU General Public License v2.0 | 4 votes |
public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { int numHosts = configuredHosts.size(); SQLException ex = null; List<String> whiteList = new ArrayList<>(numHosts); whiteList.addAll(configuredHosts); Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList); for (int attempts = 0; attempts < numRetries;) { int random = (int) Math.floor((Math.random() * whiteList.size())); if (whiteList.size() == 0) { throw SQLError.createSQLException(Messages.getString("RandomBalanceStrategy.0"), null); } String hostPortSpec = whiteList.get(random); ConnectionImpl conn = (ConnectionImpl) liveConnections.get(hostPortSpec); if (conn == null) { try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(hostPortSpec); } catch (SQLException sqlEx) { ex = sqlEx; if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { Integer whiteListIndex = whiteListMap.get(hostPortSpec); // exclude this host from being picked again if (whiteListIndex != null) { whiteList.remove(whiteListIndex.intValue()); whiteListMap = this.getArrayIndexMap(whiteList); } ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(hostPortSpec); if (whiteList.size() == 0) { attempts++; try { Thread.sleep(250); } catch (InterruptedException e) { } // start fresh whiteListMap = new HashMap<>(numHosts); whiteList.addAll(configuredHosts); blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); whiteListMap = this.getArrayIndexMap(whiteList); } continue; } throw sqlEx; } } return conn; } if (ex != null) { throw ex; } return null; // we won't get here, compiler can't tell }
Example #10
Source File: LoadBalancedConnectionProxy.java From FoxTelem with GNU General Public License v3.0 | 4 votes |
/** * Pings live connections. * * @throws SQLException * if an error occurs */ @Override public synchronized void doPing() throws SQLException { SQLException se = null; boolean foundHost = false; int pingTimeout = this.currentConnection.getPropertySet().getIntegerProperty(PropertyKey.loadBalancePingTimeout).getValue(); synchronized (this) { for (HostInfo hi : this.hostsList) { String host = hi.getHostPortPair(); ConnectionImpl conn = this.liveConnections.get(host); if (conn == null) { continue; } try { if (pingTimeout == 0) { conn.ping(); } else { conn.pingInternal(true, pingTimeout); } foundHost = true; } catch (SQLException e) { // give up if it is the current connection, otherwise NPE faking resultset later. if (host.equals(this.connectionsToHostsMap.get(this.currentConnection))) { // clean up underlying connections, since connection pool won't do it closeAllConnections(); this.isClosed = true; this.closedReason = "Connection closed because ping of current connection failed."; throw e; } // if the Exception is caused by ping connection lifetime checks, don't add to blacklist if (e.getMessage().equals(Messages.getString("Connection.exceededConnectionLifetime"))) { // only set the return Exception if it's null if (se == null) { se = e; } } else { // overwrite the return Exception no matter what se = e; if (isGlobalBlacklistEnabled()) { addToGlobalBlacklist(host); } } // take the connection out of the liveConnections Map this.liveConnections.remove(this.connectionsToHostsMap.get(conn)); } } } // if there were no successful pings if (!foundHost) { closeAllConnections(); this.isClosed = true; this.closedReason = "Connection closed due to inability to ping any active connections."; // throw the stored Exception, if exists if (se != null) { throw se; } // or create a new SQLException and throw it, must be no liveConnections ((ConnectionImpl) this.currentConnection).throwConnectionClosedException(); } }
Example #11
Source File: BestResponseTimeBalanceStrategy.java From FoxTelem with GNU General Public License v3.0 | 4 votes |
@Override public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); SQLException ex = null; for (int attempts = 0; attempts < numRetries;) { long minResponseTime = Long.MAX_VALUE; int bestHostIndex = 0; // safety if (blackList.size() == configuredHosts.size()) { blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); } for (int i = 0; i < responseTimes.length; i++) { long candidateResponseTime = responseTimes[i]; if (candidateResponseTime < minResponseTime && !blackList.containsKey(configuredHosts.get(i))) { if (candidateResponseTime == 0) { bestHostIndex = i; break; } bestHostIndex = i; minResponseTime = candidateResponseTime; } } String bestHost = configuredHosts.get(bestHostIndex); ConnectionImpl conn = (ConnectionImpl) liveConnections.get(bestHost); if (conn == null) { try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(bestHost); } catch (SQLException sqlEx) { ex = sqlEx; if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) { ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(bestHost); blackList.put(bestHost, null); if (blackList.size() == configuredHosts.size()) { attempts++; try { Thread.sleep(250); } catch (InterruptedException e) { } blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); // try again after a little bit } continue; } throw sqlEx; } } return conn; } if (ex != null) { throw ex; } return null; // we won't get here, compiler can't tell }
Example #12
Source File: StaticStrategy.java From cloudstack with Apache License 2.0 | 4 votes |
@Override public JdbcConnection pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections, long[] responseTimes, int numRetries) throws SQLException { int numHosts = configuredHosts.size(); SQLException ex = null; List<String> whiteList = new ArrayList<String>(numHosts); whiteList.addAll(configuredHosts); Map<String, Long> blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList); for (int attempts = 0; attempts < numRetries;) { if (whiteList.size() == 0) { throw SQLError.createSQLException("No hosts configured", null); } String hostPortSpec = whiteList.get(0); //Always take the first host ConnectionImpl conn = (ConnectionImpl) liveConnections.get(hostPortSpec); if (conn == null) { try { conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(hostPortSpec); } catch (SQLException sqlEx) { ex = sqlEx; if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerFailover(sqlEx)) { Integer whiteListIndex = whiteListMap.get(hostPortSpec); // exclude this host from being picked again if (whiteListIndex != null) { whiteList.remove(whiteListIndex.intValue()); whiteListMap = this.getArrayIndexMap(whiteList); } ((LoadBalancedConnectionProxy) proxy).addToGlobalBlacklist(hostPortSpec); if (whiteList.size() == 0) { attempts++; try { Thread.sleep(250); } catch (InterruptedException e) { s_logger.debug("[ignored] interupted while fail over in progres."); } // start fresh whiteListMap = new HashMap<String, Integer>(numHosts); whiteList.addAll(configuredHosts); blackList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlacklist(); whiteList.removeAll(blackList.keySet()); whiteListMap = this.getArrayIndexMap(whiteList); } continue; } throw sqlEx; } } return conn; } if (ex != null) { throw ex; } return null; // we won't get here, compiler can't tell }
Example #13
Source File: LoadBalancedConnectionProxy.java From lams with GNU General Public License v2.0 | 3 votes |
/** * Creates a new physical connection for the given host:port info. If the this connection's connection URL knows about this host:port then its host info is * used, otherwise a new host info based on current connection URL defaults is spawned. * * @param hostPortPair * The host:port pair identifying the host to connect to. * @return * The new Connection instance. */ public synchronized ConnectionImpl createConnectionForHost(String hostPortPair) throws SQLException { for (HostInfo hi : this.hostsList) { if (hi.getHostPortPair().equals(hostPortPair)) { return createConnectionForHost(hi); } } return null; }
Example #14
Source File: LoadBalancedConnectionProxy.java From FoxTelem with GNU General Public License v3.0 | 3 votes |
/** * Creates a new physical connection for the given host:port info. If the this connection's connection URL knows about this host:port then its host info is * used, otherwise a new host info based on current connection URL defaults is spawned. * * @param hostPortPair * The host:port pair identifying the host to connect to. * @return * The new Connection instance. * @throws SQLException * if an error occurs */ public synchronized ConnectionImpl createConnectionForHost(String hostPortPair) throws SQLException { for (HostInfo hi : this.hostsList) { if (hi.getHostPortPair().equals(hostPortPair)) { return createConnectionForHost(hi); } } return null; }
Example #15
Source File: FailoverConnectionProxy.java From lams with GNU General Public License v2.0 | 2 votes |
/** * Creates a new connection instance for host pointed out by the given host index. * * @param hostIndex * The host index in the global hosts list. * @return * The new connection instance. */ synchronized ConnectionImpl createConnectionForHostIndex(int hostIndex) throws SQLException { return createConnectionForHost(this.hostsList.get(hostIndex)); }
Example #16
Source File: MultiHostConnectionProxy.java From FoxTelem with GNU General Public License v3.0 | 2 votes |
/** * Creates a new physical connection for the given {@link HostInfo}. * * @param hostInfo * The host info instance. * @return * The new Connection instance. * @throws SQLException * if an error occurs */ synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException { ConnectionImpl conn = (ConnectionImpl) ConnectionImpl.getInstance(hostInfo); conn.setProxy(getProxy()); return conn; }
Example #17
Source File: FailoverConnectionProxy.java From FoxTelem with GNU General Public License v3.0 | 2 votes |
/** * Creates a new connection instance for host pointed out by the given host index. * * @param hostIndex * The host index in the global hosts list. * @return * The new connection instance. * @throws SQLException * if an error occurs */ synchronized ConnectionImpl createConnectionForHostIndex(int hostIndex) throws SQLException { return createConnectionForHost(this.hostsList.get(hostIndex)); }
Example #18
Source File: MultiHostConnectionProxy.java From lams with GNU General Public License v2.0 | 2 votes |
/** * Creates a new physical connection for the given {@link HostInfo}. * * @param hostInfo * The host info instance. * @return * The new Connection instance. */ synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException { ConnectionImpl conn = (ConnectionImpl) ConnectionImpl.getInstance(hostInfo); conn.setProxy(getProxy()); return conn; }