Dropwizard Jdbi - Unit of Work Support
Provides @JdbiUnitOfWork
annotation for a Jdbi backed Dropwizard backend for wrapping resource methods in a transaction context
-
Dropwizard
provides a very slick@UnitOfWork
annotation that wraps a transaction context around resource methods annotated with this annotation. This is very useful for wrapping multiple calls in a single database transaction all of which will succeed or roll back atomically. -
However this support is only available for
Hibernate
. For all the goodnessJdbi
brings, we had to bring the transactionality on our own. This module provides support forJdbiUnitOfWork
with aJdbi
backend
Maven Artifacts
This project is available on Maven Central. To add it to your project you can add the following dependency to your pom.xml
:
<dependency>
<groupId>com.github.isopropylcyanide</groupId>
<artifactId>dropwizard-jdbi-unitofwork</artifactId>
<version>1.0</version>
</dependency>
Features
transactionality
across multiple datasources when called from a request threadtransactionality
across multiple datasources acrossmultiple threads
excluding
, selectively, certain set of URI's from transaction contexts, such asELB
,Health Checks
etcHttp GET
methods are excluded from transaction by default.Http POST
methods are wrapped around in a transaction only when annotated with@JdbiUnitOfWork
Usage
-
Add the dependency to your
pom.xml
-
Decide which implementation of
JdbiHandleManager
is suitable for your use case.//bind(JdbiHandleManager.class).toInstance(new RequestScopedJdbiHandleManager(dbi)); bind(JdbiHandleManager.class).toInstance(new LinkedRequestScopedJdbiHandleManager(dbi));
-
Provide the list of package where the SQL Objects (to be attached) are located. Classes with Jdbi annotations
@SqlQuery
or@SqlUpdate
will be picked automaticallySet<String> daoPackages = Sets.newHashSet("<fq-package-name>", "fq-package-name", ...);
-
Install the module in your main application module.
install(new JdbiUnitOfWorkModule(handleManager, daoPackages));
-
Start annotating resource methods with
@JdbiUnitOfWork
and you're good to go.@POST @Path("/") @JdbiUnitOfWork public RequestResponse createRequest() { ..do stateful work (across multiple Dao's) return response }
Design
- This library relies on
Jersey Monitoring Events
to bind request lifecycle with a transaction aspect - At the backend, all
Jdbi
objects such asDao
orSqlObjects
are proxied behind aJdbiHandleManager
JdbiHandleManager
contract specifies theget
andclear
of the actual handles to the calling thread.
Support
Please file bug reports and feature requests in GitHub issues.
License
Copyright (c) 2012-2020 Aman Garg
This library is licensed under the Apache License, Version 2.0.
See http://www.apache.org/licenses/LICENSE-2.0.html or the LICENSE file in this repository for the full license text.