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.
NoteThe 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>
implementation 'com.h2database:h2' // version omitted, use latest
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();
datasources:
default:
url: jdbc:h2:file:./data/mydb
username: sa
password: ""
driver-class-name: org.h2.Driver
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:file:./data/mydb
quarkus.datasource.username=sa
quarkus.datasource.password=
spring.datasource.url=jdbc:h2:file:./data/mydb
spring.datasource.username=sa
spring.datasource.password=
WarningAn 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
- Keep the tables in their own schema: JobRunr can prefix every table it creates. See table prefix and schema.
- Using more than one database? Tell JobRunr which one it should use. See multiple databases in one application.
- Build the
StorageProvideryourself: Useful for an explicit table prefix,DatabaseOptions, or a provider JobRunr does not pick up automatically. See configuring the StorageProvider yourself.
Looking for another database? Back to the Storage overview.
