Class H2DatabaseFactory
- All Implemented Interfaces:
DatabaseFactory
An embedded database, in memory or in a file, runs its init scripts, Database.INIT_SCRIPTS, each once in its
life, as sa, when an instance is created for it, once per JVM until the database is closed. They are recorded in the
table CODION.INIT_SCRIPT. validateInitScripts(Connection, String...) tells whether a database would be
refused, without opening it, for a copy of a database, say.
H2's DDL commits, so a script that fails may leave part of its work behind, running again the next time.
A database on a server, or in a zip file, is not initialized, nor an existing one refusing sa without a password, its admin having one or being another: administered, its scripts run by its administrator, and a script listed for it never run.
A thread interrupted during I/O closes the file of a database opened with jdbc:h2:file:, every connection to it
failing until it is reopened. Where its threads may be interrupted, open it with jdbc:h2:async: instead, the file
being the same. Every URL to a database in a JVM must then use the same prefix.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRuns scripts using the H2 RunScript tool. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DatabaseCreates an H2 Database instancescriptRunner(String jdbcUrl) Creates a new ScriptRunner instance, using by default the sysadmin username (sa) and the default system charset.booleanstatic voidvalidateInitScripts(Connection connection, String... initScripts) Validates a database against the init scripts it would be opened with, refusing it as opening it would, seeDatabase.INIT_SCRIPTS, changing nothing, so the connection may be read only.
-
Constructor Details
-
H2DatabaseFactory
public H2DatabaseFactory()
-
-
Method Details
-
supports
- Specified by:
supportsin interfaceDatabaseFactory- Parameters:
driverClassName- the driver class name- Returns:
- true if this database factory supports the given driver
-
create
- Specified by:
createin interfaceDatabaseFactory- Parameters:
url- the jdbc url- Returns:
- a new
Databaseimplementation based on the given jdbc url.
-
database
Creates an H2 Database instance- Parameters:
url- the jdbc urlinitScripts- the init scripts, each run once in the database's life, seeDatabase.INIT_SCRIPTS- Returns:
- an H2 Database instance
-
validateInitScripts
public static void validateInitScripts(Connection connection, String... initScripts) throws SQLException Validates a database against the init scripts it would be opened with, refusing it as opening it would, seeDatabase.INIT_SCRIPTS, changing nothing, so the connection may be read only. A database without the record of its scripts is never refused, nor one opened with none listed.- Parameters:
connection- a connection to the databaseinitScripts- the init scripts it would be opened with- Throws:
DatabaseException- in case the database would be refused, or a script listed can not be readSQLException- in case the record of the scripts run can not be read
-
scriptRunner
Creates a new ScriptRunner instance, using by default the sysadmin username (sa) and the default system charset.- Parameters:
jdbcUrl- the jdbc URL- Returns:
- a new
H2DatabaseFactory.ScriptRunnerinstance
-