Dropwizard Jdbi - Unit of Work Support
Provides @JdbiUnitOfWork annotation for a Jdbi backed Dropwizard backend for wrapping resource methods in a transaction context
-
Dropwizardprovides a very slick@UnitOfWorkannotation 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 goodnessJdbibrings, we had to bring the transactionality on our own. This module provides support forJdbiUnitOfWorkwith aJdbibackend
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
transactionalityacross multiple datasources when called from a request threadtransactionalityacross multiple datasources acrossmultiple threadsexcluding, selectively, certain set of URI's from transaction contexts, such asELB,Health ChecksetcHttp GETmethods are excluded from transaction by default.Http POSTmethods are wrapped around in a transaction only when annotated with@JdbiUnitOfWork
Usage
-
Add the dependency to your
pom.xml -
Decide which implementation of
JdbiHandleManageris 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
@SqlQueryor@SqlUpdatewill 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
@JdbiUnitOfWorkand 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 Eventsto bind request lifecycle with a transaction aspect - At the backend, all
Jdbiobjects such asDaoorSqlObjectsare proxied behind aJdbiHandleManager JdbiHandleManagercontract specifies thegetandclearof 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.
