H2

Use the H2 database as the JobRunr StorageProvider - ideal for local development and tests.

On this page

JobRunr stores all background jobs in H2 through the H2StorageProvider. JobRunr is tested against H2 library version 2.3.232.

Note

The examples below use H2 in embedded mode, where the database file is locked by one JVM - so a second background job server cannot connect. H2’s server mode lifts that restriction.

Add the driver dependency

Add H2 to your project. The latest version is on Maven Central.

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version><!-- latest version --></version>
</dependency>

Configuration

If you use a framework starter, JobRunr automatically detects your existing DataSource - configure your database the usual way and you are done.

HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:h2:file:./data/mydb");
config.setUsername("sa");
DataSource dataSource = new HikariDataSource(config);

JobRunr.configure()
    .useStorageProvider(SqlStorageProviderFactory.using(dataSource))
    .useBackgroundJobServer()
    .initialize();
Warning

An in-memory H2 URL (e.g. jdbc:h2:mem:...) does not survive restarts and cannot be shared between JVMs. Use a file-based URL if you need persistence.

Creating the tables

By default JobRunr creates its tables automatically. If you want to create them yourself, use the DatabaseCreator or generate the SQL scripts with migration type h2. See Setting up an SQL database.

Next steps

Looking for another database? Back to the Storage overview.