Java Code Examples for org.skife.jdbi.v2.DBI#open()

The following examples show how to use org.skife.jdbi.v2.DBI#open() . 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: JDBIOptionalInstantTest.java    From dropwizard-java8 with Apache License 2.0 6 votes vote down vote up
@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:date-time-optional-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE tasks (" +
                "id INT PRIMARY KEY, " +
                "assignee VARCHAR(255) NOT NULL, " +
                "start_date TIMESTAMP, " +
                "end_date TIMESTAMP, " +
                "comments VARCHAR(1024) " +
                ")");
    }
    dao = dbi.onDemand(TaskDao.class);
}
 
Example 2
Source File: JDBIOptionalLocalDateTest.java    From dropwizard-java8 with Apache License 2.0 6 votes vote down vote up
@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:date-time-optional-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE tasks (" +
                "id INT PRIMARY KEY, " +
                "assignee VARCHAR(255) NOT NULL, " +
                "start_date TIMESTAMP, " +
                "end_date TIMESTAMP, " +
                "comments VARCHAR(1024) " +
                ")");
    }
    dao = dbi.onDemand(TaskDao.class);
}
 
Example 3
Source File: TestBucketBalancer.java    From presto with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setup()
{
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dbi.registerMapper(new Distribution.Mapper(new InternalTypeManager(createTestMetadataManager())));
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);

    metadataDao = dbi.onDemand(MetadataDao.class);
    nodeManager = new TestingNodeManager(AVAILABLE_WORKERS.stream()
            .map(TestBucketBalancer::createTestingNode)
            .collect(Collectors.toList()));

    NodeSupplier nodeSupplier = nodeManager::getWorkerNodes;
    shardManager = createShardManager(dbi, nodeSupplier);
    balancer = new BucketBalancer(nodeSupplier, shardManager, true, new Duration(1, DAYS), true, true, "test");
}
 
Example 4
Source File: SqlJsonDB.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public SqlJsonDB(DBI dbi, EventBus bus, Collection<Index> indexes) {
    this.dbi = dbi;
    this.bus = bus;
    this.indexes = indexes;

    for (Index index : indexes) {
        this.indexPaths.add(index.getPath()+"/#"+index.getField());
    }

    // Lets find out the type of DB we are working with.
    try (Handle handle = dbi.open()) {
        String dbName = handle.getConnection().getMetaData().getDatabaseProductName();
        databaseKind = DatabaseKind.valueOf(dbName);
    } catch (SQLException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 5
Source File: PostgresStorageTest.java    From cassandra-reaper with Apache License 2.0 6 votes vote down vote up
@Test
public void testNodeOperations() {
  DBI dbi = new DBI(DB_URL);
  UUID reaperInstanceId = UUID.randomUUID();
  PostgresStorage storage = new PostgresStorage(reaperInstanceId, dbi);
  Assertions.assertThat(storage.isStorageConnected()).isTrue();

  Handle handle = dbi.open();
  handle.execute("DELETE from node_operations");

  storage.storeOperations("fake_cluster", OpType.OP_STREAMING, "fake_host", "data1");
  String data = storage.listOperations("fake_cluster", OpType.OP_STREAMING, "fake_host");
  Assertions.assertThat(data.equals("data1"));
  storage.storeOperations("fake_cluster", OpType.OP_STREAMING, "fake_host", "data2");

  data = storage.listOperations("fake_cluster", OpType.OP_STREAMING, "fake_host");
  Assertions.assertThat(data.equals("data2"));
}
 
Example 6
Source File: SqlExtractor.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
@Override
public void open(EtlMetrics parentMetrics) {
    this.parentMetrics = parentMetrics;
    DBI dbi = EtlJdbi.newDBI(dataSource, unknownPropertyMapper);
    handle = dbi.open();

    try (EtlProfilingScope ignored = new EtlProfilingScope(parentMetrics, "SqlExtractor.executeQuery")) {
        resultIterator = handle.createQuery(extractSql)
                .bindFromMap(extractSqlParameters)
                .mapTo(extractClass)
                .iterator();
    }
}
 
Example 7
Source File: PostgresStorageTest.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteOldNodeMetrics() throws InterruptedException {
  System.out.println("Testing metrics timeout (this will take a minute)...");
  DBI dbi = new DBI(DB_URL);
  UUID reaperInstanceId = UUID.randomUUID();
  PostgresStorage storage = new PostgresStorage(reaperInstanceId, dbi, 1, 1, 1, 1);
  Assertions.assertThat(storage.isStorageConnected()).isTrue();

  Handle handle = dbi.open();
  handle.execute("DELETE from node_metrics_v1");

  UUID runId = UUID.randomUUID();
  NodeMetrics originalNm = NodeMetrics.builder()
      .withNode("fake_node")
      .withCluster("fake_cluster")
      .withDatacenter("NYDC")
      .withHasRepairRunning(true)
      .withPendingCompactions(4)
      .withActiveAnticompactions(1)
      .build();
  storage.storeNodeMetrics(runId, originalNm);

  // first delete attempt shouldn't do anything because the entry hasn't passed its expiration time
  storage.purgeNodeMetrics();
  int numMetrics = handle.createQuery("SELECT COUNT(*) FROM node_metrics_v1")
      .mapTo(Integer.class)
      .first();
  Assertions.assertThat(numMetrics).isEqualTo(1);

  TimeUnit.SECONDS.sleep(61);

  // second delete attempt should work because entry has passed its expiration time
  storage.purgeNodeMetrics();
  numMetrics = handle.createQuery("SELECT COUNT(*) FROM node_metrics_v1")
      .mapTo(Integer.class)
      .first();
  Assertions.assertThat(numMetrics).isEqualTo(0);
}
 
Example 8
Source File: PostgresStorageTest.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenewLead() throws InterruptedException {
  DBI dbi = new DBI(DB_URL);
  UUID reaperInstanceId = UUID.randomUUID();
  PostgresStorage storage = new PostgresStorage(reaperInstanceId, dbi);
  Assertions.assertThat(storage.isStorageConnected()).isTrue();

  Handle handle = dbi.open();
  handle.execute("DELETE from leader");

  UUID leaderId = UUID.randomUUID();
  int sleepTime = 3;

  final Instant initialTime = Instant.now();
  storage.takeLead(leaderId);

  // sleep 3 seconds, then renew lead
  TimeUnit.SECONDS.sleep(sleepTime);
  Assertions.assertThat(storage.renewLead(leaderId)).isTrue();

  Instant hbTime = handle.createQuery("SELECT last_heartbeat FROM leader")
      .mapTo(Timestamp.class)
      .first()
      .toInstant();

  Duration between = Duration.between(initialTime, hbTime);
  Assertions.assertThat(between.getSeconds()).isGreaterThanOrEqualTo(sleepTime);
}
 
Example 9
Source File: PostgresStorageTest.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeLead() {
  DBI dbi = new DBI(DB_URL);
  UUID reaperInstanceId = UUID.randomUUID();
  PostgresStorage storage = new PostgresStorage(reaperInstanceId, dbi);
  Assertions.assertThat(storage.isStorageConnected()).isTrue();

  Handle handle = dbi.open();
  handle.execute("DELETE from leader");

  int numEntries = 5;
  Set<UUID> leaderIds = new HashSet<>();
  for (int i = 0; i < numEntries; i++) {
    UUID msbLeaderId = UuidUtil.fromSequenceId(UuidUtil.toSequenceId(UUID.randomUUID()));
    leaderIds.add(msbLeaderId);
  }

  // insert all five leader entries
  for (UUID leaderId : leaderIds) {
    boolean result = storage.takeLead(leaderId);
    Assertions.assertThat(result).isEqualTo(true);
  }

  // make sure fetched leaders has all the inserted leaders
  List<UUID> fetchedLeaderIds = storage.getLeaders();
  for (UUID fetchedLeaderId : fetchedLeaderIds) {
    Assertions.assertThat(leaderIds.contains(fetchedLeaderId)).isTrue();
  }
}
 
Example 10
Source File: JdbiITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
  Driver.load();
  final DBI dbi = new DBI("jdbc:h2:mem:dbi", "sa", "");
  final Handle handle = dbi.open();
  handle.execute("CREATE TABLE employer (id INTEGER)");
  handle.close();

  TestUtil.checkSpan(new ComponentSpanCount("java-jdbc", 2));
}
 
Example 11
Source File: TestShardCleaner.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup()
{
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);

    temporary = createTempDir();
    File directory = new File(temporary, "data");
    storageService = new FileStorageService(directory);
    storageService.start();

    File backupDirectory = new File(temporary, "backup");
    backupStore = new FileBackupStore(backupDirectory);
    ((FileBackupStore) backupStore).start();

    ticker = new TestingTicker();

    ShardCleanerConfig config = new ShardCleanerConfig();
    cleaner = new ShardCleaner(
            new DaoSupplier<>(dbi, H2ShardDao.class),
            "node1",
            true,
            ticker,
            storageService,
            Optional.of(backupStore),
            config.getMaxTransactionAge(),
            config.getTransactionCleanerInterval(),
            config.getLocalCleanerInterval(),
            config.getLocalCleanTime(),
            config.getBackupCleanerInterval(),
            config.getBackupCleanTime(),
            config.getBackupDeletionThreads(),
            config.getMaxCompletedTransactionAge());
}
 
Example 12
Source File: TestShardOrganizationManager.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup()
{
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dummyHandle = dbi.open();
    metadataDao = dbi.onDemand(MetadataDao.class);
    organizerDao = dbi.onDemand(ShardOrganizerDao.class);

    createTablesWithRetry(dbi);
}
 
Example 13
Source File: AlarmDAOImplTest.java    From monasca-thresh with Apache License 2.0 5 votes vote down vote up
@BeforeClass
protected void setupClass() throws Exception {
  // See class comment
  db = new DBI("jdbc:mysql://192.168.10.6/mon", "monapi", "password");
  handle = db.open();
  dao = new AlarmDAOImpl(db);
}
 
Example 14
Source File: TestShardEjector.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup()
{
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);
    shardManager = createShardManager(dbi);

    dataDir = createTempDir();
    storageService = new FileStorageService(dataDir);
    storageService.start();
}
 
Example 15
Source File: TestRaptorMetadata.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setupDatabase()
{
    TypeManager typeManager = new InternalTypeManager(createTestMetadataManager());
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dbi.registerMapper(new TableColumn.Mapper(typeManager));
    dbi.registerMapper(new Distribution.Mapper(typeManager));
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);

    NodeManager nodeManager = new TestingNodeManager();
    NodeSupplier nodeSupplier = nodeManager::getWorkerNodes;
    shardManager = createShardManager(dbi, nodeSupplier, systemTicker());
    metadata = new RaptorMetadata(dbi, shardManager);
}
 
Example 16
Source File: TestDatabaseShardManager.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup()
{
    dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);
    dataDir = Files.createTempDir();
    shardManager = createShardManager(dbi);
}
 
Example 17
Source File: VerticaRepo.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
public void setDBI(DBI dbi) throws Exception {
  this.dbi = dbi;
  this.handle = dbi.open();
}
 
Example 18
Source File: VerticaRepo.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
public VerticaRepo(DBI dbi) {
  this.dbi = dbi;
  this.handle = dbi.open();
  this.handle.execute("SET TIME ZONE TO 'UTC'");
}
 
Example 19
Source File: PostgresStorageTest.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
@Test
public void testGenericMetricExpiration() {
  DBI dbi = new DBI(DB_URL);
  UUID reaperInstanceId = UUID.randomUUID();
  PostgresStorage storage = new PostgresStorage(reaperInstanceId, dbi);
  Assertions.assertThat(storage.isStorageConnected()).isTrue();

  Handle handle = dbi.open();
  handle.execute("DELETE from node_metrics_v2");
  handle.execute("DELETE from node_metrics_v2_source_nodes");
  handle.execute("DELETE from node_metrics_v2_metric_types");


  DateTime expirationTime = DateTime.now().minusMinutes(3);
  GenericMetric expiredMetric = GenericMetric.builder()
      .withClusterName("fake_cluster")
      .withHost("fake_host1")
      .withTs(expirationTime)
      .withMetricDomain("org.apache.cassandra.metrics")
      .withMetricType("ThreadPool")
      .withMetricName("PendingTasks")
      .withMetricScope("MutationStage")
      .withMetricAttribute("fake_attribute")
      .withValue(12)
      .build();
  storage.storeMetric(expiredMetric);

  // verify that the metric was stored in the DB
  List<GenericMetric> retrievedMetrics = storage.getMetrics(
      "fake_cluster",
      Optional.empty(),
      "org.apache.cassandra.metrics",
      "ThreadPool",
      expirationTime.getMillis()
  );
  Assertions.assertThat(retrievedMetrics.size() == 1);
  List<Map<String, Object>> rs = handle.select("SELECT COUNT(*) AS count FROM node_metrics_v2_source_nodes");
  long numSourceNodes = (long) rs.get(0).get("count");
  Assertions.assertThat(numSourceNodes == 1);

  // verify that on purgeMetrics(), metric is purged since it's older than 3 minutes
  storage.purgeMetrics();
  retrievedMetrics = storage.getMetrics(
      "fake_cluster",
      Optional.empty(),
      "org.apache.cassandra.metrics",
      "ThreadPool",
      expirationTime.getMillis()
  );
  Assertions.assertThat(retrievedMetrics.size() == 0);

  // verify that source nodes have also been purged
  rs = handle.select("SELECT COUNT(*) AS count FROM node_metrics_v2_source_nodes");
  numSourceNodes = (long) rs.get(0).get("count");
  Assertions.assertThat(numSourceNodes == 1);
  Assertions.assertThat(numSourceNodes == 0);
}
 
Example 20
Source File: TestRaptorSplitManager.java    From presto with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void setup()
        throws Exception
{
    DBI dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    dbi.registerMapper(new TableColumn.Mapper(new InternalTypeManager(createTestMetadataManager())));
    dummyHandle = dbi.open();
    createTablesWithRetry(dbi);
    temporary = createTempDir();
    AssignmentLimiter assignmentLimiter = new AssignmentLimiter(ImmutableSet::of, systemTicker(), new MetadataConfig());
    shardManager = new DatabaseShardManager(dbi, new DaoSupplier<>(dbi, ShardDao.class), ImmutableSet::of, assignmentLimiter, systemTicker(), new Duration(0, MINUTES));
    TestingNodeManager nodeManager = new TestingNodeManager();
    NodeSupplier nodeSupplier = nodeManager::getWorkerNodes;

    String nodeName = UUID.randomUUID().toString();
    nodeManager.addNode(new InternalNode(nodeName, new URI("http://127.0.0.1/"), NodeVersion.UNKNOWN, false));

    RaptorConnectorId connectorId = new RaptorConnectorId("raptor");
    metadata = new RaptorMetadata(dbi, shardManager);

    metadata.createTable(SESSION, TEST_TABLE, false);
    tableHandle = metadata.getTableHandle(SESSION, TEST_TABLE.getTable());

    List<ShardInfo> shards = ImmutableList.<ShardInfo>builder()
            .add(shardInfo(UUID.randomUUID(), nodeName))
            .add(shardInfo(UUID.randomUUID(), nodeName))
            .add(shardInfo(UUID.randomUUID(), nodeName))
            .add(shardInfo(UUID.randomUUID(), nodeName))
            .build();

    tableId = ((RaptorTableHandle) tableHandle).getTableId();

    List<ColumnInfo> columns = metadata.getColumnHandles(SESSION, tableHandle).values().stream()
            .map(RaptorColumnHandle.class::cast)
            .map(ColumnInfo::fromHandle)
            .collect(toList());

    long transactionId = shardManager.beginTransaction();
    shardManager.commitShards(transactionId, tableId, columns, shards, Optional.empty(), 0);

    raptorSplitManager = new RaptorSplitManager(connectorId, nodeSupplier, shardManager, false);
}