Giter Site home page Giter Site logo

isopropylcyanide / dropwizard-jdbi-unitofwork Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 2.0 258 KB

Provides Unit of Work Support for a JDBI based Dropwizard backend

License: Apache License 2.0

Java 100.00%
dropwizard dropwizard-jdbi jdbi unitofwork unitofworkpattern unitofwork-pattern jdbi-transactionality jersey-filter mavencentral

dropwizard-jdbi-unitofwork's Introduction

Dropwizard Jdbi - Unit of Work Support

Codecov GitHub Maven Central

Provides a Unit of Work annotation for a Jdbi backed Dropwizard service 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 goodness Jdbi 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:

<dependency>
    <groupId>com.github.isopropylcyanide</groupId>
    <artifactId>dropwizard-jdbi-unitofwork</artifactId>
    <version>1.2</version>
 </dependency>

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.

    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.

    // 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<String> daoPackages = Lists.newArrayList("<fq-package-name>", "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<String> excludePaths = new HashSet<>();
    environment.jersey().register(new JdbiUnitOfWorkApplicationEventListener(handleManager, excludePaths));
    

  • 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 as Dao or SqlObjects are proxied behind a JdbiHandleManager
  • JdbiHandleManager contract specifies the get and clear of the actual handles to the calling thread.

image

Support

Please file bug reports and feature requests in GitHub 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.

dropwizard-jdbi-unitofwork's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar isopropylcyanide avatar luigiberrettini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dropwizard-jdbi-unitofwork's Issues

JdbiUnitOfWork - 1.2 release is not found in maven

Hi @isopropylcyanide, thanks for this project. Are you continuing to maintain the project and do the new releases? The version is set to 1.2 in your examples, but the Maven public repositories don't have this version. Appreciate if there is any new release and if you can point me to it. Thank you

@JdbiUnitOfWork and @Transaction combinations?

Thanks for this great project! I have a question about combining @Transaction annotated methods in my DAO, and @JdbiUnitOfWork methods in resources (that use those methods).

Could this potentially mess up things (two different mechanisms to achieve the same thing)? Or can I keep both?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.