Search in sources :

Example 6 with MysqlConnectionPoolDataSource

use of com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource in project airlift by airlift.

the class MySqlDataSource method createConnectionInternal.

protected PooledConnection createConnectionInternal() throws SQLException {
    // attempt to get a connection from the current datasource if we have one
    SQLException lastException = null;
    if (dataSource != null) {
        try {
            return dataSource.getPooledConnection();
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // drop reference to current datasource
    dataSource = null;
    // attempt to create a connection to each mysql server (except for the one that we know is bad)
    for (ServiceDescriptor serviceDescriptor : serviceSelector.selectAllServices()) {
        // skip the current server since it is having problems
        if (serviceDescriptor.getId().equals(currentServer)) {
            continue;
        }
        // skip bogus announcements
        String jdbcUrl = serviceDescriptor.getProperties().get("jdbc");
        if (jdbcUrl == null) {
            continue;
        }
        try {
            MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
            dataSource.setUrl(jdbcUrl);
            // these are in MS *not* seconds like everything else in JDBC
            dataSource.setConnectTimeout(getMaxConnectionWaitMillis());
            dataSource.setInitialTimeout(getMaxConnectionWaitMillis());
            dataSource.setDefaultFetchSize(defaultFetchSize);
            PooledConnection connection = dataSource.getPooledConnection();
            // that worked so save the datasource and server id
            this.dataSource = dataSource;
            this.currentServer = serviceDescriptor.getId();
            return connection;
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // no servers found, clear the current server id since we no longer have a server at all
    currentServer = null;
    // throw the last exception we got
    if (lastException != null) {
        throw lastException;
    }
    throw new SQLException(String.format("No mysql servers of type '%s' available in pool '%s'", serviceSelector.getType(), serviceSelector.getPool()));
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource)

Example 7 with MysqlConnectionPoolDataSource

use of com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource in project core-util by WSO2Telco.

the class SpConfigServiceImplTest method setUpClass.

// @BeforeClass
public static void setUpClass() throws Exception {
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:/comp");
        ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");
        MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setURL("jdbc:mysql://localhost/qadbConnect");
        ds.setUser("root");
        ds.setPassword("");
        ic.bind("java:/comp/env/jdbc/CONNECT_DB", ds);
    } catch (NamingException ex) {
        System.out.println(ex.getMessage());
    }
}
Also used : NamingException(javax.naming.NamingException) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource) InitialContext(javax.naming.InitialContext)

Aggregations

MysqlConnectionPoolDataSource (com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource)7 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)3 PooledConnection (javax.sql.PooledConnection)2 JTAEnvironmentBean (com.arjuna.ats.jta.common.JTAEnvironmentBean)1 MysqlDataSource (com.mysql.jdbc.jdbc2.optional.MysqlDataSource)1 MysqlXADataSource (com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)1 ServiceDescriptor (io.airlift.discovery.client.ServiceDescriptor)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 Test (org.junit.Test)1 PGConnectionPoolDataSource (org.postgresql.ds.PGConnectionPoolDataSource)1 PGSimpleDataSource (org.postgresql.ds.PGSimpleDataSource)1 PGXADataSource (org.postgresql.xa.PGXADataSource)1 DBI (org.skife.jdbi.v2.DBI)1