Java Code Examples for org.apache.derby.jdbc.EmbeddedDataSource

The following examples show how to use org.apache.derby.jdbc.EmbeddedDataSource. These examples are extracted from open source projects. 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
@Override
protected void messageSent() throws Exception {
   verifyExpectedBroker(inflightMessageCount);
   if (++inflightMessageCount == failureCount) {
      LOG.info("STOPPING [email protected]!!!!");
      final EmbeddedDataSource ds = ((SyncCreateDataSource) getExistingDataSource()).getDelegate();
      ds.setShutdownDatabase("shutdown");
      LOG.info("DB [email protected]!!!!");

      Thread dbRestartThread = new Thread("db-re-start-thread") {
         @Override
         public void run() {
            delayTillRestartRequired();
            ds.setShutdownDatabase("false");
            LOG.info("DB [email protected]!!!!");
         }
      };
      dbRestartThread.start();
   }
   verifyExpectedBroker(inflightMessageCount);
}
 
Example 2
Source Project: hadoop-ozone   Source File: DerbyDataSourceProvider.java    License: 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 Project: DataVec   Source File: JDBCRecordReaderTest.java    License: 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 Project: activemq-artemis   Source File: JdbcXARecoveryBrokerTest.java    License: Apache License 2.0 5 votes vote down vote up
private void stopDerby() {
   LOG.info("STOPPING [email protected]!!!!");
   final EmbeddedDataSource ds = dataSource;
   try {
      ds.setShutdownDatabase("shutdown");
      ds.getConnection();
   } catch (Exception ignored) {
   }

}
 
Example 5
Source Project: activemq-artemis   Source File: DbRestartJDBCQueueTest.java    License: 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 Project: activemq-artemis   Source File: DbRestartJDBCQueueTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void messageSent() throws Exception {
   if (++inflightMessageCount == failureCount) {
      LOG.info("STOPPING [email protected]!!!!");
      final EmbeddedDataSource ds = sharedDs;
      ds.setShutdownDatabase("shutdown");
      try {
         ds.getConnection();
      } catch (Exception ignored) {
      }
      LOG.info("DB [email protected]!!!!");

      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 [email protected]!!!!");
         }
      };
      dbRestartThread.start();
   }
}
 
Example 7
Source Project: activemq-artemis   Source File: JDBCConfigTest.java    License: 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
@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 Project: karaf-decanter   Source File: TestJdbcCollector.java    License: 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 Project: karaf-decanter   Source File: TestJdbcAppender.java    License: 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 Project: karaf-decanter   Source File: TestJdbcAppender.java    License: 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 Project: deeplearning4j   Source File: JDBCRecordReaderTest.java    License: 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 Project: activemq-artemis   Source File: SyncCreateDataSource.java    License: Apache License 2.0 4 votes vote down vote up
SyncCreateDataSource(EmbeddedDataSource dataSource) {
   this.delegate = dataSource;
}
 
Example 14
Source Project: activemq-artemis   Source File: SyncCreateDataSource.java    License: Apache License 2.0 4 votes vote down vote up
EmbeddedDataSource getDelegate() {
   return delegate;
}
 
Example 15
@Override
protected void setUp() throws Exception {
   // startup db
   sharedDs = new SyncCreateDataSource((EmbeddedDataSource) DataSourceServiceSupport.createDataSource(IOHelper.getDefaultDataDirectory()));
   super.setUp();
}
 
Example 16
Source Project: activemq-artemis   Source File: JDBCQueueMasterSlaveTest.java    License: 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();
}