public class Databases extends Object implements sirius.kernel.di.Initializable
DataSource
which can be configured via the system
configuration.
Use get(String)
to obtain a managed connection to the given database.
Configuration is done via the system configuration. To declare a database provide an extension in jdbc.database. For examples see "component-db.conf".
Modifier and Type | Class and Description |
---|---|
static class |
Databases.DatabaseMetricProvider
Provides some metrics across all managed data sources.
|
Modifier and Type | Field and Description |
---|---|
protected static sirius.kernel.health.Log |
LOG |
protected static sirius.kernel.health.Counter |
numConnects |
protected static sirius.kernel.health.Counter |
numQueries |
protected static sirius.kernel.health.Counter |
numSlowQueries |
protected static sirius.kernel.health.Counter |
numUses |
protected static sirius.kernel.health.Average |
queryDuration |
protected static sirius.kernel.health.Log |
SLOW_DB_LOG |
Constructor and Description |
---|
Databases() |
Modifier and Type | Method and Description |
---|---|
static Object |
convertValue(Object value)
Transforms the given value into its database representation.
|
static LocalDateTime |
decodeLocalDateTime(long timestamp)
Decodes a long back into a LocalDateTime.
|
static long |
encodeLocalDateTime(LocalDateTime date)
Encodes a LocalDateTime as a long.
|
Row |
fetchGeneratedKeys(PreparedStatement stmt)
Returns all generated keys wrapped as row
|
Database |
get(String name)
Provides access to the selected database.
|
List<String> |
getDatabases()
Returns a list of all known (configured) databases from the system config.
|
protected static long |
getLongConnectionThresholdMillis()
Converts the threshold for "long connections" into a long containing milliseconds for performance reasons.
|
protected static long |
getLongQueryThresholdMillis()
Converts the threshold for "slow queries" into a long containing milliseconds for performance reasons.
|
boolean |
hasDatabase(String name)
Determines if a databse with the given name is present in the configuration.
|
void |
initialize() |
Set<String> |
readColumns(ResultSet rs)
Reads and returns all available columns of the given result set as upper case.
|
protected static final sirius.kernel.health.Log LOG
protected static final sirius.kernel.health.Log SLOW_DB_LOG
protected static sirius.kernel.health.Counter numUses
protected static sirius.kernel.health.Counter numConnects
protected static sirius.kernel.health.Counter numQueries
protected static sirius.kernel.health.Counter numSlowQueries
protected static sirius.kernel.health.Average queryDuration
public void initialize() throws Exception
initialize
in interface sirius.kernel.di.Initializable
Exception
public Database get(String name)
The configuration of the connection pool will be loaded from jdbc.database.[name]
name
- name of the database to accesspublic List<String> getDatabases()
public boolean hasDatabase(String name)
name
- the name of the databaseprotected static long getLongQueryThresholdMillis()
protected static long getLongConnectionThresholdMillis()
public static long encodeLocalDateTime(@Nullable LocalDateTime date)
Basically the generated long consists of year_month_day_hour_minute_second to support sorting and ordering. Also we do not use timestamps as MySQL does autoupdate these unexpectedly and also because we do not need the millisecond resolution (it can even lead to errors).
date
- the date to encode@Nullable public static LocalDateTime decodeLocalDateTime(long timestamp)
This is the inverse of encodeLocalDateTime(LocalDateTime)
.
timestamp
- the number to decodepublic static Object convertValue(Object value)
value
- the value to transformpublic Set<String> readColumns(ResultSet rs) throws SQLException
rs
- the result set to parseSQLException
- in case of a database errorpublic Row fetchGeneratedKeys(PreparedStatement stmt) throws SQLException
stmt
- the statement which was used to perform an update or insertSQLException
- in case of an error thrown by the database or driverCopyright © 2018. All rights reserved.