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()));
}
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());
}
}
Aggregations