Search in sources :

Example 1 with Log

use of com.mysql.cj.log.Log in project JavaSegundasQuintas by ecteruel.

the class NativeSession method createConfigCacheIfNeeded.

private void createConfigCacheIfNeeded(Object syncMutex) {
    synchronized (syncMutex) {
        if (this.serverConfigCache != null) {
            return;
        }
        try {
            Class<?> factoryClass = Class.forName(getPropertySet().getStringProperty(PropertyKey.serverConfigCacheFactory).getStringValue());
            @SuppressWarnings("unchecked") CacheAdapterFactory<String, Map<String, String>> cacheFactory = ((CacheAdapterFactory<String, Map<String, String>>) factoryClass.newInstance());
            this.serverConfigCache = cacheFactory.getInstance(syncMutex, this.hostInfo.getDatabaseUrl(), Integer.MAX_VALUE, Integer.MAX_VALUE);
            ExceptionInterceptor evictOnCommsError = new ExceptionInterceptor() {

                public ExceptionInterceptor init(Properties config, Log log1) {
                    return this;
                }

                public void destroy() {
                }

                @SuppressWarnings("synthetic-access")
                public Exception interceptException(Exception sqlEx) {
                    if (sqlEx instanceof SQLException && ((SQLException) sqlEx).getSQLState() != null && ((SQLException) sqlEx).getSQLState().startsWith("08")) {
                        NativeSession.this.serverConfigCache.invalidate(NativeSession.this.hostInfo.getDatabaseUrl());
                    }
                    return null;
                }
            };
            if (this.exceptionInterceptor == null) {
                this.exceptionInterceptor = evictOnCommsError;
            } else {
                ((ExceptionInterceptorChain) this.exceptionInterceptor).addRingZero(evictOnCommsError);
            }
        } catch (ClassNotFoundException e) {
            throw ExceptionFactory.createException(Messages.getString("Connection.CantFindCacheFactory", new Object[] { getPropertySet().getStringProperty(PropertyKey.parseInfoCacheFactory).getValue(), PropertyKey.parseInfoCacheFactory }), e, getExceptionInterceptor());
        } catch (InstantiationException | IllegalAccessException | CJException e) {
            throw ExceptionFactory.createException(Messages.getString("Connection.CantLoadCacheFactory", new Object[] { getPropertySet().getStringProperty(PropertyKey.parseInfoCacheFactory).getValue(), PropertyKey.parseInfoCacheFactory }), e, getExceptionInterceptor());
        }
    }
}
Also used : Log(com.mysql.cj.log.Log) SQLException(java.sql.SQLException) ExceptionInterceptor(com.mysql.cj.exceptions.ExceptionInterceptor) Properties(java.util.Properties) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) OperationCancelledException(com.mysql.cj.exceptions.OperationCancelledException) IOException(java.io.IOException) ConnectionIsClosedException(com.mysql.cj.exceptions.ConnectionIsClosedException) ExceptionInterceptorChain(com.mysql.cj.exceptions.ExceptionInterceptorChain) HashMap(java.util.HashMap) Map(java.util.Map) CJException(com.mysql.cj.exceptions.CJException)

Example 2 with Log

use of com.mysql.cj.log.Log in project aws-mysql-jdbc by awslabs.

the class NodeMonitoringConnectionPluginTest method generateNullArguments.

/**
 * Generate different sets of method arguments where one argument is null to ensure
 * {@link NodeMonitoringConnectionPlugin#NodeMonitoringConnectionPlugin(ICurrentConnectionProvider, PropertySet, IConnectionPlugin, Log)}
 * can handle null arguments correctly.
 *
 * @return different sets of arguments.
 */
private static Stream<Arguments> generateNullArguments() {
    final ConnectionProxy proxy = mock(ConnectionProxy.class);
    final ICurrentConnectionProvider currentConnectionProvider = mock(ICurrentConnectionProvider.class);
    final PropertySet set = new DefaultPropertySet();
    final Log log = new NullLogger("NodeMonitoringConnectionPluginTest");
    final IConnectionPlugin connectionPlugin = new DefaultConnectionPlugin(currentConnectionProvider, log);
    return Stream.of(Arguments.of(null, set, connectionPlugin, log), Arguments.of(proxy, null, connectionPlugin, log), Arguments.of(proxy, set, null, log), Arguments.of(proxy, set, connectionPlugin, null));
}
Also used : DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) NullLogger(com.mysql.cj.log.NullLogger) Log(com.mysql.cj.log.Log) PropertySet(com.mysql.cj.conf.PropertySet) DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) ConnectionProxy(com.mysql.cj.jdbc.ha.ConnectionProxy)

Example 3 with Log

use of com.mysql.cj.log.Log in project aws-mysql-jdbc by awslabs.

the class UtilsRegressionTest method testBug82115.

/**
 * Tests fix for Bug#82115 - Some exceptions are intercepted twice or fail to set the init cause.
 *
 * @throws Exception
 */
@Test
public void testBug82115() throws Exception {
    Exception ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), null);
    assertEquals("ORIGINAL_EXCEPTION", ex.getMessage());
    assertEquals("ORIGINAL_CAUSE", ex.getCause().getMessage());
    ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), new ExceptionInterceptor() {

        boolean alreadyIntercepted = false;

        @Override
        public ExceptionInterceptor init(Properties props, Log log) {
            this.alreadyIntercepted = false;
            return this;
        }

        public void destroy() {
        }

        @Override
        public Exception interceptException(Exception sqlEx) {
            assertFalse(this.alreadyIntercepted);
            this.alreadyIntercepted = true;
            assertEquals("ORIGINAL_EXCEPTION", sqlEx.getMessage());
            assertEquals("ORIGINAL_CAUSE", sqlEx.getCause().getMessage());
            SQLException newSqlEx = new SQLException("INTERCEPT_EXCEPTION");
            return newSqlEx;
        }
    });
    assertEquals("INTERCEPT_EXCEPTION", ex.getMessage());
    assertNull(ex.getCause());
    ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), new ExceptionInterceptor() {

        boolean alreadyIntercepted = false;

        @Override
        public ExceptionInterceptor init(Properties props, Log log) {
            this.alreadyIntercepted = false;
            return this;
        }

        public void destroy() {
        }

        @Override
        public Exception interceptException(Exception sqlEx) {
            assertFalse(this.alreadyIntercepted);
            this.alreadyIntercepted = true;
            assertEquals("ORIGINAL_EXCEPTION", sqlEx.getMessage());
            assertEquals("ORIGINAL_CAUSE", sqlEx.getCause().getMessage());
            SQLException newSqlEx = new SQLException("INTERCEPT_EXCEPTION");
            newSqlEx.initCause(new Exception("INTERCEPT_CAUSE"));
            return newSqlEx;
        }
    });
    assertEquals("INTERCEPT_EXCEPTION", ex.getMessage());
    assertEquals("INTERCEPT_CAUSE", ex.getCause().getMessage());
}
Also used : Log(com.mysql.cj.log.Log) SQLException(java.sql.SQLException) ExceptionInterceptor(com.mysql.cj.exceptions.ExceptionInterceptor) Properties(java.util.Properties) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test)

Example 4 with Log

use of com.mysql.cj.log.Log in project aws-mysql-jdbc by awslabs.

the class NativeSession method createConfigCacheIfNeeded.

private void createConfigCacheIfNeeded(Object syncMutex) {
    synchronized (syncMutex) {
        if (this.serverConfigCache != null) {
            return;
        }
        try {
            Class<?> factoryClass = Class.forName(getPropertySet().getStringProperty(PropertyKey.serverConfigCacheFactory).getStringValue());
            @SuppressWarnings("unchecked") CacheAdapterFactory<String, Map<String, String>> cacheFactory = ((CacheAdapterFactory<String, Map<String, String>>) factoryClass.newInstance());
            this.serverConfigCache = cacheFactory.getInstance(syncMutex, this.hostInfo.getDatabaseUrl(), Integer.MAX_VALUE, Integer.MAX_VALUE);
            ExceptionInterceptor evictOnCommsError = new ExceptionInterceptor() {

                public ExceptionInterceptor init(Properties config, Log log1) {
                    return this;
                }

                public void destroy() {
                }

                @SuppressWarnings("synthetic-access")
                public Exception interceptException(Exception sqlEx) {
                    if (sqlEx instanceof SQLException && ((SQLException) sqlEx).getSQLState() != null && ((SQLException) sqlEx).getSQLState().startsWith("08")) {
                        NativeSession.this.serverConfigCache.invalidate(NativeSession.this.hostInfo.getDatabaseUrl());
                    }
                    return null;
                }
            };
            if (this.exceptionInterceptor == null) {
                this.exceptionInterceptor = evictOnCommsError;
            } else {
                ((ExceptionInterceptorChain) this.exceptionInterceptor).addRingZero(evictOnCommsError);
            }
        } catch (ClassNotFoundException e) {
            throw ExceptionFactory.createException(Messages.getString("Connection.CantFindCacheFactory", new Object[] { getPropertySet().getStringProperty(PropertyKey.parseInfoCacheFactory).getValue(), PropertyKey.parseInfoCacheFactory }), e, getExceptionInterceptor());
        } catch (InstantiationException | IllegalAccessException | CJException e) {
            throw ExceptionFactory.createException(Messages.getString("Connection.CantLoadCacheFactory", new Object[] { getPropertySet().getStringProperty(PropertyKey.parseInfoCacheFactory).getValue(), PropertyKey.parseInfoCacheFactory }), e, getExceptionInterceptor());
        }
    }
}
Also used : Log(com.mysql.cj.log.Log) SQLException(java.sql.SQLException) ExceptionInterceptor(com.mysql.cj.exceptions.ExceptionInterceptor) Properties(java.util.Properties) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) OperationCancelledException(com.mysql.cj.exceptions.OperationCancelledException) IOException(java.io.IOException) ConnectionIsClosedException(com.mysql.cj.exceptions.ConnectionIsClosedException) ExceptionInterceptorChain(com.mysql.cj.exceptions.ExceptionInterceptorChain) HashMap(java.util.HashMap) Map(java.util.Map) CJException(com.mysql.cj.exceptions.CJException)

Example 5 with Log

use of com.mysql.cj.log.Log in project JavaSegundasQuintas by ecteruel.

the class UtilsRegressionTest method testBug82115.

/**
 * Tests fix for Bug#82115 - Some exceptions are intercepted twice or fail to set the init cause.
 *
 * @throws Exception
 */
@Test
public void testBug82115() throws Exception {
    Exception ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), null);
    assertEquals("ORIGINAL_EXCEPTION", ex.getMessage());
    assertEquals("ORIGINAL_CAUSE", ex.getCause().getMessage());
    ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), new ExceptionInterceptor() {

        boolean alreadyIntercepted = false;

        @Override
        public ExceptionInterceptor init(Properties props, Log log) {
            this.alreadyIntercepted = false;
            return this;
        }

        public void destroy() {
        }

        @Override
        public Exception interceptException(Exception sqlEx) {
            assertFalse(this.alreadyIntercepted);
            this.alreadyIntercepted = true;
            assertEquals("ORIGINAL_EXCEPTION", sqlEx.getMessage());
            assertEquals("ORIGINAL_CAUSE", sqlEx.getCause().getMessage());
            SQLException newSqlEx = new SQLException("INTERCEPT_EXCEPTION");
            return newSqlEx;
        }
    });
    assertEquals("INTERCEPT_EXCEPTION", ex.getMessage());
    assertNull(ex.getCause());
    ex = SQLError.createSQLException("ORIGINAL_EXCEPTION", "0", new Exception("ORIGINAL_CAUSE"), new ExceptionInterceptor() {

        boolean alreadyIntercepted = false;

        @Override
        public ExceptionInterceptor init(Properties props, Log log) {
            this.alreadyIntercepted = false;
            return this;
        }

        public void destroy() {
        }

        @Override
        public Exception interceptException(Exception sqlEx) {
            assertFalse(this.alreadyIntercepted);
            this.alreadyIntercepted = true;
            assertEquals("ORIGINAL_EXCEPTION", sqlEx.getMessage());
            assertEquals("ORIGINAL_CAUSE", sqlEx.getCause().getMessage());
            SQLException newSqlEx = new SQLException("INTERCEPT_EXCEPTION");
            newSqlEx.initCause(new Exception("INTERCEPT_CAUSE"));
            return newSqlEx;
        }
    });
    assertEquals("INTERCEPT_EXCEPTION", ex.getMessage());
    assertEquals("INTERCEPT_CAUSE", ex.getCause().getMessage());
}
Also used : Log(com.mysql.cj.log.Log) SQLException(java.sql.SQLException) ExceptionInterceptor(com.mysql.cj.exceptions.ExceptionInterceptor) Properties(java.util.Properties) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test)

Aggregations

Log (com.mysql.cj.log.Log)7 CJException (com.mysql.cj.exceptions.CJException)6 ExceptionInterceptor (com.mysql.cj.exceptions.ExceptionInterceptor)6 SQLException (java.sql.SQLException)6 Properties (java.util.Properties)6 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)3 ConnectionIsClosedException (com.mysql.cj.exceptions.ConnectionIsClosedException)3 ExceptionInterceptorChain (com.mysql.cj.exceptions.ExceptionInterceptorChain)3 OperationCancelledException (com.mysql.cj.exceptions.OperationCancelledException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 DefaultPropertySet (com.mysql.cj.conf.DefaultPropertySet)1 PropertySet (com.mysql.cj.conf.PropertySet)1 ConnectionProxy (com.mysql.cj.jdbc.ha.ConnectionProxy)1 NullLogger (com.mysql.cj.log.NullLogger)1