Class EmbeddedDatabaseFactory

java.lang.Object
org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory
Direct Known Subclasses:
EmbeddedDatabaseFactoryBean

public class EmbeddedDatabaseFactory extends Object
Factory for creating an EmbeddedDatabase instance.

Callers are guaranteed that the returned database has been fully initialized and populated.

The factory can be configured as follows:

After configuring the factory, call getDatabase() to obtain a reference to the EmbeddedDatabase instance.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller, Sam Brannen, Stephane Nicoll
  • Field Details

    • DEFAULT_DATABASE_NAME

      public static final String DEFAULT_DATABASE_NAME
      Default name for an embedded database: "testdb".
      See Also:
  • Constructor Details

    • EmbeddedDatabaseFactory

      public EmbeddedDatabaseFactory()
  • Method Details

    • setGenerateUniqueDatabaseName

      public void setGenerateUniqueDatabaseName(boolean generateUniqueDatabaseName)
      Set the generateUniqueDatabaseName flag to enable or disable generation of a pseudo-random unique ID to be used as the database name.

      Setting this flag to true overrides any explicit name set via setDatabaseName(java.lang.String).

      Since:
      4.2
      See Also:
    • setDatabaseName

      public void setDatabaseName(String databaseName)
      Set the name of the database.

      Defaults to "testdb".

      Will be overridden if the generateUniqueDatabaseName flag has been set to true.

      Parameters:
      databaseName - name of the embedded database
      See Also:
    • setDataSourceFactory

      public void setDataSourceFactory(DataSourceFactory dataSourceFactory)
      Set the factory to use to create the DataSource instance that connects to the embedded database.

      Defaults to SimpleDriverDataSourceFactory.

    • setDatabaseType

      public void setDatabaseType(EmbeddedDatabaseType type)
      Set the type of embedded database to use.

      Call this when you wish to configure one of the pre-supported types with its default settings.

      Defaults to HSQL.

      Parameters:
      type - the database type
    • setDatabaseConfigurer

      public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer)
      Set the strategy that will be used to configure the embedded database instance.

      Call this with customizeConfigurer when you wish to customize the settings of one of the pre-supported types. Alternatively, use this when you wish to use an embedded database type not already supported.

      Since:
      6.2
    • setDatabasePopulator

      public void setDatabasePopulator(DatabasePopulator populator)
      Set the strategy that will be used to initialize or populate the embedded database.

      Defaults to null.

    • getDatabase

      public EmbeddedDatabase getDatabase()
      Factory method that returns the embedded database instance, which is also a DataSource.
    • initDatabase

      protected void initDatabase()
      Hook to initialize the embedded database.

      If the generateUniqueDatabaseName flag has been set to true, the current value of the database name will be overridden with an auto-generated name.

      Subclasses may call this method to force initialization; however, this method should only be invoked once.

      After calling this method, getDataSource() returns the DataSource providing connectivity to the database.

    • shutdownDatabase

      protected void shutdownDatabase()
      Hook to shut down the embedded database. Subclasses may call this method to force shutdown.

      After calling, getDataSource() returns null.

      Does nothing if no embedded database has been initialized.

    • getDataSource

      protected final @Nullable DataSource getDataSource()
      Hook that gets the DataSource that provides the connectivity to the embedded database.

      Returns null if the DataSource has not been initialized or if the database has been shut down. Subclasses may call this method to access the DataSource instance directly.