Top Description Inners Fields Constructors Methods
org.springframework.jdbc.datasource.embedded

public Class EmbeddedDatabaseFactory

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean
Imports
java.io.PrintWriter, java.sql.Connection, .SQLException, java.util.UUID, java.util.logging.Logger, javax.sql.DataSource, org.apache.commons.logging.Log, .LogFactory, org.springframework.jdbc.datasource.SimpleDriverDataSource, org.springframework.jdbc.datasource.init.DatabasePopulator, .DatabasePopulatorUtils, org.springframework.lang.Nullable, org.springframework.util.Assert

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.

Authors
Keith Donald, Juergen Hoeller, Sam Brannen
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private EmbeddedDatabaseConfigurer
private String
private DatabasePopulator
private DataSource
private DataSourceFactory
public static final String
DEFAULT_DATABASE_NAME

Default name for an embedded database: testdb.

private boolean
private static final Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public EmbeddedDatabase
getDatabase()

Factory method that returns the embedded database instance, which is also a DataSource.

protected final DataSource
getDataSource()

Hook that gets the DataSource that provides the connectivity to the embedded database.

protected void
initDatabase()

Hook to initialize the embedded database.

public void
setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer)

Set the strategy that will be used to configure the embedded database instance.

public void
setDatabaseName(String
name of the embedded database
databaseName
)

Set the name of the database.

public void
setDatabasePopulator(DatabasePopulator populator)

Set the strategy that will be used to initialize or populate the embedded database.

public void
setDatabaseType(EmbeddedDatabaseType
the database type
type
)

Set the type of embedded database to use.

public void
setDataSourceFactory(DataSourceFactory dataSourceFactory)

Set the factory to use to create the DataSource instance that connects to the embedded database.

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.

protected void
shutdownDatabase()

Hook to shut down the embedded database.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databaseConfigurerback to summary
private EmbeddedDatabaseConfigurer databaseConfigurer
Annotations
@Nullable
databaseNameback to summary
private String databaseName
databasePopulatorback to summary
private DatabasePopulator databasePopulator
Annotations
@Nullable
dataSourceback to summary
private DataSource dataSource
Annotations
@Nullable
dataSourceFactoryback to summary
private DataSourceFactory dataSourceFactory
DEFAULT_DATABASE_NAMEback to summary
public static final String DEFAULT_DATABASE_NAME

Default name for an embedded database: testdb.

generateUniqueDatabaseNameback to summary
private boolean generateUniqueDatabaseName
loggerback to summary
private static final Log logger

Constructor Detail

EmbeddedDatabaseFactoryback to summary
public EmbeddedDatabaseFactory()

Method Detail

getDatabaseback to summary
public EmbeddedDatabase getDatabase()

Factory method that returns the embedded database instance, which is also a DataSource.

getDataSourceback to summary
protected final 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.

Annotations
@Nullable
initDatabaseback to summary
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.

setDatabaseConfigurerback to summary
public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer)

Set the strategy that will be used to configure the embedded database instance.

Call this when you wish to use an embedded database type not already supported.

setDatabaseNameback to summary
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:String

name of the embedded database

See Also
setGenerateUniqueDatabaseName
setDatabasePopulatorback to summary
public void setDatabasePopulator(DatabasePopulator populator)

Set the strategy that will be used to initialize or populate the embedded database.

Defaults to null.

setDatabaseTypeback to summary
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.

Defaults to HSQL.

Parameters
type:EmbeddedDatabaseType

the database type

setDataSourceFactoryback to summary
public void setDataSourceFactory(DataSourceFactory dataSourceFactory)

Set the factory to use to create the DataSource instance that connects to the embedded database.

Defaults to SimpleDriverDataSourceFactory.

setGenerateUniqueDatabaseNameback to summary
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.

Since
4.2
See Also
setDatabaseName
shutdownDatabaseback to summary
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.

org.springframework.jdbc.datasource.embedded back to summary

private Class EmbeddedDatabaseFactory.EmbeddedDataSourceProxy

extends Object
implements EmbeddedDatabase
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.EmbeddedDataSourceProxy
All Implemented Interfaces
org.springframework.jdbc.datasource.embedded.EmbeddedDatabase, javax.sql.DataSource, java.sql.Wrapper, javax.sql.CommonDataSource

Field Summary

Modifier and TypeField and Description
private final DataSource

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Connection
getConnection()

Implements javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

public Connection
getConnection(String
the database user on whose behalf the connection is being made
username
,
String
the user's password
password
)

Implements javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

public int
getLoginTimeout()

Implements javax.sql.DataSource.getLoginTimeout.

Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.

public PrintWriter
getLogWriter()

Implements javax.sql.DataSource.getLogWriter.

Retrieves the log writer for this DataSource object.

public Logger
getParentLogger()

Implements javax.sql.CommonDataSource.getParentLogger.

Return the parent Logger of all the Loggers used by this data source.

public boolean
isWrapperFor(Class<?>
a Class defining an interface.
iface
)

Implements java.sql.Wrapper.isWrapperFor.

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.

public void
setLoginTimeout(int
the data source login time limit
seconds
)

Implements javax.sql.DataSource.setLoginTimeout.

Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.

public void
setLogWriter(PrintWriter
the new log writer; to disable logging, set to null
out
)

Implements javax.sql.DataSource.setLogWriter.

Sets the log writer for this DataSource object to the given java.io.PrintWriter object.

public void
public <T> T
unwrap(Class<T>
A Class defining an interface that the result must implement.
iface
)

Implements java.sql.Wrapper.unwrap.

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataSourceback to summary
private final DataSource dataSource

Constructor Detail

EmbeddedDataSourceProxyback to summary
public EmbeddedDataSourceProxy(DataSource dataSource)

Method Detail

getConnectionback to summary
public Connection getConnection() throws SQLException

Implements javax.sql.DataSource.getConnection.

Doc from javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

Returns:Connection

a connection to the data source

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs
getConnectionback to summary
public Connection getConnection(String username, String password) throws SQLException

Implements javax.sql.DataSource.getConnection.

Doc from javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

Parameters
username:String

the database user on whose behalf the connection is being made

password:String

the user's password

Returns:Connection

a connection to the data source

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs
getLoginTimeoutback to summary
public int getLoginTimeout() throws SQLException

Implements javax.sql.DataSource.getLoginTimeout.

Doc from javax.sql.DataSource.getLoginTimeout.

Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A value of zero means that the timeout is the default system timeout if there is one; otherwise, it means that there is no timeout. When a DataSource object is created, the login timeout is initially zero.

Returns:int

Doc from javax.sql.CommonDataSource.getLoginTimeout.

the data source login time limit

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.CommonDataSource.getLoginTimeout.

if a database access error occurs.

getLogWriterback to summary
public PrintWriter getLogWriter() throws SQLException

Implements javax.sql.DataSource.getLogWriter.

Doc from javax.sql.DataSource.getLogWriter.

Retrieves the log writer for this DataSource object.

The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source specific log writer are not printed to the log writer associated with the java.sql.DriverManager class. When a DataSource object is created, the log writer is initially null; in other words, the default is for logging to be disabled.

Returns:PrintWriter

Doc from javax.sql.CommonDataSource.getLogWriter.

the log writer for this data source or null if logging is disabled

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.CommonDataSource.getLogWriter.

if a database access error occurs

getParentLoggerback to summary
public Logger getParentLogger()

Implements javax.sql.CommonDataSource.getParentLogger.

Doc from javax.sql.CommonDataSource.getParentLogger.

Return the parent Logger of all the Loggers used by this data source. This should be the Logger farthest from the root Logger that is still an ancestor of all of the Loggers used by this data source. Configuring this Logger will affect all of the log messages generated by the data source. In the worst case, this may be the root Logger.

Returns:Logger

the parent Logger for this data source

Annotations
@Override
isWrapperForback to summary
public boolean isWrapperFor(Class<?> iface) throws SQLException

Implements java.sql.Wrapper.isWrapperFor.

Doc from java.sql.Wrapper.isWrapperFor.

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.

Parameters
iface:Class<?>

a Class defining an interface.

Returns:boolean

true if this implements the interface or directly or indirectly wraps an object that does.

Annotations
@Override
Exceptions
SQLException:
if an error occurs while determining whether this is a wrapper for an object with the given interface.
setLoginTimeoutback to summary
public void setLoginTimeout(int seconds) throws SQLException

Implements javax.sql.DataSource.setLoginTimeout.

Doc from javax.sql.DataSource.setLoginTimeout.

Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A value of zero specifies that the timeout is the default system timeout if there is one; otherwise, it specifies that there is no timeout. When a DataSource object is created, the login timeout is initially zero.

Parameters
seconds:int

Doc from javax.sql.CommonDataSource.setLoginTimeout.

the data source login time limit

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.CommonDataSource.setLoginTimeout.

if a database access error occurs.

setLogWriterback to summary
public void setLogWriter(PrintWriter out) throws SQLException

Implements javax.sql.DataSource.setLogWriter.

Doc from javax.sql.DataSource.setLogWriter.

Sets the log writer for this DataSource object to the given java.io.PrintWriter object.

The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source- specific log writer are not printed to the log writer associated with the java.sql.DriverManager class. When a DataSource object is created the log writer is initially null; in other words, the default is for logging to be disabled.

Parameters
out:PrintWriter

Doc from javax.sql.CommonDataSource.setLogWriter.

the new log writer; to disable logging, set to null

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.CommonDataSource.setLogWriter.

if a database access error occurs

shutdownback to summary
public void shutdown()

Implements org.springframework.jdbc.datasource.embedded.EmbeddedDatabase.shutdown.

Doc from org.springframework.jdbc.datasource.embedded.EmbeddedDatabase.shutdown.

Shut down this embedded database.

Annotations
@Override
unwrapback to summary
public <T> T unwrap(Class<T> iface) throws SQLException

Implements java.sql.Wrapper.unwrap.

Doc from java.sql.Wrapper.unwrap.

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy. If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

Parameters
<T>
the type of the class modeled by this Class object
iface:Class<T>

A Class defining an interface that the result must implement.

Returns:T

an object that implements the interface. May be a proxy for the actual implementing object.

Annotations
@Override
Exceptions
SQLException:
If no object found that implements the interface