Search in sources :

Example 1 with ClientDataSource

use of org.apache.derby.jdbc.ClientDataSource in project torodb by torodb.

the class OfficialDerbyDriver method getConfiguredDataSource.

@Override
public DataSource getConfiguredDataSource(DerbyDbBackendConfiguration configuration, String poolName) {
    DataSource dataSource;
    if (configuration.embedded()) {
        EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
        embeddedDataSource.setCreateDatabase("create");
        if (configuration.inMemory()) {
            embeddedDataSource.setDatabaseName("memory:" + configuration.getDbName());
        } else {
            embeddedDataSource.setDatabaseName(configuration.getDbName());
        }
        try (Connection connection = embeddedDataSource.getConnection()) {
            LOGGER.debug("Derby test connection has been successfully created.");
        } catch (SQLException ex) {
            throw new SystemException(ex);
        }
        embeddedDataSource.setCreateDatabase(null);
        dataSource = embeddedDataSource;
    } else {
        ClientDataSource clientDataSource = new ClientDataSource();
        clientDataSource.setServerName(configuration.getDbHost());
        clientDataSource.setPortNumber(configuration.getDbPort());
        clientDataSource.setUser(configuration.getUsername());
        clientDataSource.setPassword(configuration.getPassword());
        if (configuration.inMemory()) {
            clientDataSource.setDatabaseName("memory:" + configuration.getDbName());
        } else {
            clientDataSource.setDatabaseName(configuration.getDbName());
        }
        dataSource = clientDataSource;
    }
    if (LOGGER.isTraceEnabled()) {
        try {
            dataSource.setLogWriter(LOGGER_WRITER);
        } catch (SQLException sqlException) {
            throw new SystemException(sqlException);
        }
    }
    //TODO
    try (Connection conn = dataSource.getConnection();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("SELECT 1 FROM SYSIBM.SYSDUMMY1")) {
        rs.next();
    } catch (SQLException ex) {
        throw new SystemException(ex);
    }
    return dataSource;
}
Also used : SystemException(com.torodb.core.exceptions.SystemException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ClientDataSource(org.apache.derby.jdbc.ClientDataSource) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) DataSource(javax.sql.DataSource) ClientDataSource(org.apache.derby.jdbc.ClientDataSource)

Example 2 with ClientDataSource

use of org.apache.derby.jdbc.ClientDataSource in project beam by apache.

the class JdbcIOTest method startDatabase.

@BeforeClass
public static void startDatabase() throws Exception {
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();
    LOG.info("Starting Derby database on {}", port);
    System.setProperty("derby.stream.error.file", "target/derby.log");
    derbyServer = new NetworkServerControl(InetAddress.getByName("localhost"), port);
    StringWriter out = new StringWriter();
    derbyServer.start(new PrintWriter(out));
    boolean started = false;
    int count = 0;
    // networks where the DNS lookups are slow, this may take a little time
    while (!started && count < 30) {
        if (out.toString().contains("started")) {
            started = true;
        } else {
            count++;
            Thread.sleep(500);
            try {
                derbyServer.ping();
                started = true;
            } catch (Throwable t) {
            //ignore, still trying to start
            }
        }
    }
    dataSource = new ClientDataSource();
    dataSource.setCreateDatabase("create");
    dataSource.setDatabaseName("target/beam");
    dataSource.setServerName("localhost");
    dataSource.setPortNumber(port);
    JdbcTestDataSet.createReadDataTable(dataSource);
}
Also used : StringWriter(java.io.StringWriter) NetworkServerControl(org.apache.derby.drda.NetworkServerControl) ClientDataSource(org.apache.derby.jdbc.ClientDataSource) ServerSocket(java.net.ServerSocket) PrintWriter(java.io.PrintWriter) BeforeClass(org.junit.BeforeClass)

Aggregations

ClientDataSource (org.apache.derby.jdbc.ClientDataSource)2 SystemException (com.torodb.core.exceptions.SystemException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ServerSocket (java.net.ServerSocket)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 DataSource (javax.sql.DataSource)1 NetworkServerControl (org.apache.derby.drda.NetworkServerControl)1 EmbeddedDataSource (org.apache.derby.jdbc.EmbeddedDataSource)1 BeforeClass (org.junit.BeforeClass)1