Class Datastore2
- Direct Known Subclasses:
H2Store2,MariaDBStore2,MySQLStore2,SQLiteStore2
- Author:
- gmt2001
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbackup()Performs a backup of the database to thedbbackupfoldervoidPerforms a backup of the database to thedbbackupfolderReturns the default backup filename, which is usually the database name withtimestamp()appendedprotected voidcheckVersion(SQLDialect dialect, Connection connection, String className) Checks if JOOQ supports the software version of the connected databasebooleanIndicates if this driver prefers automatic backups to be enabled by defaultvoiddispose()Disposes of resources as necessaryvoidPerforms periodic database maintenanceprotected voidAllows the driver to perform additional disposal actions beyond what the base Datastore2 class performsReturns theDSLContextwhich can be used to start a fluent statementAttempts to find the named tableTable<?>findTableRequired(String tableName) Attempts to find the named tableRetrieves aConnectionfrom the connection poolstatic voidinit()Initializes a new instance of Datastore2, based on the propertydatastorein botlogin.txtprotected voidinit(ConnectionPoolDataSource dataSource, int maxConnections, int timeout, SQLDialect sqlDialect) Instance Initializerprotected voidinit(ConnectionPoolDataSource dataSource, SQLDialect sqlDialect) Instance Initializer.static Datastore2instance()Provides an instance ofDatastore2voidInvalidates the cache of known tablesReturns theDataTyperepresenting theLONGTEXTequivalent SQL data type for the drivermeta()Allows the driver to perform operations to prepare a meta object for use, such as selecting the schemaprotected voidprepareConnection(Connection connection) Allows the driver to perform operations to prepare the connection for use, such as selecting the schemavoidrestoreBackup(String fileName) Restores a database from the specified backup file, which must be in thedbbackupfolderbooleanIndicates if this driver supports making backups without an external tooltables()Returns a list of cached known tables, blocking until availableReturns a list of cached known tables in aMonobooleanTests if the database is accessiblestatic StringReturns a timestamp suitable for database backup names in the formatyyyy-MM-dd.hh-mm-ss
-
Field Details
-
PREFIX
Table name prefix for all tables created as POJOs- See Also:
-
-
Constructor Details
-
Datastore2
public Datastore2()
-
-
Method Details
-
instance
Provides an instance ofDatastore2If the instance has not been initialized, calls
init()- Returns:
- an instance of
Datastore2
-
timestamp
Returns a timestamp suitable for database backup names in the formatyyyy-MM-dd.hh-mm-ss- Returns:
- the timestamp
-
init
public static void init()Initializes a new instance of Datastore2, based on the propertydatastorein botlogin.txtIf a datastore driver is not specified or is blank, defaults to
H2Store2If this function is called when Datastore2 is already initialized, it is a no-op
Builtin datastore drivers are not case-sensitive. Custom datastore drivers are case-sensitive
If loading a custom datastore driver, the following requirements must be met:
- Must extend
Datastore2 - Must call
init(ConnectionPoolDataSource, SQLDialect)orinit(ConnectionPoolDataSource, int, int, SQLDialect), passing in a validConnectionPoolDataSource, in the constructor - May optionally start a timer in the constructor to periodically call
doMaintenance() - Must be in a JAR file located in the
./datastoresfolder - The name of the JAR file must match the output of
Class.getSimpleName()(Classname) of the type, including case - The
.jarfile extension on the JAR file must be lower-case - The value of the
datastoreproperty in botlogin.txt must match the output ofClass.getName()(Fully qualified classname) of the type, including case
- Must extend
-
init
Instance Initializer. Sets a max connections of 30 and a timeout of 20Valid dialects:
SQLDialect.DERBYSQLDialect.FIREBIRDSQLDialect.H2SQLDialect.HSQLDBSQLDialect.MARIADBSQLDialect.MYSQLSQLDialect.POSTGRESSQLDialect.SQLITESQLDialect.YUGABYTEDB
NOTE: SQL will be generated according to the latest supported version of the selected database dialect
- Parameters:
dataSource- aConnectionPoolDataSourcewhich can be used withMiniConnectionPoolManagersqlDialect- the dialect to use with objects created from theDSLContext
-
init
protected void init(ConnectionPoolDataSource dataSource, int maxConnections, int timeout, SQLDialect sqlDialect) Instance InitializerValid dialects:
SQLDialect.DERBYSQLDialect.FIREBIRDSQLDialect.H2SQLDialect.HSQLDBSQLDialect.MARIADBSQLDialect.MYSQLSQLDialect.POSTGRESSQLDialect.SQLITESQLDialect.YUGABYTEDB
NOTE: SQL will be generated according to the latest supported version of the selected database dialect
- Parameters:
dataSource- aConnectionPoolDataSourcewhich can be used withMiniConnectionPoolManagermaxConnections- the maximum number ofConnectionobjects to hold in the pooltimeout- the number of seconds until a call toMiniConnectionPoolManager.getConnection()fails waiting for aConnectionto become availablesqlDialect- the dialect to use with objects created from theDSLContext
-
checkVersion
protected void checkVersion(SQLDialect dialect, Connection connection, String className) throws IllegalStateException, SQLException Checks if JOOQ supports the software version of the connected database- Parameters:
dialect- theSQLDialectto check againseconnection- a validConnectionto the databaseclassName- the name of the Datastore2 subclass that is requesting the check- Throws:
IllegalStateException- if the detected software version of the database is not supported by JOOQSQLException- if a database access error occurs
-
prepareConnection
Allows the driver to perform operations to prepare the connection for use, such as selecting the schema- Parameters:
connection- the connection to prepare- Throws:
SQLException- if a database access error occurs
-
meta
Allows the driver to perform operations to prepare a meta object for use, such as selecting the schema- Returns:
- A
Metaobject for the current database
-
getConnection
Retrieves aConnectionfrom the connection poolIf the maximum number of connections are already in use, the method waits until a connection becomes available or the timeout has elapsed
When the application is finished using the connection, it must call
Connection.close()on it in order to return it to the connection poolConsider using try-with-resources instead to safely auto-close the connection
Transactions are not committed automatically when closing a
Connectionthat has auto-commit disabled- Returns:
- a new
Connectionobject - Throws:
SQLException- if a database access error occursMiniConnectionPoolManager.TimeoutException- when no connection becomes available within the timeout
-
testConnection
Tests if the database is accessibleHow this is achieved is driver-defined, but is usually something similar to executing
SELECT 1;- Returns:
trueif a valid connection can be established to the database before the timeout expires- Throws:
SQLException- if a database access error occurs
-
dslContext
Returns theDSLContextwhich can be used to start a fluent statement- Returns:
- the
DSLContext - See Also:
-
invalidateTableCache
public void invalidateTableCache()Invalidates the cache of known tablesThis should be called every time a table is created, renamed, or deleted
-
tablesAsync
Returns a list of cached known tables in aMonoIf the cache has not been invalidated, returns a
Monowhich returns the previous cached resultIf the cache has been invalidated with
invalidateTableCache(), refreshes the cache -
tables
Returns a list of cached known tables, blocking until availableIf the cache has not been invalidated, returns the previous cached result
If the cache has been invalidated with
invalidateTableCache(), refreshes the cache- Returns:
- a list of
Table
-
findTable
Attempts to find the named table -
findTableRequired
Attempts to find the named table- Parameters:
tableName- the table name- Returns:
- the matching
Table - Throws:
TableDoesNotExistException- if the table can not be found or the cache is stale
-
longTextDataType
Returns theDataTyperepresenting theLONGTEXTequivalent SQL data type for the driver- Returns:
- the DataType
-
supportsBackup
public boolean supportsBackup()Indicates if this driver supports making backups without an external tool- Returns:
trueif supported
-
defaultBackupPreference
public boolean defaultBackupPreference()Indicates if this driver prefers automatic backups to be enabled by default- Returns:
trueif enabled by default
-
backupFileName
Returns the default backup filename, which is usually the database name withtimestamp()appended- Returns:
- the filename
-
backup
public void backup()Performs a backup of the database to thedbbackupfolderThe default backup filename is used from
backupFileName() -
backup
Performs a backup of the database to thedbbackupfolder- Parameters:
fileName- the name of the backup file
-
restoreBackup
Restores a database from the specified backup file, which must be in thedbbackupfolder- Parameters:
fileName- the name of the backup file- Throws:
FileNotFoundException- if the backup file can not be found
-
doMaintenance
public void doMaintenance()Performs periodic database maintenance -
dispose
public void dispose()Disposes of resources as necessaryOnce this is called, the connection pool is invalid and the program must be restarted to futher access the database
-
driverDispose
protected void driverDispose()Allows the driver to perform additional disposal actions beyond what the base Datastore2 class performsThis method is called before the connection pool is disposed
-