Class JdbiBundle

  • All Implemented Interfaces:
    ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBundle

    public final class JdbiBundle
    extends ru.vyarus.dropwizard.guice.module.context.unique.item.UniqueGuiceyBundle
    Bundle activates JDBI3 support. To construct bundle use static builders with jdbi or database config providers. For example: JdbiBundle.forDatabase((env, conf) -> conf.getDatabase()).

    Bundle introduce unit of work concept for JDBI. All actions must perform inside unit of work. You may use InTransaction annotation to annotate classes or methods in order to wrap logic with unit of work (and actual transaction). Annotations could be nested: in this case upper most annotation declares transaction and nested are ignored (logic executed inside upper transaction). To declare unit of work without transaction use UnitManager.

    To manually declare transaction use TransactionTemplate bean.

    Custom installations:

    • Classes annotated with JdbiRepository are installed as guice beans, but provides usual functionality as JDBI sql proxies (just no need to always combine them).
    • Classes implementing RowMapper are registered automatically.

    Only one bundle instance will be actually used (in case of multiple registrations).

    Since:
    31.08.2018
    See Also:
    for manual unit of work definition, for manual work with transactions, for sql object customization details
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <C extends io.dropwizard.Configuration>
      JdbiBundle
      forDatabase​(ConfigAwareProvider<io.dropwizard.db.PooledDataSourceFactory,​C> db)
      Builds bundle, by using only database factory from configuration.
      static <C extends io.dropwizard.Configuration>
      JdbiBundle
      forDbi​(ConfigAwareProvider<org.jdbi.v3.core.Jdbi,​C> dbi)
      Builds bundle for custom JDBI instance.
      void initialize​(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap bootstrap)  
      void run​(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyEnvironment environment)  
      JdbiBundle withConfig​(java.util.function.Consumer<org.jdbi.v3.core.Jdbi> configurer)
      Manual jdbi instance configuration.
      JdbiBundle withEagerInitialization()
      By default, repository beans (annotated with JdbiRepository) are initialized on first method call.
      JdbiBundle withPlugins​(org.jdbi.v3.core.spi.JdbiPlugin... plugins)
      Note that dropwizard registers some plugins (sql objects, guava and jodatime).
      JdbiBundle withTxAnnotations​(java.lang.Class<? extends java.lang.annotation.Annotation>... txAnnotations)
      By default, InTransaction annotation registered.
      • Methods inherited from class ru.vyarus.dropwizard.guice.module.context.unique.item.UniqueGuiceyBundle

        equals, hashCode
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • withTxAnnotations

        @SafeVarargs
        public final JdbiBundle withTxAnnotations​(java.lang.Class<? extends java.lang.annotation.Annotation>... txAnnotations)
        By default, InTransaction annotation registered. If you need to use different or more annotations provide all of them. Note, that you will need to provide InTransaction too if you want to use it too, otherwise it would not be supported.
        Parameters:
        txAnnotations - annotations to use as transaction annotations
        Returns:
        bundle instance for chained calls
      • withPlugins

        public JdbiBundle withPlugins​(org.jdbi.v3.core.spi.JdbiPlugin... plugins)
        Note that dropwizard registers some plugins (sql objects, guava and jodatime).
        Parameters:
        plugins - extra jdbi plugins to register
        Returns:
        bundle instance for chained calls
      • withConfig

        public JdbiBundle withConfig​(java.util.function.Consumer<org.jdbi.v3.core.Jdbi> configurer)
        Manual jdbi instance configuration. Configuration will be called just after jdbi object creation (on run dropwizard phase), but before guice injector creation.
        Parameters:
        configurer - configuration action
        Returns:
        bundle instance for chained calls
      • withEagerInitialization

        public JdbiBundle withEagerInitialization()
        By default, repository beans (annotated with JdbiRepository) are initialized on first method call. Lazy initialization is required to properly add all registered jdbi extensions. Also, this slightly speed up startup.

        This option will enable eager repositories initialization after application startup. It may be important if execution time of first method call is important (e.g. due to some metrics).

        Returns:
        bundle instance for chained calls
      • initialize

        public void initialize​(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap bootstrap)
      • run

        public void run​(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyEnvironment environment)
      • forDbi

        public static <C extends io.dropwizard.Configuration> JdbiBundle forDbi​(ConfigAwareProvider<org.jdbi.v3.core.Jdbi,​C> dbi)
        Builds bundle for custom JDBI instance.
        Type Parameters:
        C - configuration type
        Parameters:
        dbi - JDBI instance provider
        Returns:
        bundle instance
      • forDatabase

        public static <C extends io.dropwizard.Configuration> JdbiBundle forDatabase​(ConfigAwareProvider<io.dropwizard.db.PooledDataSourceFactory,​C> db)
        Builds bundle, by using only database factory from configuration.
        Type Parameters:
        C - configuration type
        Parameters:
        db - database configuration provider
        Returns:
        bundle instance