Java Code Examples for com.mysql.cj.protocol.ServerSession

The following examples show how to use com.mysql.cj.protocol.ServerSession. These examples are extracted from open source projects. 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 Project: lams   Source File: ExceptionFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static CJCommunicationsException createCommunicationsException(PropertySet propertySet, ServerSession serverSession, long lastPacketSentTimeMs,
        long lastPacketReceivedTimeMs, Throwable cause, ExceptionInterceptor interceptor) {
    CJCommunicationsException sqlEx = createException(CJCommunicationsException.class, null, cause, interceptor);
    sqlEx.init(propertySet, serverSession, lastPacketSentTimeMs, lastPacketReceivedTimeMs);

    // TODO: Decide whether we need to intercept exceptions at this level
    //if (interceptor != null) {
    //    @SuppressWarnings("unchecked")
    //    T interceptedEx = (T) interceptor.interceptException(sqlEx, null);
    //    if (interceptedEx != null) {
    //        return interceptedEx;
    //    }
    //}

    return sqlEx;
}
 
Example 2
Source Project: FoxTelem   Source File: ExceptionFactory.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static CJCommunicationsException createCommunicationsException(PropertySet propertySet, ServerSession serverSession,
        PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable cause, ExceptionInterceptor interceptor) {
    CJCommunicationsException sqlEx = createException(CJCommunicationsException.class, null, cause, interceptor);
    sqlEx.init(propertySet, serverSession, packetSentTimeHolder, packetReceivedTimeHolder);

    // TODO: Decide whether we need to intercept exceptions at this level
    //if (interceptor != null) {
    //    @SuppressWarnings("unchecked")
    //    T interceptedEx = (T) interceptor.interceptException(sqlEx, null);
    //    if (interceptedEx != null) {
    //        return interceptedEx;
    //    }
    //}

    return sqlEx;
}
 
Example 3
Source Project: core-ng-project   Source File: MySQLQueryInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
    boolean noIndexUsed = serverSession.noIndexUsed();
    boolean badIndexUsed = serverSession.noGoodIndexUsed();
    if (noIndexUsed || badIndexUsed) {
        boolean suppress = suppressSlowSQLWarning();
        String message = noIndexUsed ? "no index used" : "bad index used";
        String sqlValue = sql.get();
        if (suppress) {
            LOGGER.debug("{}, sql={}", message, sqlValue);
        } else {
            LOGGER.warn(errorCode("SLOW_SQL"), "{}, sql={}", message, sqlValue);
        }
    }
    return null;
}
 
Example 4
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void checkTransactionState() {
    int transState = this.serverSession.getTransactionState();
    if (transState == ServerSession.TRANSACTION_COMPLETED) {
        this.transactionManager.transactionCompleted();
    } else if (transState == ServerSession.TRANSACTION_STARTED) {
        this.transactionManager.transactionBegun();
    }
}
 
Example 5
@SuppressWarnings("unchecked")
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    // requirement of anonymous class
    final T finalResultSet = originalResultSet;

    return (T) Proxy.newProxyInstance(originalResultSet.getClass().getClassLoader(), new Class<?>[] { Resultset.class, ResultSetInternalMethods.class },
            new InvocationHandler() {

                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                    if ("equals".equals(method.getName())) {
                        // Let args[0] "unwrap" to its InvocationHandler if it is a proxy.
                        return args[0].equals(this);
                    }

                    Object invocationResult = method.invoke(finalResultSet, args);

                    String methodName = method.getName();

                    if (invocationResult != null && invocationResult instanceof String || "getString".equals(methodName) || "getObject".equals(methodName)
                            || "getObjectStoredProc".equals(methodName)) {
                        Matcher matcher = ResultSetScannerInterceptor.this.regexP.matcher(invocationResult.toString());

                        if (matcher.matches()) {
                            throw new SQLException(Messages.getString("ResultSetScannerInterceptor.2"));
                        }
                    }

                    return invocationResult;
                }
            });

}
 
Example 6
@Override
public <T extends Closeable> T performTlsHandshake(
    SocketConnection socketConnection, ServerSession serverSession) throws IOException {
  @SuppressWarnings("unchecked")
  T socket = (T) socketConnection.getMysqlSocket();
  return socket;
}
 
Example 7
@SuppressWarnings("unchecked")
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    // requirement of anonymous class
    final T finalResultSet = originalResultSet;

    return (T) Proxy.newProxyInstance(originalResultSet.getClass().getClassLoader(), new Class<?>[] { Resultset.class, ResultSetInternalMethods.class },
            new InvocationHandler() {

                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                    if ("equals".equals(method.getName())) {
                        // Let args[0] "unwrap" to its InvocationHandler if it is a proxy.
                        return args[0].equals(this);
                    }

                    Object invocationResult = method.invoke(finalResultSet, args);

                    String methodName = method.getName();

                    if (invocationResult != null && invocationResult instanceof String || "getString".equals(methodName) || "getObject".equals(methodName)
                            || "getObjectStoredProc".equals(methodName)) {
                        Matcher matcher = ResultSetScannerInterceptor.this.regexP.matcher(invocationResult.toString());

                        if (matcher.matches()) {
                            throw new SQLException(Messages.getString("ResultSetScannerInterceptor.2"));
                        }
                    }

                    return invocationResult;
                }
            });

}
 
Example 8
Source Project: FoxTelem   Source File: NativeProtocol.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void checkTransactionState() {
    int transState = this.serverSession.getTransactionState();
    if (transState == ServerSession.TRANSACTION_COMPLETED) {
        this.transactionManager.transactionCompleted();
    } else if (transState == ServerSession.TRANSACTION_STARTED) {
        this.transactionManager.transactionBegun();
    }
}
 
Example 9
Source Project: core-ng-project   Source File: MySQLQueryInterceptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void postProcess() {
    ServerSession session = mock(ServerSession.class);
    when(session.noGoodIndexUsed()).thenReturn(Boolean.TRUE);

    Database.suppressSlowSQLWarning(true);
    interceptor.postProcess(() -> "sql", null, null, session);
    Database.suppressSlowSQLWarning(false);
    assertThat(actionLog.errorCode()).isNull();

    interceptor.postProcess(() -> "sql", null, null, session);
    assertThat(actionLog.errorCode()).isEqualTo("SLOW_SQL");
}
 
Example 10
Source Project: brave   Source File: TracingQueryInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery,
  T originalResultSet, ServerSession serverSession) {
  if (interceptingExceptions && originalResultSet == null) {
    // Error case, the span will be finished in TracingExceptionInterceptor.
    return null;
  }
  Span span = ThreadLocalSpan.CURRENT_TRACER.remove();
  if (span == null || span.isNoop()) return null;

  span.finish();

  return null;
}
 
Example 11
Source Project: lams   Source File: SocketFactoryWrapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T extends Closeable> T performTlsHandshake(SocketConnection socketConnection, ServerSession serverSession) throws IOException {
    return (T) super.performTlsHandshake(socketConnection, serverSession);
}
 
Example 12
Source Project: lams   Source File: XAsyncSocketConnection.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public void performTlsHandshake(ServerSession serverSession) throws SSLParamsException, FeatureNotAvailableException, IOException {
    this.channel = this.socketFactory.performTlsHandshake(this, serverSession);
}
 
Example 13
Source Project: lams   Source File: XAuthenticationProvider.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public void connect(ServerSession serverSession, String userName, String password, String database) {
    changeUser(serverSession, userName, password, database);
}
 
Example 14
Source Project: lams   Source File: XProtocol.java    License: GNU General Public License v2.0 4 votes vote down vote up
public ServerSession getServerSession() {
    return this.serverSession;
}
 
Example 15
Source Project: lams   Source File: NoSubInterceptorWrapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
    this.underlyingInterceptor.postProcess(sql, interceptedQuery, originalResultSet, serverSession);

    return null; // don't allow result set substitution
}
 
Example 16
public CJConnectionFeatureNotAvailableException(PropertySet propertySet, ServerSession serverSession, long lastPacketSentTimeMs,
        Exception underlyingException) {
    super(underlyingException);
    init(propertySet, serverSession, lastPacketSentTimeMs, 0L);
}
 
Example 17
public void init(PropertySet propertySet, ServerSession serverSession, long lastPacketSentTimeMs, long lastPacketReceivedTimeMs) {
    this.exceptionMessage = ExceptionFactory.createLinkFailureMessageBasedOnHeuristics(propertySet, serverSession, lastPacketSentTimeMs,
            lastPacketReceivedTimeMs, getCause());
}
 
Example 18
@SuppressWarnings("resource")
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    try {
        // Don't count SETs neither SHOWs. Those are mostly used internally and must not trigger a connection switch.
        if (!this.countStatements || StringUtils.startsWithIgnoreCase(sql.get(), "SET") || StringUtils.startsWithIgnoreCase(sql.get(), "SHOW")) {
            return originalResultSet;
        }

        // Don't care if auto-commit is not enabled.
        if (!this.conn.getAutoCommit()) {
            this.matchingAfterStatementCount = 0;
            return originalResultSet;
        }

        if (this.proxy == null && this.conn.isProxySet()) {
            JdbcConnection lcl_proxy = this.conn.getMultiHostSafeProxy();
            while (lcl_proxy != null && !(lcl_proxy instanceof LoadBalancedMySQLConnection)) {
                lcl_proxy = lcl_proxy.getMultiHostSafeProxy();
            }
            if (lcl_proxy != null) {
                this.proxy = ((LoadBalancedMySQLConnection) lcl_proxy).getThisAsProxy();
            }
        }

        // Connection is not ready to rebalance yet.
        if (this.proxy == null) {
            return originalResultSet;
        }

        // Increment the match count if no regex specified, or if matches.
        if (this.matchingAfterStatementRegex == null || sql.get().matches(this.matchingAfterStatementRegex)) {
            this.matchingAfterStatementCount++;
        }

        // Trigger rebalance if count exceeds threshold.
        if (this.matchingAfterStatementCount >= this.matchingAfterStatementThreshold) {
            this.matchingAfterStatementCount = 0;
            try {
                this.proxy.pickNewConnection();
            } catch (SQLException e) {
                // eat this exception, the auto-commit statement completed, but we could not rebalance for some reason.  User may get exception when using
                // connection next.
            }
        }
    } catch (SQLException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    }
    // always return the original result set.
    return originalResultSet;
}
 
Example 19
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
    return null;
}
 
Example 20
Source Project: lams   Source File: CoreSession.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public ServerSession getServerSession() {
    return this.protocol.getServerSession();
}
 
Example 21
@SuppressWarnings("unchecked")
@Override
public <T extends Closeable> T performTlsHandshake(SocketConnection socketConnection, ServerSession serverSession) throws IOException {
    return (T) super.performTlsHandshake(socketConnection, serverSession);
}
 
Example 22
@Override
@SuppressWarnings("resource")
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    try {
        // Don't count SETs neither SHOWs. Those are mostly used internally and must not trigger a connection switch.
        if (!this.countStatements || StringUtils.startsWithIgnoreCase(sql.get(), "SET") || StringUtils.startsWithIgnoreCase(sql.get(), "SHOW")) {
            return originalResultSet;
        }

        // Don't care if auto-commit is not enabled.
        if (!this.conn.getAutoCommit()) {
            this.matchingAfterStatementCount = 0;
            return originalResultSet;
        }

        if (this.proxy == null && this.conn.isProxySet()) {
            JdbcConnection lcl_proxy = this.conn.getMultiHostSafeProxy();
            while (lcl_proxy != null && !(lcl_proxy instanceof LoadBalancedMySQLConnection)) {
                lcl_proxy = lcl_proxy.getMultiHostSafeProxy();
            }
            if (lcl_proxy != null) {
                this.proxy = ((LoadBalancedMySQLConnection) lcl_proxy).getThisAsProxy();
            }
        }

        // Connection is not ready to rebalance yet.
        if (this.proxy == null) {
            return originalResultSet;
        }

        // Increment the match count if no regex specified, or if matches.
        if (this.matchingAfterStatementRegex == null || sql.get().matches(this.matchingAfterStatementRegex)) {
            this.matchingAfterStatementCount++;
        }

        // Trigger rebalance if count exceeds threshold.
        if (this.matchingAfterStatementCount >= this.matchingAfterStatementThreshold) {
            this.matchingAfterStatementCount = 0;
            try {
                this.proxy.pickNewConnection();
            } catch (SQLException e) {
                // eat this exception, the auto-commit statement completed, but we could not rebalance for some reason.  User may get exception when using
                // connection next.
            }
        }
    } catch (SQLException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    }
    // always return the original result set.
    return originalResultSet;
}
 
Example 23
@Override
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
    return null;
}
 
Example 24
@Override
public void performTlsHandshake(ServerSession serverSession) throws SSLParamsException, FeatureNotAvailableException, IOException {
    this.channel = this.socketFactory.performTlsHandshake(this, serverSession);
}
 
Example 25
@Override
public void connect(ServerSession serverSession, String userName, String password, String database) {
    changeUser(serverSession, userName, password, database);
}
 
Example 26
Source Project: FoxTelem   Source File: XProtocol.java    License: GNU General Public License v3.0 4 votes vote down vote up
public ServerSession getServerSession() {
    return this.serverSession;
}
 
Example 27
public CJConnectionFeatureNotAvailableException(PropertySet propertySet, ServerSession serverSession, PacketSentTimeHolder packetSentTimeHolder,
        Exception underlyingException) {
    super(underlyingException);
    init(propertySet, serverSession, packetSentTimeHolder, null);
}
 
Example 28
public void init(PropertySet propertySet, ServerSession serverSession, PacketSentTimeHolder packetSentTimeHolder,
        PacketReceivedTimeHolder packetReceivedTimeHolder) {
    this.exceptionMessage = ExceptionFactory.createLinkFailureMessageBasedOnHeuristics(propertySet, serverSession, packetSentTimeHolder,
            packetReceivedTimeHolder, getCause());
}
 
Example 29
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
    this.underlyingInterceptor.postProcess(sql, interceptedQuery, originalResultSet, serverSession);

    return null; // don't allow result set substitution
}
 
Example 30
Source Project: FoxTelem   Source File: CoreSession.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public ServerSession getServerSession() {
    return this.protocol.getServerSession();
}