Search in sources :

Example 1 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testReplicationConnectWithNoSource.

@Test
public void testReplicationConnectWithNoSource() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.retriesAllDown.getKeyName(), "3");
    props.setProperty(PropertyKey.allowSourceDownConnections.getKeyName(), "true");
    Set<String> downedHosts = new HashSet<>();
    downedHosts.add("first");
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(new String[] { "first", "second", "third" }, props, downedHosts);
    assertTrue(conn2.isReadOnly());
    assertFalse(conn2.isSourceConnection());
    try {
        conn2.createStatement().execute("SELECT 1");
    } catch (SQLException e) {
        fail("Should not fail to execute SELECT statements!");
    }
    UnreliableSocketFactory.flushAllStaticData();
    conn2.setReadOnly(false);
    assertFalse(conn2.isReadOnly());
    assertTrue(conn2.isSourceConnection());
    try {
        conn2.createStatement().execute("DROP TABLE IF EXISTS testRepTable");
        conn2.createStatement().execute("CREATE TABLE testRepTable (a INT)");
        conn2.createStatement().execute("INSERT INTO testRepTable VALUES (1)");
        conn2.createStatement().execute("DROP TABLE IF EXISTS testRepTable");
    } catch (SQLException e) {
        fail("Should not fail to execute CREATE/INSERT/DROP statements.");
    }
}
Also used : SQLException(java.sql.SQLException) Properties(java.util.Properties) HashSet(java.util.HashSet) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 2 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testBug22643.

/**
 * Test of a new feature to fix BUG 22643, specifying a "validation query" in your connection pool that starts with "slash-star ping slash-star" _exactly_
 * will cause the driver to " + instead send a ping to the server (much lighter weight), and when using a ReplicationConnection or a LoadBalancedConnection,
 * will send the ping across all active connections.
 *
 * @throws Exception
 */
@Test
public void testBug22643() throws Exception {
    checkPingQuery(this.conn);
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    Connection replConnection = getSourceReplicaReplicationConnection(props);
    try {
        checkPingQuery(replConnection);
    } finally {
        if (replConnection != null) {
            replConnection.close();
        }
    }
    Connection lbConn = getLoadBalancedConnection(props);
    try {
        checkPingQuery(lbConn);
    } finally {
        if (lbConn != null) {
            lbConn.close();
        }
    }
}
Also used : ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 3 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testBug68763.

/**
 * Tests fix for BUG#68763, ReplicationConnection.isSourceConnection() returns false always
 *
 * @throws Exception
 *             if the test fails.
 */
@Test
public void testBug68763() throws Exception {
    ReplicationConnection replConn = null;
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    replConn = (ReplicationConnection) getSourceReplicaReplicationConnection(props);
    replConn.setReadOnly(true);
    assertFalse(replConn.isSourceConnection(), "isSourceConnection() should be false for replica connection");
    replConn.setReadOnly(false);
    assertTrue(replConn.isSourceConnection(), "isSourceConnection() should be true for source connection");
}
Also used : Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 4 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testReplicationConnectionNoReplicasRemainOnSource.

/**
 * Test that we remain on the source when:
 * - the connection is not in read-only mode
 * - no replicas are configured
 * - a new replica is added
 *
 * @throws Exception
 */
@Test
public void testReplicationConnectionNoReplicasRemainOnSource() throws Exception {
    Properties props = getPropertiesFromTestsuiteUrl();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    String sourceHost = props.getProperty(PropertyKey.HOST.getKeyName()) + ":" + props.getProperty(PropertyKey.PORT.getKeyName());
    Properties props2 = getHostFreePropertiesFromTestsuiteUrl();
    props2.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props2.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    ReplicationConnection replConn = getTestReplicationConnectionNoReplicas(sourceHost, props2);
    Statement s = replConn.createStatement();
    ResultSet rs1 = s.executeQuery("select CONNECTION_ID()");
    assertTrue(rs1.next());
    int sourceConnectionId = rs1.getInt(1);
    rs1.close();
    s.close();
    // add a replica and make sure we are NOT on a new connection
    replConn.addReplicaHost(sourceHost);
    s = replConn.createStatement();
    rs1 = s.executeQuery("select CONNECTION_ID()");
    assertTrue(rs1.next());
    assertEquals(sourceConnectionId, rs1.getInt(1));
    assertFalse(replConn.isReadOnly());
    rs1.close();
    s.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 5 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project JavaSegundasQuintas by ecteruel.

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)

Aggregations

ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)67 Properties (java.util.Properties)61 Test (org.junit.jupiter.api.Test)58 Connection (java.sql.Connection)25 MysqlConnection (com.mysql.cj.MysqlConnection)24 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)24 SQLException (java.sql.SQLException)24 XAConnection (javax.sql.XAConnection)24 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)18 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)18 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)18 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)18 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)18 PreparedStatement (java.sql.PreparedStatement)18 Statement (java.sql.Statement)18 PooledConnection (javax.sql.PooledConnection)18 HostInfo (com.mysql.cj.conf.HostInfo)12 HashSet (java.util.HashSet)12 ClosedOnExpiredPasswordException (com.mysql.cj.exceptions.ClosedOnExpiredPasswordException)9 PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)9