Java Code Examples for org.apache.derby.jdbc.EmbeddedDataSource#getConnection()

The following examples show how to use org.apache.derby.jdbc.EmbeddedDataSource#getConnection() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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);
}