https://github.com/isopropylcyanide/dropwizard-jdbi-unitofwork
Provides Unit of Work Support for a JDBI based Dropwizard backend
https://github.com/isopropylcyanide/dropwizard-jdbi-unitofwork
dropwizard dropwizard-jdbi jdbi jdbi-transactionality jersey-filter mavencentral unitofwork unitofwork-pattern unitofworkpattern
Last synced: 6 months ago
JSON representation
Provides Unit of Work Support for a JDBI based Dropwizard backend
- Host: GitHub
- URL: https://github.com/isopropylcyanide/dropwizard-jdbi-unitofwork
- Owner: isopropylcyanide
- License: apache-2.0
- Created: 2020-04-25T08:10:17.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-24T23:42:11.000Z (over 1 year ago)
- Last Synced: 2025-04-07T12:52:00.395Z (7 months ago)
- Topics: dropwizard, dropwizard-jdbi, jdbi, jdbi-transactionality, jersey-filter, mavencentral, unitofwork, unitofwork-pattern, unitofworkpattern
- Language: Java
- Homepage:
- Size: 252 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Dropwizard Jdbi - Unit of Work Support


Provides a Unit of Work annotation for a Jdbi backed Dropwizard service for wrapping resource methods in a transaction
context- [`Dropwizard`](https://github.com/dropwizard/dropwizard) provides a very
slick [`@UnitOfWork`](https://www.dropwizard.io/en/latest/manual/hibernate.html) 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 goodness [`Jdbi`](http://jdbi.org/jdbi2/) brings,
we had to bring the transactionality on our own. This module provides support for `JdbiUnitOfWork` with a `Jdbi`
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`:
com.github.isopropylcyanide
dropwizard-jdbi-unitofwork
1.2
## Features
- `transactionality` across multiple datasources when called from a request thread
- `transactionality` across multiple datasources across `multiple threads`
- `excluding` selectively, certain set of URI's from transaction contexts, such as `ELB`, `Health Checks` etc
- `Http 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`
- Construct a `JdbiUnitOfWorkProvider` from the DBI instance.
```java
JdbiUnitOfWorkProvider provider = JdbiUnitOfWorkProvider.withDefault(dbi); // most common
or
JdbiUnitOfWorkProvider provider = JdbiUnitOfWorkProvider.withLinked(dbi);
```If you are using Guice, you can bind the instance
```
bind(JdbiUnitOfWorkProvider.class).toInstance(provider);
```
- Provide the list of package where the SQL Objects / DAO (to be attached) are located. Classes with Jdbi
annotations `@SqlQuery` or `@SqlUpdate` or `@SqlBatch` or `@SqlCall` will be picked automatically.
Use `JdbiUnitOfWorkProvider` to generate the proxies. You can also register the classes one by one.
```java
// class level
SampleDao dao = (SampleDao) provider.getWrappedInstanceForDaoClass(SampleDao.class);
// use the proxies and pass it as they were normal instances
resource = new SampleResource(dao);
// package level
List daoPackages = Lists.newArrayList("", "fq-package-name-2", ...);
Map extends Class, Object> proxies = unitOfWorkProvider.getWrappedInstanceForDaoPackage(daoPackages);
// use the proxies and pass it as they were normal instances
resource = ...new SampleResource((SampleDao)proxies.get(SampleDao.class))
```
- Finally, we need to register the event listener with the Jersey Environment using the constructed provider
```
environment.jersey().register(new JdbiUnitOfWorkApplicationEventListener(provider, new HashSet<>()));;
```
In case you'd like to exclude certain URI paths from being monitored, you can pass them into exclude paths;
```
Set excludePaths = new HashSet<>();
environment.jersey().register(new JdbiUnitOfWorkApplicationEventListener(handleManager, excludePaths));
```
- Start annotating resource methods with `@JdbiUnitOfWork` and you're good to go.
```java
@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 as `Dao` or `SqlObjects` are proxied behind a `JdbiHandleManager`
- `JdbiHandleManager` contract specifies the `get` and `clear` of the actual handles to the calling thread.
## Support
Please file bug reports and feature requests
in [GitHub issues](https://github.com/isopropylcyanide/dropwizard-jdbi-unitofwork/issues).## License
Copyright (c) 2020-2023 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.