Package org.dalesbred

Class DatabaseSource

java.lang.Object
org.dalesbred.DatabaseSource

public final class DatabaseSource extends Object
A factory for DatabaseConnection instances, sharing a single configuration (dialect and type conversions) across all connections it creates.

Use DatabaseSource when you need explicit connection lifecycle management rather than the automatic transaction handling provided by Database. Custom type conversions registered via getTypeConversionRegistry() apply to all connections opened by this source.


 var source = new DatabaseSource(dataSource, dialect);
 source.getTypeConversionRegistry().registerConverter(...);

 try (DatabaseConnection conn = source.openConnection()) {
     conn.findAll(MyType.class, "select ...");
 }
 
See Also:
  • Constructor Details

    • DatabaseSource

      public DatabaseSource(@NotNull @NotNull DataSource dataSource, @NotNull @NotNull Dialect dialect)
      Creates a new DatabaseSource using the given data source and dialect.
  • Method Details

    • getTypeConversionRegistry

      @NotNull public @NotNull TypeConversionRegistry getTypeConversionRegistry()
      Returns the type conversion registry, which can be used to register custom type conversions that apply to all connections opened by this source.
    • openConnection

      @NotNull public @NotNull DatabaseConnection openConnection()
      Opens a new connection from the underlying data source with auto-commit disabled. The caller is responsible for closing the connection, typically via try-with-resources. Closing commits the transaction; see DatabaseConnection.close() for details.
      Throws:
      DatabaseException - if a connection cannot be obtained or configured