org.apache.derby.jdbc.EmbeddedDataSource Java Examples

The following examples show how to use org.apache.derby.jdbc.EmbeddedDataSource. 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: DbRestartJDBCQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void messageSent() throws Exception {
   verifyExpectedBroker(inflightMessageCount);
   if (++inflightMessageCount == failureCount) {
      LOG.info("STOPPING DB!@!!!!");
      final EmbeddedDataSource ds = ((SyncCreateDataSource) getExistingDataSource()).getDelegate();
      ds.setShutdownDatabase("shutdown");
      LOG.info("DB STOPPED!@!!!!");

      Thread dbRestartThread = new Thread("db-re-start-thread") {
         @Override
         public void run() {
            delayTillRestartRequired();
            ds.setShutdownDatabase("false");
            LOG.info("DB RESTARTED!@!!!!");
         }
      };
      dbRestartThread.start();
   }
   verifyExpectedBroker(inflightMessageCount);
}
 
Example #2
Source File: DerbyDataSourceProvider.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource get() {
  String jdbcUrl = configuration.getJdbcUrl();
  LOG.info("JDBC Url for Recon : {} ", jdbcUrl);
  try {
    createNewDerbyDatabase(jdbcUrl, RECON_SCHEMA_NAME);
  } catch (Exception e) {
    LOG.error("Error creating Recon Derby DB.", e);
  }
  EmbeddedDataSource dataSource = new EmbeddedDataSource();
  dataSource.setDatabaseName(jdbcUrl.split(":")[2]);
  dataSource.setUser(RECON_SCHEMA_NAME);
  return dataSource;
}
 
Example #3
Source File: JDBCRecordReaderTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName(dbName);
    dataSource.setCreateDatabase("create");
    conn = dataSource.getConnection();

    TestDb.dropTables(conn);
    TestDb.buildCoffeeTable(conn);
}
 
Example #4
Source File: JdbcXARecoveryBrokerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void stopDerby() {
   LOG.info("STOPPING DB!@!!!!");
   final EmbeddedDataSource ds = dataSource;
   try {
      ds.setShutdownDatabase("shutdown");
      ds.getConnection();
   } catch (Exception ignored) {
   }

}
 
Example #5
Source File: DbRestartJDBCQueueTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
   setAutoFail(true);
   topic = false;
   verbose = true;
   // startup db
   sharedDs = (EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory());

   broker = new BrokerService();

   DefaultIOExceptionHandler handler = new DefaultIOExceptionHandler();
   handler.setIgnoreSQLExceptions(false);
   handler.setStopStartConnectors(true);
   broker.setIoExceptionHandler(handler);
   broker.addConnector("tcp://localhost:0");
   broker.setUseJmx(false);
   broker.setPersistent(true);
   broker.setDeleteAllMessagesOnStartup(true);
   JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
   persistenceAdapter.setDataSource(sharedDs);
   persistenceAdapter.setUseLock(false);
   persistenceAdapter.setLockKeepAlivePeriod(500);
   persistenceAdapter.getLocker().setLockAcquireSleepInterval(500);
   broker.setPersistenceAdapter(persistenceAdapter);
   broker.start();
   super.setUp();
}
 
Example #6
Source File: DbRestartJDBCQueueTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void messageSent() throws Exception {
   if (++inflightMessageCount == failureCount) {
      LOG.info("STOPPING DB!@!!!!");
      final EmbeddedDataSource ds = sharedDs;
      ds.setShutdownDatabase("shutdown");
      try {
         ds.getConnection();
      } catch (Exception ignored) {
      }
      LOG.info("DB STOPPED!@!!!!");

      Thread dbRestartThread = new Thread("db-re-start-thread") {
         @Override
         public void run() {
            LOG.info("Sleeping for 10 seconds before allowing db restart");
            try {
               restartDBLatch.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            ds.setShutdownDatabase("false");
            LOG.info("DB RESTARTED!@!!!!");
         }
      };
      dbRestartThread.start();
   }
}
 
Example #7
Source File: JDBCConfigTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdbcConfig() throws Exception {
   File journalFile = new File(JOURNAL_ROOT + "testJDBCConfig/journal");
   recursiveDelete(journalFile);

   File derbyFile = new File(DERBY_ROOT + "testJDBCConfig/derbydb"); // Default
   recursiveDelete(derbyFile);

   BrokerService broker;
   broker = createBroker(new FileSystemResource(CONF_ROOT + "jdbc-example.xml"));
   try {
      assertEquals("Broker Config Error (brokerName)", "brokerJdbcConfigTest", broker.getBrokerName());

      PersistenceAdapter adapter = broker.getPersistenceAdapter();

      assertTrue("Should have created a jdbc persistence adapter", adapter instanceof JDBCPersistenceAdapter);
      assertEquals("JDBC Adapter Config Error (cleanupPeriod)", 60000, ((JDBCPersistenceAdapter) adapter).getCleanupPeriod());
      assertTrue("Should have created an EmbeddedDataSource", ((JDBCPersistenceAdapter) adapter).getDataSource() instanceof EmbeddedDataSource);
      assertTrue("Should have created a DefaultWireFormat", ((JDBCPersistenceAdapter) adapter).getWireFormat() instanceof ObjectStreamWireFormat);

      LOG.info("Success");
   } finally {
      if (broker != null) {
         broker.stop();
      }
   }
}
 
Example #8
Source File: JDBCDurableSubscriptionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected PersistenceAdapter createPersistenceAdapter() throws IOException {
   JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
   EmbeddedDataSource dataSource = new EmbeddedDataSource();
   dataSource.setDatabaseName("derbyDb");
   dataSource.setCreateDatabase("create");
   jdbc.setDataSource(dataSource);
   jdbc.setCleanupPeriod(1000); // set up small cleanup period
   return jdbc;
}
 
Example #9
Source File: TestJdbcCollector.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareBase() throws Exception {
    System.setProperty("derby.stream.error.file", "target/derby.log");
    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName("target/testDB");
    dataSource.setCreateDatabase("create");

    try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement();) {
        statement.executeUpdate("create table TEST(id integer not null, name varchar(26))");
        statement.executeUpdate("insert into TEST(id, name) values(1, 'TEST1')");
        statement.executeUpdate("insert into TEST(id, name) values(2, 'TEST2')");
    }
}
 
Example #10
Source File: TestJdbcAppender.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws SQLException {
    System.setProperty("derby.stream.error.file", "target/derby.log");
    Marshaller marshaller = new JsonMarshaller();
    EmbeddedDataSource dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName("target/testDB");
    dataSource.setCreateDatabase("create");
    
    deleteTable(dataSource);
    
    JdbcAppender appender = new JdbcAppender();
    appender.marshaller = marshaller;
    appender.dataSource = dataSource;
    Dictionary<String, Object> config = new Hashtable<>();
    config.put("dialect", "derby");
    appender.open(config);
    
    Map<String, Object> data = new HashMap<>();
    data.put(EventConstants.TIMESTAMP, TIMESTAMP);
    Event event = new Event(TOPIC, data);
    appender.handleEvent(event);

    try (Connection con = dataSource.getConnection(); Statement statement = con.createStatement();) {
        ResultSet res = statement.executeQuery("select timestamp, content from " + TABLE_NAME);
        res.next();
        long dbTimeStamp = res.getLong(1);
        String json = res.getString(2);
        JsonReader reader = Json.createReader(new StringReader(json));
        JsonObject jsonO = reader.readObject();
        Assert.assertEquals("Timestamp db", TIMESTAMP, dbTimeStamp);
        Assert.assertEquals("Timestamp string", "2016-02-02T15:59:40,634Z",jsonO.getString("@timestamp"));
        Assert.assertEquals("timestamp long", TIMESTAMP, jsonO.getJsonNumber(EventConstants.TIMESTAMP).longValue());
        Assert.assertEquals("Topic", TOPIC, jsonO.getString(EventConstants.EVENT_TOPIC.replace('.','_')));
        Assert.assertFalse(res.next());
    }
}
 
Example #11
Source File: TestJdbcAppender.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
private void deleteTable(EmbeddedDataSource dataSource) throws SQLException {
    try (Connection con = dataSource.getConnection(); Statement statement = con.createStatement();) {
        statement.execute("delete from " + TABLE_NAME);
    } catch (Exception e) {
        // Ignore
    }
}
 
Example #12
Source File: JDBCRecordReaderTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    File f = testDir.newFolder();
    System.setProperty("derby.system.home", f.getAbsolutePath());

    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName(dbName);
    dataSource.setCreateDatabase("create");
    conn = dataSource.getConnection();

    TestDb.dropTables(conn);
    TestDb.buildCoffeeTable(conn);
}
 
Example #13
Source File: SyncCreateDataSource.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
SyncCreateDataSource(EmbeddedDataSource dataSource) {
   this.delegate = dataSource;
}
 
Example #14
Source File: SyncCreateDataSource.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
EmbeddedDataSource getDelegate() {
   return delegate;
}
 
Example #15
Source File: kahaDbJdbcLeaseQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
protected void setUp() throws Exception {
   // startup db
   sharedDs = new SyncCreateDataSource((EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory()));
   super.setUp();
}
 
Example #16
Source File: JDBCQueueMasterSlaveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
protected void setUp() throws Exception {
   // startup db
   sharedDs = new SyncCreateDataSource((EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory()));
   super.setUp();
}