Search in sources :

Example 21 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method getTestReplicationConnectionNoReplicas.

/**
 * Internal method for tests to get a replication connection with a
 * single source host to the test URL.
 *
 * @param sourceHost
 * @param props
 * @return a replication connection
 * @throws Exception
 */
private ReplicationConnection getTestReplicationConnectionNoReplicas(String sourceHost, Properties props) throws Exception {
    List<HostInfo> sourceHosts = new ArrayList<>();
    sourceHosts.add(mainConnectionUrl.getHostOrSpawnIsolated(sourceHost));
    // empty
    List<HostInfo> replicaHosts = new ArrayList<>();
    Map<String, String> properties = new HashMap<>();
    props.stringPropertyNames().stream().forEach(k -> properties.put(k, props.getProperty(k)));
    ReplicationConnection replConn = ReplicationConnectionProxy.createProxyInstance(new ReplicationConnectionUrl(sourceHosts, replicaHosts, properties));
    return replConn;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HostInfo(com.mysql.cj.conf.HostInfo) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) ReplicationConnectionUrl(com.mysql.cj.conf.url.ReplicationConnectionUrl)

Example 22 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class StatementRegressionTest method testBug78961.

/**
 * Tests fix for Bug#78961 - Can't call MySQL procedure with InOut parameters in Fabric environment.
 *
 * Although this is a Fabric related bug we are able reproduce it using a couple of multi-host connections.
 *
 * @throws Exception
 */
@Test
public void testBug78961() throws Exception {
    createProcedure("testBug78961", "(IN c1 FLOAT, IN c2 FLOAT, OUT h FLOAT, INOUT t FLOAT) BEGIN SET h = SQRT(c1 * c1 + c2 * c2); SET t = t + h; END;");
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    Connection highLevelConn = getLoadBalancedConnection(props);
    assertTrue(highLevelConn.getClass().getName().startsWith("com.sun.proxy") || highLevelConn.getClass().getName().startsWith("$Proxy"));
    Connection lowLevelConn = getSourceReplicaReplicationConnection(props);
    // This simulates the behavior from Fabric connections that are causing the problem.
    ((ReplicationConnection) lowLevelConn).setProxy((JdbcConnection) highLevelConn);
    CallableStatement cstmt = lowLevelConn.prepareCall("{CALL testBug78961 (?, ?, ?, ?)}");
    cstmt.setFloat(1, 3.0f);
    cstmt.setFloat(2, 4.0f);
    cstmt.setFloat(4, 5.0f);
    cstmt.registerOutParameter(3, Types.FLOAT);
    cstmt.registerOutParameter(4, Types.FLOAT);
    cstmt.execute();
    assertEquals(5.0f, cstmt.getFloat(3));
    assertEquals(10.0f, cstmt.getFloat(4));
}
Also used : CallableStatement(java.sql.CallableStatement) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 23 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class NonRegisteringDriverTest method testReplicationProtocolReturnsReplicationConnectionProxy.

@Test
public void testReplicationProtocolReturnsReplicationConnectionProxy() throws SQLException {
    String url = "jdbc:mysql:replication://host-1:1234,host-2:1234/test";
    ReplicationConnection mockReplicationProtocolConn = Mockito.mock(ReplicationConnection.class);
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
    NonRegisteringDriver driver = new NonRegisteringDriver();
    try (MockedStatic<ReplicationConnectionProxy> mockStaticReplicationConnectionProxy = mockStatic(ReplicationConnectionProxy.class)) {
        mockStaticReplicationConnectionProxy.when(() -> ReplicationConnectionProxy.createProxyInstance(eq(connUrl))).thenReturn(mockReplicationProtocolConn);
        Connection replicationConn = driver.connect(url, new Properties());
        assertEquals(mockReplicationProtocolConn, replicationConn);
    }
}
Also used : ReplicationConnectionProxy(com.mysql.cj.jdbc.ha.ReplicationConnectionProxy) Connection(java.sql.Connection) LoadBalancedConnection(com.mysql.cj.jdbc.ha.LoadBalancedConnection) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Aggregations

ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)23 Properties (java.util.Properties)21 Test (org.junit.jupiter.api.Test)20 Connection (java.sql.Connection)9 MysqlConnection (com.mysql.cj.MysqlConnection)8 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)8 SQLException (java.sql.SQLException)8 XAConnection (javax.sql.XAConnection)8 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)6 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)6 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)6 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)6 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)6 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 PooledConnection (javax.sql.PooledConnection)6 HostInfo (com.mysql.cj.conf.HostInfo)4 HashSet (java.util.HashSet)4 ClosedOnExpiredPasswordException (com.mysql.cj.exceptions.ClosedOnExpiredPasswordException)3 PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)3