Package org.dalesbred

Class Database


public final class Database extends DatabaseAccess

The main abstraction of the library: represents a configured connection to database and provides a way to execute callbacks in transactions.

Usually you'll need only single instance of this in your application, unless you need to connect several different databases or need different default settings for different use cases.

  • Constructor Details

  • Method Details

    • forDataSource

      @NotNull public static @NotNull Database forDataSource(@NotNull @NotNull DataSource dataSource)
      Returns a new Database that uses given DataSource to retrieve connections.
    • forJndiDataSource

      @NotNull public static @NotNull Database forJndiDataSource(@NotNull @NotNull String jndiName)
      Returns a new Database that uses DataSource with given JNDI-name.
    • forUrlAndCredentials

      @NotNull public static @NotNull Database forUrlAndCredentials(@NotNull @NotNull String url, @Nullable @Nullable String username, @Nullable @Nullable String password)
      Returns a new Database that uses given connection options to open connection. The database opens connections directly from DriverManager without performing connection pooling.
      See Also:
    • withTransaction

      public <T> T withTransaction(@NotNull @NotNull TransactionCallback<T> callback)
      Executes a block of code within a context of a transaction, using Propagation.REQUIRED propagation.
    • withTransaction

      public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull TransactionCallback<T> callback)
      Executes a block of code with given propagation and configuration default isolation.
    • withTransaction

      public <T> T withTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback)
      Executes a block of code with given isolation.
    • withTransaction

      public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback)
      Executes a block of code with given propagation and isolation.
    • withTransaction

      public <T> T withTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull TransactionCallback<T> callback)
      Executes a block of code with given transaction settings.
      See Also:
    • withVoidTransaction

      public void withVoidTransaction(@NotNull @NotNull VoidTransactionCallback callback)
      Executes a block of code within a context of a transaction, using Propagation.REQUIRED propagation.
    • withVoidTransaction

      public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull VoidTransactionCallback callback)
      Executes a block of code with given propagation and configuration default isolation.
    • withVoidTransaction

      public void withVoidTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback)
      Executes a block of code with given isolation.
    • withVoidTransaction

      public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback)
      Executes a block of code with given propagation and isolation.
    • withVoidTransaction

      public void withVoidTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull VoidTransactionCallback callback)
      Executes a block of code with given transaction settings.
      See Also:
    • hasActiveTransaction

      public boolean hasActiveTransaction()
      Returns true if and only if the current thread has an active transaction for this database.
    • withCurrentTransaction

      protected <T> T withCurrentTransaction(@NotNull @NotNull SqlQuery query, @NotNull @NotNull TransactionCallback<T> callback)
      Executes the block of code within context of current transaction. If there's no transaction in progress throws NoActiveTransactionException unless implicit transaction are allowed: in this case, starts a new transaction.
      Specified by:
      withCurrentTransaction in class DatabaseAccess
      Throws:
      NoActiveTransactionException - if there's no active transaction.
      See Also:
    • getTypeConversionRegistry

      @NotNull public @NotNull TypeConversionRegistry getTypeConversionRegistry()
      Returns TypeConversionRegistry that can be used to register new type-conversions.
    • isAllowImplicitTransactions

      public boolean isAllowImplicitTransactions()
      Returns whether queries outside an active transaction will start a fresh transaction (true, the default) or throw NoActiveTransactionException (false).
    • setAllowImplicitTransactions

      public void setAllowImplicitTransactions(boolean allowImplicitTransactions)
      Controls whether queries outside an active transaction automatically start a fresh transaction. When set to false, calling query methods without an active transaction throws NoActiveTransactionException. Defaults to true.
    • toString

      @NotNull public @NotNull String toString()
      Returns a string containing useful debug information about the state of this object.
      Overrides:
      toString in class Object