Java Code Examples for org.apache.derby.jdbc.ClientDataSource#setDatabaseName()

The following examples show how to use org.apache.derby.jdbc.ClientDataSource#setDatabaseName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcDatasetRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startDatabase() throws Exception {
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();

    JDBC_URL = "jdbc:derby://localhost:" + port + "/target/tcomp";

    System.setProperty("derby.stream.error.file", "target/derby.log");

    derbyServer = new NetworkServerControl(InetAddress.getByName("localhost"), port);
    derbyServer.start(null);

    dataSource = new ClientDataSource();
    dataSource.setCreateDatabase("create");
    dataSource.setDatabaseName("target/tcomp");
    dataSource.setServerName("localhost");
    dataSource.setPortNumber(port);

    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("create table " + TABLE_IN + "(id INT, name VARCHAR(500))");
            statement.executeUpdate("create table " + TABLE_OUT + "(id INT, name VARCHAR(500))");
        }
    }
}
 
Example 2
Source File: 919148_ReplicationRun_0_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
void _verifyDatabase(String serverHost, 
        int serverPort,
        String dbPath,
        int _noTuplesInserted)
    throws SQLException
{
    util.DEBUG("_verifyDatabase: "+serverHost+":"+serverPort+"/"+dbPath);
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(dbPath);
    ds.setServerName(serverHost);
    ds.setPortNumber(serverPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
    
    _verify(conn,_noTuplesInserted);
    
    conn.close();
}
 
Example 3
Source File: 919148_ReplicationRun_0_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
void waitForConnect(long sleepTime, int tries,
        String fullDbPath, 
        String serverHost, int serverPort)
    throws Exception
{
    int count = 0;
    String msg = null;
    while ( count++ <= tries )
    {
        try
        {
            ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
            ds.setDatabaseName(fullDbPath);
            ds.setServerName(serverHost);
            ds.setPortNumber(serverPort);
            ds.setConnectionAttributes(useEncryption(false));
            Connection conn = ds.getConnection();
            util.DEBUG("Got connection after " 
                    + (count-1) +" * "+ sleepTime + " ms.");
            conn.close();
            return;
        }
        catch ( SQLException se )
        {
            msg = se.getErrorCode() + "' '" + se.getSQLState()
                    + "' '" + se.getMessage();
            util.DEBUG(count  + " got '" + msg +"'.");
            Thread.sleep(sleepTime); // ms. Sleep and try again...
        }
    }        
    assertTrue(msg + ": Could NOT connect in "
            + tries+"*"+sleepTime + "ms.",false);
}
 
Example 4
Source File: 919148_ReplicationRun_0_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
void verifySlave()
throws Exception
{
    util.DEBUG("BEGIN verifySlave "+slaveServerHost+":"
            +slaveServerPort+"/"+slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb);
    
    if ( (replicationTest != null) // If 'replicationTest==null' no table was created/filled
            && simpleLoad )
    {
        _verifyDatabase(slaveServerHost, slaveServerPort, 
                slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb,
                simpleLoadTuples);
        // return;
    }
 
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(slaveDatabasePath + FS + slaveDbSubPath + FS +
                       replicatedDb);
    ds.setServerName(slaveServerHost);
    ds.setPortNumber(slaveServerPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
        
    simpleVerify(conn);
    conn.close();
    /*
    runSlaveVerificationCLient(jvmVersion,
            testClientHost,
            replicatedDb,
            slaveServerHost, slaveServerPort);
    */
    util.DEBUG("END   verifySlave");
}
 
Example 5
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 5 votes vote down vote up
void _testInsertUpdateDeleteOnMaster(String serverHost, 
        int serverPort,
        String dbPath,
        int _noTuplesToInsert)
    throws SQLException
{
    util.DEBUG("_testInsertUpdateDeleteOnMaster: " + serverHost + ":" +
               serverPort + "/" + dbPath + " " + _noTuplesToInsert);
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(dbPath);
    ds.setServerName(serverHost);
    ds.setPortNumber(serverPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
    
    PreparedStatement ps = conn.prepareStatement("create table t(i integer primary key, s varchar(64))");
    
    ps.execute();
    
    ps = conn.prepareStatement("insert into t values (?,?)");
    for (int i = 0; i< _noTuplesToInsert; i++)
    {
        ps.setInt(1,i);
        ps.setString(2,"dilldall"+i);
        ps.execute();
        if ( (i % 10000) == 0 ) conn.commit();
    }
    
    _verify(conn, _noTuplesToInsert);
    
    conn.close();
}
 
Example 6
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 5 votes vote down vote up
void verifyMaster()
throws Exception
{
    util.DEBUG("BEGIN verifyMaster " + masterServerHost + ":"
            +masterServerPort+"/"+masterDatabasePath+FS+masterDbSubPath+FS+replicatedDb);
    
    if ( (replicationTest != null)  // If 'replicationTest==null' no table was created/filled
            && simpleLoad )
    {
        _verifyDatabase(masterServerHost, masterServerPort, 
                masterDatabasePath+FS+masterDbSubPath+FS+replicatedDb,
                simpleLoadTuples);
        // return;
    }
 
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(masterDatabasePath + FS + masterDbSubPath + FS +
                       replicatedDb);
    ds.setServerName(masterServerHost);
    ds.setPortNumber(masterServerPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
        
    simpleVerify(conn);
    conn.close();
    /*
    runMasterVerificationCLient(jvmVersion,
            testClientHost,
            replicatedDb,
            masterServerHost, masterServerPort);
    */
    util.DEBUG("END   verifyMaster");
}
 
Example 7
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 5 votes vote down vote up
void verifySlave()
throws Exception
{
    util.DEBUG("BEGIN verifySlave "+slaveServerHost+":"
            +slaveServerPort+"/"+slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb);
    
    if ( (replicationTest != null) // If 'replicationTest==null' no table was created/filled
            && simpleLoad )
    {
        _verifyDatabase(slaveServerHost, slaveServerPort, 
                slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb,
                simpleLoadTuples);
        // return;
    }
 
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(slaveDatabasePath + FS + slaveDbSubPath + FS +
                       replicatedDb);
    ds.setServerName(slaveServerHost);
    ds.setPortNumber(slaveServerPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
        
    simpleVerify(conn);
    conn.close();
    /*
    runSlaveVerificationCLient(jvmVersion,
            testClientHost,
            replicatedDb,
            slaveServerHost, slaveServerPort);
    */
    util.DEBUG("END   verifySlave");
}
 
Example 8
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 5 votes vote down vote up
void waitForConnect(long sleepTime, int tries,
        String fullDbPath, 
        String serverHost, int serverPort)
    throws Exception
{
    int count = 0;
    String msg = null;
    while ( count++ <= tries )
    {
        try
        {
            ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
            ds.setDatabaseName(fullDbPath);
            ds.setServerName(serverHost);
            ds.setPortNumber(serverPort);
            ds.setConnectionAttributes(useEncryption(false));
            Connection conn = ds.getConnection();
            util.DEBUG("Got connection after " 
                    + (count-1) +" * "+ sleepTime + " ms.");
            conn.close();
            return;
        }
        catch ( SQLException se )
        {
            msg = se.getErrorCode() + "' '" + se.getSQLState()
                    + "' '" + se.getMessage();
            util.DEBUG(count  + " got '" + msg +"'.");
            Thread.sleep(sleepTime); // ms. Sleep and try again...
        }
    }        
    assertTrue(msg + ": Could NOT connect in "
            + tries+"*"+sleepTime + "ms.",false);
}
 
Example 9
Source File: 919148_ReplicationRun_0_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
void verifySlave()
throws Exception
{
    util.DEBUG("BEGIN verifySlave "+slaveServerHost+":"
            +slaveServerPort+"/"+slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb);
    
    if ( (replicationTest != null) // If 'replicationTest==null' no table was created/filled
            && simpleLoad )
    {
        _verifyDatabase(slaveServerHost, slaveServerPort, 
                slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb,
                simpleLoadTuples);
        // return;
    }
 
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(slaveDatabasePath + FS + slaveDbSubPath + FS +
                       replicatedDb);
    ds.setServerName(slaveServerHost);
    ds.setPortNumber(slaveServerPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
        
    simpleVerify(conn);
    conn.close();
    /*
    runSlaveVerificationCLient(jvmVersion,
            testClientHost,
            replicatedDb,
            slaveServerHost, slaveServerPort);
    */
    util.DEBUG("END   verifySlave");
}
 
Example 10
Source File: 919148_ReplicationRun_0_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
void verifyMaster()
throws Exception
{
    util.DEBUG("BEGIN verifyMaster " + masterServerHost + ":"
            +masterServerPort+"/"+masterDatabasePath+FS+masterDbSubPath+FS+replicatedDb);
    
    if ( (replicationTest != null)  // If 'replicationTest==null' no table was created/filled
            && simpleLoad )
    {
        _verifyDatabase(masterServerHost, masterServerPort, 
                masterDatabasePath+FS+masterDbSubPath+FS+replicatedDb,
                simpleLoadTuples);
        // return;
    }
 
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(masterDatabasePath + FS + masterDbSubPath + FS +
                       replicatedDb);
    ds.setServerName(masterServerHost);
    ds.setPortNumber(masterServerPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
        
    simpleVerify(conn);
    conn.close();
    /*
    runMasterVerificationCLient(jvmVersion,
            testClientHost,
            replicatedDb,
            masterServerHost, masterServerPort);
    */
    util.DEBUG("END   verifyMaster");
}
 
Example 11
Source File: 919148_ReplicationRun_0_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
void _testInsertUpdateDeleteOnMaster(String serverHost, 
        int serverPort,
        String dbPath,
        int _noTuplesToInsert)
    throws SQLException
{
    util.DEBUG("_testInsertUpdateDeleteOnMaster: " + serverHost + ":" +
               serverPort + "/" + dbPath + " " + _noTuplesToInsert);
    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
    ds.setDatabaseName(dbPath);
    ds.setServerName(serverHost);
    ds.setPortNumber(serverPort);
    ds.setConnectionAttributes(useEncryption(false));
    Connection conn = ds.getConnection();
    
    PreparedStatement ps = conn.prepareStatement("create table t(i integer primary key, s varchar(64))");
    
    ps.execute();
    
    ps = conn.prepareStatement("insert into t values (?,?)");
    for (int i = 0; i< _noTuplesToInsert; i++)
    {
        ps.setInt(1,i);
        ps.setString(2,"dilldall"+i);
        ps.execute();
        if ( (i % 10000) == 0 ) conn.commit();
    }
    
    _verify(conn, _noTuplesToInsert);
    
    conn.close();
}
 
Example 12
Source File: DynamicJdbcIOTest.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void startDatabase() throws Exception {
  ServerSocket socket = new ServerSocket(0);
  port = socket.getLocalPort();
  socket.close();

  LOG.info("Starting Derby database on {}", port);

  // by default, derby uses a lock timeout of 60 seconds. In order to speed up the test
  // and detect the lock faster, we decrease this timeout
  System.setProperty("derby.locks.waitTimeout", "2");
  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;
  // Use two different methods to detect when server is started:
  // 1) Check the server stdout for the "started" string
  // 2) wait up to 15 seconds for the derby server to start based on a ping
  // on faster machines and networks, this may return very quick, but on slower
  // 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++;
      TimeUnit.MILLISECONDS.sleep(500);
      try {
        derbyServer.ping();
        started = true;
      } catch (Throwable t) {
        // ignore, still trying to start
      }
    }
  }

  if (!started) {
    // Server has not started in the expected time frame
    throw new IllegalStateException("Derby server failed to start.");
  }

  dataSource = new ClientDataSource();
  dataSource.setCreateDatabase("create");
  dataSource.setDatabaseName("target/beam");
  dataSource.setServerName("localhost");
  dataSource.setPortNumber(port);

  readTableName = getTestTableName("UT_READ");

  createTable(dataSource, readTableName);
  addInitialData(dataSource, readTableName);
}
 
Example 13
Source File: 919148_ReplicationRun_0_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 4 votes vote down vote up
void waitForSQLState(String expectedState, 
        long sleepTime, int tries,
        String fullDbPath, 
        String serverHost, int serverPort)
    throws Exception
{
    int count = 0;
    String msg = null;
    while ( count++ <= tries )
    {
        try
        {
            ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
            ds.setDatabaseName(fullDbPath);
            ds.setServerName(serverHost);
            ds.setPortNumber(serverPort);
            ds.setConnectionAttributes(useEncryption(false));
            Connection conn = ds.getConnection();
            // Should never get here!
            conn.close();
            assertTrue("Expected SQLState'"+expectedState
                        + "', but got connection!",
                    false);
        }
        catch ( SQLException se )
        {
            int errCode = se.getErrorCode();
            msg = se.getMessage();
            String sState = se.getSQLState();
            msg = "'" + errCode + "' '" + sState + "' '" + msg +"'";
            util.DEBUG(count 
                    + ": SQLState expected '"+expectedState+"'," +
                    " got " + msg);
            if ( sState.equals(expectedState) )
            {
                util.DEBUG("Reached SQLState '" + expectedState +"' in "
                        + (count-1)+"*"+sleepTime + "ms.");
                return; // Got desired SQLState.
            }
            else
            {
                Thread.sleep(sleepTime); // ms. Sleep and try again...
            }
        }
        
    }
    assertTrue(msg + ": SQLState '"+expectedState+"' was not reached in "
            + tries+"*"+sleepTime + "ms.",false);
    
}
 
Example 14
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 4 votes vote down vote up
void waitForSQLState(String expectedState, 
        long sleepTime, int tries,
        String fullDbPath, 
        String serverHost, int serverPort)
    throws Exception
{
    int count = 0;
    String msg = null;
    while ( count++ <= tries )
    {
        try
        {
            ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
            ds.setDatabaseName(fullDbPath);
            ds.setServerName(serverHost);
            ds.setPortNumber(serverPort);
            ds.setConnectionAttributes(useEncryption(false));
            Connection conn = ds.getConnection();
            // Should never get here!
            conn.close();
            assertTrue("Expected SQLState'"+expectedState
                        + "', but got connection!",
                    false);
        }
        catch ( SQLException se )
        {
            int errCode = se.getErrorCode();
            msg = se.getMessage();
            String sState = se.getSQLState();
            msg = "'" + errCode + "' '" + sState + "' '" + msg +"'";
            util.DEBUG(count 
                    + ": SQLState expected '"+expectedState+"'," +
                    " got " + msg);
            if ( sState.equals(expectedState) )
            {
                util.DEBUG("Reached SQLState '" + expectedState +"' in "
                        + (count-1)+"*"+sleepTime + "ms.");
                return; // Got desired SQLState.
            }
            else
            {
                Thread.sleep(sleepTime); // ms. Sleep and try again...
            }
        }
        
    }
    assertTrue(msg + ": SQLState '"+expectedState+"' was not reached in "
            + tries+"*"+sleepTime + "ms.",false);
    
}
 
Example 15
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 4 votes vote down vote up
private void startMaster_direct(String dbName,
        String masterHost,  // Where the master db is run.
        int masterServerPort, // master server interface accepting client requests
        
        String slaveReplInterface, // slaveHost,
        int slaveReplPort)
        throws Exception
{
    
    String URL = masterURL(dbName)
            +";startMaster=true;slaveHost="+slaveReplInterface
            +";slavePort="+slaveReplPort;
            
        util.DEBUG("startMaster_direct getConnection("+URL+")");
        Connection conn = null;
        boolean done = false;
        int count = 0;
        while ( !done )
        {
            try
            {
                /* On 1.5 locking of Drivermanager.class prevents
                 * using DriverManager.getConnection() concurrently
                 * in startMaster and startSlave!
                Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
                conn = DriverManager.getConnection(URL);
                 */
                ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
                ds.setDatabaseName(masterDatabasePath+FS+masterDbSubPath+FS+dbName);
                ds.setServerName(masterHost);
                ds.setPortNumber(masterServerPort);
                ds.setConnectionAttributes("startMaster=true"
                        +";slaveHost="+slaveReplInterface
                        +";slavePort="+slaveReplPort
                        +useEncryption(false));
                conn = ds.getConnection();
                
                done = true;
                conn.close();
                util.DEBUG("startMaster_direct connected in " + count + " * 100ms.");
            }
            catch ( SQLException se )
            {
                int errCode = se.getErrorCode();
                String msg = se.getMessage();
                String sState = se.getSQLState();
                String expectedState = "XRE04";
                util.DEBUG("startMaster Got SQLException: " 
                        + errCode + " " + sState + " " + msg + ". Expected " + expectedState);
                if ( (errCode == -1)
                && (sState.equalsIgnoreCase(expectedState) ) )
                {
                    util.DEBUG("Not ready to startMaster. "
                            +"Beware: Will also report "
                            + "'... got a fatal error for database '...../<dbname>'"
                            + " in master derby.log.");
                    Thread.sleep(100L); // ms.
                }
                else
                {
                    if (REPLICATION_MASTER_TIMED_OUT.equals(sState)) // FIXME! CANNOT_START_MASTER_ALREADY_BOOTED
                    {
                        util.DEBUG("Master already started?");
                    }
                    util.DEBUG("startMaster_direct Got: "
                            +state+" Expected "+expectedState);
                    throw se;
                }
            }
 
            assertTrue("startMaster did not succeed.", count++ < 1200); // 1200*100ms = 120s = 2mins.
        }
        util.DEBUG("startMaster_direct exit.");
}
 
Example 16
Source File: 919148_ReplicationRun_0_t.java    From coming with MIT License 4 votes vote down vote up
private void startSlave_direct(String dbName,
        String slaveHost,  // Where the slave db is run.
        int slaveServerPort, // slave server interface accepting client requests
        String slaveReplInterface,
        int slaveReplPort)
        throws Exception
{
    final String URL = slaveURL(dbName)
            +";startSlave=true;slaveHost="+slaveReplInterface
            +";slavePort="+slaveReplPort;
    
        util.DEBUG("startSlave_direct getConnection("+URL+")");
        
        final String fDbPath = slaveDatabasePath+FS+slaveDbSubPath+FS+dbName;
        final String fSlaveHost = slaveHost;
        final int fSlaveServerPort = slaveServerPort;
        final String fConnAttrs = "startSlave=true"
                            +";slaveHost="+slaveReplInterface
                            +";slavePort="+slaveReplPort
                            +useEncryption(false);
        Thread connThread = new Thread(
                new Runnable()
        {
            public void run()
            {
                startSlaveException = null;
                Connection conn = null;
                String expectedState = "XRE08";
                try {
                    // NB! WIll hang here until startMaster is executed!
                    /*On 1.5 locking of Drivermanager.class prevents
                     * using DriverManager.getConnection() concurrently
                     * in startMaster and startSlave!
                    Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
                    conn = DriverManager.getConnection(URL);
                     */
                    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
                    ds.setDatabaseName(fDbPath);
                    ds.setServerName(fSlaveHost);
                    ds.setPortNumber(fSlaveServerPort);
                    ds.setConnectionAttributes(fConnAttrs);
                    conn = ds.getConnection();
                    conn.close();
                }
                catch (SQLException se)
                {
                    startSlaveException = se;
                }
                catch (Exception ex)
                {
                    startSlaveException = ex;
                }
            }
        }
        );
        connThread.start();
        util.DEBUG("startSlave_direct exit.");
}
 
Example 17
Source File: 919148_ReplicationRun_0_s.java    From coming with MIT License 4 votes vote down vote up
private void startSlave_direct(String dbName,
        String slaveHost,  // Where the slave db is run.
        int slaveServerPort, // slave server interface accepting client requests
        String slaveReplInterface,
        int slaveReplPort)
        throws Exception
{
    final String URL = slaveURL(dbName)
            +";startSlave=true;slaveHost="+slaveReplInterface
            +";slavePort="+slaveReplPort;
    
        util.DEBUG("startSlave_direct getConnection("+URL+")");
        
        final String fDbPath = slaveDatabasePath+FS+slaveDbSubPath+FS+dbName;
        final String fSlaveHost = slaveHost;
        final int fSlaveServerPort = slaveServerPort;
        final String fConnAttrs = "startSlave=true"
                            +";slaveHost="+slaveReplInterface
                            +";slavePort="+slaveReplPort
                            +useEncryption(false);
        Thread connThread = new Thread(
                new Runnable()
        {
            public void run()
            {
                startSlaveException = null;
                Connection conn = null;
                String expectedState = "XRE08";
                try {
                    // NB! WIll hang here until startMaster is executed!
                    /*On 1.5 locking of Drivermanager.class prevents
                     * using DriverManager.getConnection() concurrently
                     * in startMaster and startSlave!
                    Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
                    conn = DriverManager.getConnection(URL);
                     */
                    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
                    ds.setDatabaseName(fDbPath);
                    ds.setServerName(fSlaveHost);
                    ds.setPortNumber(fSlaveServerPort);
                    ds.setConnectionAttributes(fConnAttrs);
                    conn = ds.getConnection();
                    conn.close();
                }
                catch (SQLException se)
                {
                    startSlaveException = se;
                }
                catch (Exception ex)
                {
                    startSlaveException = ex;
                }
            }
        }
        );
        connThread.start();
        util.DEBUG("startSlave_direct exit.");
}
 
Example 18
Source File: 919148_ReplicationRun_0_s.java    From coming with MIT License 4 votes vote down vote up
private void startMaster_direct(String dbName,
        String masterHost,  // Where the master db is run.
        int masterServerPort, // master server interface accepting client requests
        
        String slaveReplInterface, // slaveHost,
        int slaveReplPort)
        throws Exception
{
    
    String URL = masterURL(dbName)
            +";startMaster=true;slaveHost="+slaveReplInterface
            +";slavePort="+slaveReplPort;
            
        util.DEBUG("startMaster_direct getConnection("+URL+")");
        Connection conn = null;
        boolean done = false;
        int count = 0;
        while ( !done )
        {
            try
            {
                /* On 1.5 locking of Drivermanager.class prevents
                 * using DriverManager.getConnection() concurrently
                 * in startMaster and startSlave!
                Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
                conn = DriverManager.getConnection(URL);
                 */
                ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
                ds.setDatabaseName(masterDatabasePath+FS+masterDbSubPath+FS+dbName);
                ds.setServerName(masterHost);
                ds.setPortNumber(masterServerPort);
                ds.setConnectionAttributes("startMaster=true"
                        +";slaveHost="+slaveReplInterface
                        +";slavePort="+slaveReplPort
                        +useEncryption(false));
                conn = ds.getConnection();
                
                done = true;
                conn.close();
                util.DEBUG("startMaster_direct connected in " + count + " * 100ms.");
            }
            catch ( SQLException se )
            {
                int errCode = se.getErrorCode();
                String msg = se.getMessage();
                String sState = se.getSQLState();
                String expectedState = "XRE04";
                util.DEBUG("startMaster Got SQLException: " 
                        + errCode + " " + sState + " " + msg + ". Expected " + expectedState);
                if ( (errCode == -1)
                && (sState.equalsIgnoreCase(expectedState) ) )
                {
                    util.DEBUG("Not ready to startMaster. "
                            +"Beware: Will also report "
                            + "'... got a fatal error for database '...../<dbname>'"
                            + " in master derby.log.");
                    Thread.sleep(100L); // ms.
                }
                else
                {
                    if (REPLICATION_MASTER_TIMED_OUT.equals(sState)) // FIXME! CANNOT_START_MASTER_ALREADY_BOOTED
                    {
                        util.DEBUG("Master already started?");
                    }
                    util.DEBUG("startMaster_direct Got: "
                            +state+" Expected "+expectedState);
                    throw se;
                }
            }
 
            assertTrue("startMaster did not succeed.", count++ < 1200); // 1200*100ms = 120s = 2mins.
        }
        util.DEBUG("startMaster_direct exit.");
}
 
Example 19
Source File: 919148_ReplicationRun_0_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 4 votes vote down vote up
private void startSlave_direct(String dbName,
        String slaveHost,  // Where the slave db is run.
        int slaveServerPort, // slave server interface accepting client requests
        String slaveReplInterface,
        int slaveReplPort)
        throws Exception
{
    final String URL = slaveURL(dbName)
            +";startSlave=true;slaveHost="+slaveReplInterface
            +";slavePort="+slaveReplPort;
    
        util.DEBUG("startSlave_direct getConnection("+URL+")");
        
        final String fDbPath = slaveDatabasePath+FS+slaveDbSubPath+FS+dbName;
        final String fSlaveHost = slaveHost;
        final int fSlaveServerPort = slaveServerPort;
        final String fConnAttrs = "startSlave=true"
                            +";slaveHost="+slaveReplInterface
                            +";slavePort="+slaveReplPort
                            +useEncryption(false);
        Thread connThread = new Thread(
                new Runnable()
        {
            public void run()
            {
                startSlaveException = null;
                Connection conn = null;
                String expectedState = "XRE08";
                try {
                    // NB! WIll hang here until startMaster is executed!
                    /*On 1.5 locking of Drivermanager.class prevents
                     * using DriverManager.getConnection() concurrently
                     * in startMaster and startSlave!
                    Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
                    conn = DriverManager.getConnection(URL);
                     */
                    ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
                    ds.setDatabaseName(fDbPath);
                    ds.setServerName(fSlaveHost);
                    ds.setPortNumber(fSlaveServerPort);
                    ds.setConnectionAttributes(fConnAttrs);
                    conn = ds.getConnection();
                    conn.close();
                }
                catch (SQLException se)
                {
                    startSlaveException = se;
                }
                catch (Exception ex)
                {
                    startSlaveException = ex;
                }
            }
        }
        );
        connThread.start();
        util.DEBUG("startSlave_direct exit.");
}
 
Example 20
Source File: JdbcIOTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  port = NetworkTestHelper.getAvailableLocalPort();
  LOG.info("Starting Derby database on {}", port);

  // by default, derby uses a lock timeout of 60 seconds. In order to speed up the test
  // and detect the lock faster, we decrease this timeout
  System.setProperty("derby.locks.waitTimeout", "2");
  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;
  // Use two different methods to detect when server is started:
  // 1) Check the server stdout for the "started" string
  // 2) wait up to 15 seconds for the derby server to start based on a ping
  // on faster machines and networks, this may return very quick, but on slower
  // 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);

  readTableName = DatabaseTestHelper.getTestTableName("UT_READ");

  DatabaseTestHelper.createTable(dataSource, readTableName);
  addInitialData(dataSource, readTableName);
}