Package org.h2.engine
Class Database
java.lang.Object
org.h2.engine.Database
- All Implemented Interfaces:
CastDataProvider
,org.h2.store.DataHandler
There is one database object per open database.
The format of the meta data table is:
id int, 0, objectType int, sql varchar
- Since:
- 2004-04-15 22:49
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addDatabaseObject
(SessionLocal session, DbObject obj) Add an object to the database.void
addSchemaObject
(SessionLocal session, org.h2.schema.SchemaObject obj) Add a schema object to the database.int
Allocate a new object id.void
Flush all changes and open a new transaction log.void
Check if the simulated power failure occurred.void
Check if writing is allowed.org.h2.value.ValueTimestampTimeZone
Returns the current timestamp with maximum resolution.org.h2.util.TimeZoneProvider
Returns the current time zone.boolean
equalsIdentifiers
(String a, String b) Compare two identifiers (table names, column names,...) and verify they are equal.void
exceptionThrown
(SQLException e, String sql) This method is called after an exception occurred, to inform the database event listener (if one is set).findComment
(DbObject object) Get the comment for the given database object if one exists, or null if not.Get the role if it exists, or null if not.org.h2.schema.Schema
findSchema
(String schemaName) Get the schema if it exists, or null if not.findSetting
(String name) Get the setting if it exists, or null if not.Get the user if it exists, or null if not.findUserOrRole
(String name) Get the user or role if it exists, ornull
if not.void
flush()
Flush all pending changes to the transaction log.int
Collection<org.h2.schema.Schema>
Collection<org.h2.schema.Schema>
ArrayList<org.h2.table.TableSynonym>
Get all synonyms.ArrayList<org.h2.table.Table>
Get all tables and views.org.h2.security.auth.Authenticator
get authenticator for database usersorg.h2.value.CompareMode
Return compare mode.org.h2.util.SourceCompiler
Get the database path.org.h2.mode.DefaultNullOrdering
int
org.h2.table.Table
getDependentTable
(org.h2.schema.SchemaObject obj, org.h2.table.Table except) Get the first table that depends on this object.org.h2.table.Table
Get the first user defined table, excluding the LOB_BLOCKS table that the Recover tool creates.boolean
boolean
ArrayList<org.h2.store.InDoubtTransaction>
Get the list of in-doubt transactions.Returns the custom Java object serializer, ornull
.org.h2.table.TableLinkConnection
getLinkConnection
(String driver, String url, String user, String password) Open a new connection or get an existing connection to another database.Get the lob file list cache if it is used.org.h2.store.LobStorageInterface
Get the lob storage mechanism to use.Get the synchronization object for lob operations.int
int
org.h2.schema.Schema
Returns main schema (usually PUBLIC).int
Get the maximum length of a in-place large objectint
int
getMode()
Returns the database mode.long
long
getName()
long
long
long
boolean
int
int
boolean
boolean
long
int
org.h2.result.RowFactory
org.h2.schema.Schema
Get the schema.int
getSessions
(boolean includingSystemSession) Get all sessions that are currently connected to the database.org.h2.mvstore.db.Store
getStore()
Returns system user.getTableEngine
(String tableEngine) Get the table engine class, loading it if needed.org.h2.util.TempFileDeleter
Get the temp file deleter mechanism.getTempTableName
(String baseName, SessionLocal session) Get a unique temporary table name.org.h2.message.Trace
getTrace
(int moduleId) Get the trace object for the given module id.org.h2.message.TraceSystem
Get user with the given name.int
boolean
boolean
Check if the database is in the process of closing.boolean
Check if this database is disk-based.boolean
boolean
Check if the database is currently opening.boolean
Checks if the system table (containing the catalog) is locked.boolean
isSysTableLockedBy
(SessionLocal session) Checks if the system table (containing the catalog) is locked by the given session.boolean
lockMeta
(SessionLocal session) Lock the metadata table for updates.<V> ConcurrentHashMap<String,
V> Create a new hash map.Create a new hash map.newStringMap
(int initialCapacity) Create a new hash map.org.h2.store.FileStore
Open a file at the given location.int
readLob
(long lobId, byte[] hmac, long offset, byte[] buff, int off, int length) Read from a lob.void
releaseDatabaseObjectIds
(BitSet idsToRelease) Mark some database ids as unused.void
removeDatabaseObject
(SessionLocal session, DbObject obj) Remove the object from the database.void
removeMeta
(SessionLocal session, int id) Remove the given object from the meta data.void
removeSchemaObject
(SessionLocal session, org.h2.schema.SchemaObject obj) Remove an object from the system table.void
removeSession
(SessionLocal session) Remove a session.void
renameDatabaseObject
(SessionLocal session, DbObject obj, String newName) Rename a database object.void
renameSchemaObject
(SessionLocal session, org.h2.schema.SchemaObject obj, String newName) Rename a schema object.void
setAllowBuiltinAliasOverride
(boolean b) void
setAllowLiterals
(int value) void
setAuthenticator
(org.h2.security.auth.Authenticator authenticator) Set current database authenticatorvoid
setBackgroundException
(org.h2.message.DbException e) void
setCacheSize
(int kb) void
setCloseDelay
(int value) void
setCluster
(String cluster) void
setCompactMode
(int compactMode) void
setCompareMode
(org.h2.value.CompareMode compareMode) void
setDefaultNullOrdering
(org.h2.mode.DefaultNullOrdering defaultNullOrdering) void
setDefaultTableType
(int defaultTableType) void
setDeleteFilesOnDisconnect
(boolean b) void
setEventListener
(DatabaseEventListener eventListener) void
setEventListenerClass
(String className) boolean
setExclusiveSession
(SessionLocal session, boolean closeOthers) Set the session that can exclusively access the database.void
setIgnoreCase
(boolean b) void
setIgnoreCatalogs
(boolean b) static void
setInitialPowerOffCount
(int count) void
setJavaObjectSerializerName
(String serializerName) void
setLockMode
(int lockMode) void
setMasterUser
(User user) void
setMaxLengthInplaceLob
(int value) void
setMaxMemoryRows
(int value) void
setMaxOperationMemory
(int maxOperationMemory) void
void
setOptimizeReuseResults
(boolean b) void
setPowerOffCount
(int count) void
setProgress
(int state, String name, long x, long max) Set the progress of a long running operation.void
setQueryStatistics
(boolean b) void
setQueryStatisticsMaxEntries
(int n) void
setReadOnly
(boolean readOnly) Switch the database to read-only mode.void
setReferentialIntegrity
(boolean b) void
setRetentionTime
(int value) void
setRowFactory
(org.h2.result.RowFactory rowFactory) void
setWriteDelay
(int value) void
Immediately close the database.void
sync()
Synchronize the files with the file system.sysIdentifier
(String upperName) Returns identifier in upper or lower case depending on database settings.toString()
void
unlockMeta
(SessionLocal session) Unlock the metadata table.boolean
unsetExclusiveSession
(SessionLocal session) Stop exclusive access the database by provided session.void
updateMeta
(SessionLocal session, DbObject obj) Update an object in the system table.void
verifyMetaLocked
(SessionLocal session) Verify the meta table is locked.boolean
Returns are ENUM values 0-based.
-
Constructor Details
-
Database
-
-
Method Details
-
getLockTimeout
public int getLockTimeout() -
getRowFactory
public org.h2.result.RowFactory getRowFactory() -
setRowFactory
public void setRowFactory(org.h2.result.RowFactory rowFactory) -
setInitialPowerOffCount
public static void setInitialPowerOffCount(int count) -
setPowerOffCount
public void setPowerOffCount(int count) -
getStore
public org.h2.mvstore.db.Store getStore() -
getModificationDataId
public long getModificationDataId() -
getNextModificationDataId
public long getNextModificationDataId() -
getModificationMetaId
public long getModificationMetaId() -
getNextModificationMetaId
public long getNextModificationMetaId() -
getRemoteSettingsId
public long getRemoteSettingsId() -
getNextRemoteSettingsId
public long getNextRemoteSettingsId() -
getPowerOffCount
public int getPowerOffCount() -
checkPowerOff
public void checkPowerOff()Description copied from interface:org.h2.store.DataHandler
Check if the simulated power failure occurred. This call will decrement the countdown.- Specified by:
checkPowerOff
in interfaceorg.h2.store.DataHandler
-
getTrace
public org.h2.message.Trace getTrace(int moduleId) Get the trace object for the given module id.- Parameters:
moduleId
- the module id- Returns:
- the trace object
-
openFile
Description copied from interface:org.h2.store.DataHandler
Open a file at the given location.- Specified by:
openFile
in interfaceorg.h2.store.DataHandler
- Parameters:
name
- the file nameopenMode
- the modemustExist
- whether the file must already exist- Returns:
- the file
-
verifyMetaLocked
Verify the meta table is locked.- Parameters:
session
- the session
-
lockMeta
Lock the metadata table for updates.- Parameters:
session
- the session- Returns:
- whether it was already locked before by this session
-
unlockMeta
Unlock the metadata table.- Parameters:
session
- the session
-
removeMeta
Remove the given object from the meta data.- Parameters:
session
- the sessionid
- the id of the object to remove
-
releaseDatabaseObjectIds
Mark some database ids as unused.- Parameters:
idsToRelease
- the ids to release
-
addSchemaObject
Add a schema object to the database.- Parameters:
session
- the sessionobj
- the object to add
-
addDatabaseObject
Add an object to the database.- Parameters:
session
- the sessionobj
- the object to add
-
findComment
Get the comment for the given database object if one exists, or null if not.- Parameters:
object
- the database object- Returns:
- the comment or null
-
findRole
Get the role if it exists, or null if not.- Parameters:
roleName
- the name of the role- Returns:
- the role or null
-
findSchema
Get the schema if it exists, or null if not.- Parameters:
schemaName
- the name of the schema- Returns:
- the schema or null
-
findSetting
Get the setting if it exists, or null if not.- Parameters:
name
- the name of the setting- Returns:
- the setting or null
-
findUser
Get the user if it exists, or null if not.- Parameters:
name
- the name of the user- Returns:
- the user or null
-
getUser
Get user with the given name. This method throws an exception if the user does not exist.- Parameters:
name
- the user name- Returns:
- the user
- Throws:
org.h2.message.DbException
- if the user does not exist
-
findUserOrRole
Get the user or role if it exists, ornull
if not.- Parameters:
name
- the name of the user or role- Returns:
- the user, the role, or
null
-
removeSession
Remove a session. This method is called after the user has disconnected.- Parameters:
session
- the session
-
allocateObjectId
public int allocateObjectId()Allocate a new object id.- Returns:
- the id
-
getSystemUser
Returns system user.- Returns:
- system user
-
getMainSchema
public org.h2.schema.Schema getMainSchema()Returns main schema (usually PUBLIC).- Returns:
- main schema (usually PUBLIC)
-
getAllComments
-
getAllowLiterals
public int getAllowLiterals() -
getAllRights
-
getAllTablesAndViews
Get all tables and views. Meta data tables may be excluded.- Returns:
- all objects of that type
-
getAllSynonyms
Get all synonyms.- Returns:
- all objects of that type
-
getAllSchemas
-
getAllSchemasNoMeta
-
getAllSettings
-
getAllUsersAndRoles
-
getCacheType
-
getCluster
-
getCompareMode
public org.h2.value.CompareMode getCompareMode()Description copied from interface:org.h2.store.DataHandler
Return compare mode.- Specified by:
getCompareMode
in interfaceorg.h2.store.DataHandler
- Returns:
- Compare mode.
-
getDatabasePath
Description copied from interface:org.h2.store.DataHandler
Get the database path.- Specified by:
getDatabasePath
in interfaceorg.h2.store.DataHandler
- Returns:
- the database path
-
getShortName
-
getName
-
getSessions
Get all sessions that are currently connected to the database.- Parameters:
includingSystemSession
- if the system session should also be included- Returns:
- the list of sessions
-
updateMeta
Update an object in the system table.- Parameters:
session
- the sessionobj
- the database object
-
renameSchemaObject
public void renameSchemaObject(SessionLocal session, org.h2.schema.SchemaObject obj, String newName) Rename a schema object.- Parameters:
session
- the sessionobj
- the objectnewName
- the new name
-
renameDatabaseObject
Rename a database object.- Parameters:
session
- the sessionobj
- the objectnewName
- the new name
-
getSchema
Get the schema. If the schema does not exist, an exception is thrown.- Parameters:
schemaName
- the name of the schema- Returns:
- the schema
- Throws:
org.h2.message.DbException
- no schema with that name exists
-
removeDatabaseObject
Remove the object from the database.- Parameters:
session
- the sessionobj
- the object to remove
-
getDependentTable
public org.h2.table.Table getDependentTable(org.h2.schema.SchemaObject obj, org.h2.table.Table except) Get the first table that depends on this object.- Parameters:
obj
- the object to findexcept
- the table to exclude (or null)- Returns:
- the first dependent table, or null
-
removeSchemaObject
Remove an object from the system table.- Parameters:
session
- the sessionobj
- the object to be removed
-
isPersistent
public boolean isPersistent()Check if this database is disk-based.- Returns:
- true if it is disk-based, false if it is in-memory only.
-
getTraceSystem
public org.h2.message.TraceSystem getTraceSystem() -
setCacheSize
public void setCacheSize(int kb) -
setMasterUser
-
getPublicRole
-
getTempTableName
Get a unique temporary table name.- Parameters:
baseName
- the prefix of the returned namesession
- the session- Returns:
- a unique name
-
setCompareMode
public void setCompareMode(org.h2.value.CompareMode compareMode) -
setCluster
-
checkWritingAllowed
public void checkWritingAllowed()Description copied from interface:org.h2.store.DataHandler
Check if writing is allowed.- Specified by:
checkWritingAllowed
in interfaceorg.h2.store.DataHandler
-
isReadOnly
public boolean isReadOnly() -
getWriteDelay
public int getWriteDelay() -
setWriteDelay
public void setWriteDelay(int value) -
getRetentionTime
public int getRetentionTime() -
setRetentionTime
public void setRetentionTime(int value) -
setAllowBuiltinAliasOverride
public void setAllowBuiltinAliasOverride(boolean b) -
isAllowBuiltinAliasOverride
public boolean isAllowBuiltinAliasOverride() -
getInDoubtTransactions
Get the list of in-doubt transactions.- Returns:
- the list
-
setBackgroundException
public void setBackgroundException(org.h2.message.DbException e) -
getBackgroundException
-
flush
public void flush()Flush all pending changes to the transaction log. -
setEventListener
-
setEventListenerClass
-
setProgress
Set the progress of a long running operation. This method calls theDatabaseEventListener
if one is registered.- Parameters:
state
- theDatabaseEventListener
statename
- the object namex
- the current positionmax
- the highest value or 0 if unknown
-
exceptionThrown
This method is called after an exception occurred, to inform the database event listener (if one is set).- Parameters:
e
- the exceptionsql
- the SQL statement
-
sync
public void sync()Synchronize the files with the file system. This method is called when executing the SQL statement CHECKPOINT SYNC. -
getMaxMemoryRows
public int getMaxMemoryRows() -
setMaxMemoryRows
public void setMaxMemoryRows(int value) -
setLockMode
public void setLockMode(int lockMode) -
getLockMode
public int getLockMode() -
setCloseDelay
public void setCloseDelay(int value) -
getSystemSession
-
isClosing
public boolean isClosing()Check if the database is in the process of closing.- Returns:
- true if the database is closing
-
setMaxLengthInplaceLob
public void setMaxLengthInplaceLob(int value) -
getMaxLengthInplaceLob
public int getMaxLengthInplaceLob()Description copied from interface:org.h2.store.DataHandler
Get the maximum length of a in-place large object- Specified by:
getMaxLengthInplaceLob
in interfaceorg.h2.store.DataHandler
- Returns:
- the maximum size
-
setIgnoreCase
public void setIgnoreCase(boolean b) -
getIgnoreCase
public boolean getIgnoreCase() -
setIgnoreCatalogs
public void setIgnoreCatalogs(boolean b) -
getIgnoreCatalogs
public boolean getIgnoreCatalogs() -
setDeleteFilesOnDisconnect
public void setDeleteFilesOnDisconnect(boolean b) -
setAllowLiterals
public void setAllowLiterals(int value) -
getOptimizeReuseResults
public boolean getOptimizeReuseResults() -
setOptimizeReuseResults
public void setOptimizeReuseResults(boolean b) -
getLobSyncObject
Description copied from interface:org.h2.store.DataHandler
Get the synchronization object for lob operations.- Specified by:
getLobSyncObject
in interfaceorg.h2.store.DataHandler
- Returns:
- the synchronization object
-
getSessionCount
public int getSessionCount() -
setReferentialIntegrity
public void setReferentialIntegrity(boolean b) -
getReferentialIntegrity
public boolean getReferentialIntegrity() -
setQueryStatistics
public void setQueryStatistics(boolean b) -
getQueryStatistics
public boolean getQueryStatistics() -
setQueryStatisticsMaxEntries
public void setQueryStatisticsMaxEntries(int n) -
getQueryStatisticsData
-
isStarting
public boolean isStarting()Check if the database is currently opening. This is true until all stored SQL statements have been executed.- Returns:
- true if the database is still starting
-
setMode
-
getMode
Description copied from interface:CastDataProvider
Returns the database mode.- Specified by:
getMode
in interfaceCastDataProvider
- Returns:
- the database mode
-
setDefaultNullOrdering
public void setDefaultNullOrdering(org.h2.mode.DefaultNullOrdering defaultNullOrdering) -
getDefaultNullOrdering
public org.h2.mode.DefaultNullOrdering getDefaultNullOrdering() -
setMaxOperationMemory
public void setMaxOperationMemory(int maxOperationMemory) -
getMaxOperationMemory
public int getMaxOperationMemory() -
getExclusiveSession
-
setExclusiveSession
Set the session that can exclusively access the database.- Parameters:
session
- the sessioncloseOthers
- whether other sessions are closed- Returns:
- true if success or if database is in exclusive mode set by this session already, false otherwise
-
unsetExclusiveSession
Stop exclusive access the database by provided session.- Parameters:
session
- the session- Returns:
- true if success or if database is in non-exclusive mode already, false otherwise
-
getLobFileListCache
Description copied from interface:org.h2.store.DataHandler
Get the lob file list cache if it is used.- Specified by:
getLobFileListCache
in interfaceorg.h2.store.DataHandler
- Returns:
- the cache or null
-
isSysTableLocked
public boolean isSysTableLocked()Checks if the system table (containing the catalog) is locked.- Returns:
- true if it is currently locked
-
isSysTableLockedBy
Checks if the system table (containing the catalog) is locked by the given session.- Parameters:
session
- the session- Returns:
- true if it is currently locked
-
getLinkConnection
public org.h2.table.TableLinkConnection getLinkConnection(String driver, String url, String user, String password) Open a new connection or get an existing connection to another database.- Parameters:
driver
- the database driver or nullurl
- the database URLuser
- the user namepassword
- the password- Returns:
- the connection
-
toString
-
shutdownImmediately
public void shutdownImmediately()Immediately close the database. -
getTempFileDeleter
public org.h2.util.TempFileDeleter getTempFileDeleter()Description copied from interface:org.h2.store.DataHandler
Get the temp file deleter mechanism.- Specified by:
getTempFileDeleter
in interfaceorg.h2.store.DataHandler
- Returns:
- the temp file deleter
-
getFirstUserTable
public org.h2.table.Table getFirstUserTable()Get the first user defined table, excluding the LOB_BLOCKS table that the Recover tool creates.- Returns:
- the table or null if no table is defined
-
checkpoint
public void checkpoint()Flush all changes and open a new transaction log. -
setReadOnly
public void setReadOnly(boolean readOnly) Switch the database to read-only mode.- Parameters:
readOnly
- the new value
-
setCompactMode
public void setCompactMode(int compactMode) -
getCompiler
public org.h2.util.SourceCompiler getCompiler() -
getLobStorage
public org.h2.store.LobStorageInterface getLobStorage()Description copied from interface:org.h2.store.DataHandler
Get the lob storage mechanism to use.- Specified by:
getLobStorage
in interfaceorg.h2.store.DataHandler
- Returns:
- the lob storage mechanism
-
getLobSession
-
getDefaultTableType
public int getDefaultTableType() -
setDefaultTableType
public void setDefaultTableType(int defaultTableType) -
getSettings
-
newStringMap
Create a new hash map. Depending on the configuration, the key is case sensitive or case insensitive.- Type Parameters:
V
- the value type- Returns:
- the hash map
-
newStringMap
Create a new hash map. Depending on the configuration, the key is case sensitive or case insensitive.- Type Parameters:
V
- the value type- Parameters:
initialCapacity
- the initial capacity- Returns:
- the hash map
-
newConcurrentStringMap
Create a new hash map. Depending on the configuration, the key is case sensitive or case insensitive.- Type Parameters:
V
- the value type- Returns:
- the hash map
-
equalsIdentifiers
Compare two identifiers (table names, column names,...) and verify they are equal. Case sensitivity depends on the configuration.- Parameters:
a
- the first identifierb
- the second identifier- Returns:
- true if they match
-
sysIdentifier
Returns identifier in upper or lower case depending on database settings.- Parameters:
upperName
- identifier in the upper case- Returns:
- identifier in upper or lower case
-
readLob
public int readLob(long lobId, byte[] hmac, long offset, byte[] buff, int off, int length) Description copied from interface:org.h2.store.DataHandler
Read from a lob.- Specified by:
readLob
in interfaceorg.h2.store.DataHandler
- Parameters:
lobId
- the lob idhmac
- the message authentication codeoffset
- the offset within the lobbuff
- the target bufferoff
- the offset within the target bufferlength
- the number of bytes to read- Returns:
- the number of bytes read
-
getPageSize
public int getPageSize() -
getJavaObjectSerializer
Description copied from interface:CastDataProvider
Returns the custom Java object serializer, ornull
.- Specified by:
getJavaObjectSerializer
in interfaceCastDataProvider
- Returns:
- the custom Java object serializer, or
null
-
setJavaObjectSerializerName
-
getTableEngine
Get the table engine class, loading it if needed.- Parameters:
tableEngine
- the table engine name- Returns:
- the class
-
getAuthenticator
public org.h2.security.auth.Authenticator getAuthenticator()get authenticator for database users- Returns:
- authenticator set for database
-
setAuthenticator
public void setAuthenticator(org.h2.security.auth.Authenticator authenticator) Set current database authenticator- Parameters:
authenticator
- = authenticator to set, null to revert to the Internal authenticator
-
currentTimestamp
public org.h2.value.ValueTimestampTimeZone currentTimestamp()Description copied from interface:CastDataProvider
Returns the current timestamp with maximum resolution. The value must be the same within a transaction or within execution of a command.- Specified by:
currentTimestamp
in interfaceCastDataProvider
- Returns:
- the current timestamp for CURRENT_TIMESTAMP(9)
-
currentTimeZone
public org.h2.util.TimeZoneProvider currentTimeZone()Description copied from interface:CastDataProvider
Returns the current time zone.- Specified by:
currentTimeZone
in interfaceCastDataProvider
- Returns:
- the current time zone
-
zeroBasedEnums
public boolean zeroBasedEnums()Description copied from interface:CastDataProvider
Returns are ENUM values 0-based.- Specified by:
zeroBasedEnums
in interfaceCastDataProvider
- Returns:
- are ENUM values 0-based
-