SonicBase. In-memory embedded or distributed streaming sql database

Embedded Database Server

SonicBase can be embedded in your server. EmbeddedDatabase is a simple class that provides this functionality. There are a few methods on this class that you will need to use:

  • setUseUnsafe - set to true if you want SonicBase to use unsafe memory. Unsafe memory allows for less memory consumption, but it is more complicated to manage (you have to set the Xmx setting just right). The default if you don't set this option is to not use unsafe.
  • setDurability - pass in the path to the location you want data stored on disk. If this method is not called then SonicBase will operate in an in-memory only mode.
  • purge - deletes all persisted data.
  • start - starts the embedded database. You must call setUsafeUnsafe, setDurability and optionally purge before calling start.
  • createDatabaseIfNeeded - creates the specified database if it doesn't already exist. This call must be made after calling start.
  • getConnection - returns a standard jdbc connection for the specified database. This call must be made after calling start.
  • shutdown - call this when you are done using the database.

  • To use the embedded database you need to include the SonicBase JDBC jar and the SonicBase Server jar in your project. To do this, include the following in your maven project:
    
    <dependency>
        <groupId>com.sonicbase</groupId>
        <artifactId>sonicbase-jdbc</artifactId>
        <version>[version]</version>
    </dependency>
    
    <dependency>
        <groupId>com.sonicbase</groupId>
        <artifactId>sonicbase-server</artifactId>
        <version>[version]</version>
    </dependency>
            

    Example

    
    EmbeddedDatabase db = new EmbeddedDatabase();
    try {
      db.setUseUnsafe(false);
      db.setDurability(System.getProperty("user.home") + "/db-data.embedded");
      db.purge();
      db.start();
    
      db.createDatabaseIfNeeded("db");
    
      try (Connection conn = db.getConnection("db")) {
        try (PreparedStatement stmt = conn.prepareStatement("create table Persons (id BIGINT, socialSecurityNumber VARCHAR(20), gender VARCHAR(8), PRIMARY KEY (id))")) {
          stmt.executeUpdate();
        }
    
        for (int i = 0; i < 10; i++) {
          try (PreparedStatement stmt = conn.prepareStatement("insert into persons (id, socialSecurityNumber, gender) VALUES (?, ?, ?)")) {
            stmt.setLong(1, i);
            stmt.setString(2, "933-28-" + i);
            stmt.setString(3, "m");
            stmt.executeUpdate();
          }
        }
    
        try (PreparedStatement stmt = conn.prepareStatement("select * from persons where id < 2")) {
          try (ResultSet rs = stmt.executeQuery()) {
            rs.next();
            System.out.println("personId=" + rs.getLong("id"));
            rs.next();
            System.out.println("personId=" + rs.getLong("id"));
          }
        }
      }
    }
    finally {
      db.shutdown();
    }