Package ch.autumo.commons.database
Class DatabaseManager
java.lang.Object
ch.autumo.commons.database.DatabaseManager
Commons Database Manager.
Supported databases: H2, MySQL, MariaDB, Oracle, PostgreSQL.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
countRecords
(String entity) Count amount of records of an entity / table in database.describeTable
(String table) Describe table columns for given table.Get an new DB connection.Get the data source.Get data-source class name.Get driver class.Get an new global DB connection.getH2Url()
Get H2 URL.static DatabaseManager
Access DB manager.getLanguage
(int dbId) Get language for user.getPass()
Get password.getProperty
(String name) Get property value from database (table 'properties').getUrl()
Get database connection URL.getUser()
Get user.void
Initialize DB manager.boolean
isH2Db()
Is a Java H2 database used?boolean
Has this database manager been initialized?boolean
Is a Maria database used?boolean
Is a MySQL database used?boolean
Is an Oracle database used?boolean
Is a PostgreSQL database used?boolean
Is a PostgreSQL database with NG driver used?boolean
Is databse unsupported? This doesn't mean that it cannot work, but the SQL used are not verified for the unsupported database's SQL dialect.void
release()
Resource database pool resources.void
Retire a global DB connection.void
updateLanguage
(String lang, int dbUserId) Update language.
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOGLogger. -
POOL_NAME_PEFIX
DB pool postfix name.- See Also:
-
CFG_KEY_DS_EXT_JNDI
External JNDI configuration key.- See Also:
-
CFG_KEY_DS_INT_DSCN
External data-source configuration key.- See Also:
-
-
Method Details
-
getInstance
Access DB manager.- Returns:
- DB manager
-
isInitialized
public boolean isInitialized()Has this database manager been initialized?- Returns:
- true if so, otherwise false
-
initialize
Initialize DB manager.- Throws:
Exception
- if initialization fails
-
release
public void release()Resource database pool resources. Should be called when a container life-cycle or a server ends! -
getDataSource
Get the data source.- Returns:
- data source
-
getConnection
Get an new DB connection.- Returns:
- DB connection
- Throws:
SQLException
- if a connection cannot be acquired
-
getGlobalConnection
Get an new global DB connection. You have to roll back or commit the transaction, before you retire it withretireGlobalConnection(Connection)
. If you useDBUtils
roll-backs are done automatically and you'll receive anSQLException
. Don't close it by yourself!- Returns:
- global DB connection
- Throws:
SQLException
- SQL exception
-
retireGlobalConnection
Retire a global DB connection.- Parameters:
conn
- connection- Throws:
SQLException
- SQL exception- See Also:
-
getH2Url
Get H2 URL.- Returns:
- H2 URL
-
isH2Db
public boolean isH2Db()Is a Java H2 database used?- Returns:
- true if so
-
isMysqlDb
public boolean isMysqlDb()Is a MySQL database used?- Returns:
- true if so
-
isMariaDb
public boolean isMariaDb()Is a Maria database used?- Returns:
- true if so
-
isOracleDb
public boolean isOracleDb()Is an Oracle database used?- Returns:
- true if so
-
isPostgreDb
public boolean isPostgreDb()Is a PostgreSQL database used?- Returns:
- true if so
-
isPostgreDbWithNGDriver
public boolean isPostgreDbWithNGDriver()Is a PostgreSQL database with NG driver used?- Returns:
- true if so
-
isUnsupported
public boolean isUnsupported()Is databse unsupported? This doesn't mean that it cannot work, but the SQL used are not verified for the unsupported database's SQL dialect.- Returns:
- true if so
-
countRecords
Count amount of records of an entity / table in database.- Parameters:
entity
- entity- Returns:
- amount of records
- Throws:
SQLException
- if records cannot be counted
-
getProperty
Get property value from database (table 'properties'). If the value isn't found, null is returned.- Parameters:
name
- name/key- Returns:
- value for name/key
- Throws:
Exception
- if propeties (settings) cannot be read from database
-
getLanguage
Get language for user.- Parameters:
dbId
- user id- Returns:
- language
- Throws:
Exception
- if language cannot be read from database
-
updateLanguage
Update language.- Parameters:
lang
- language codedbUserId
- user id- Throws:
Exception
- if language cannot be updated from database
-
describeTable
Describe table columns for given table.- Parameters:
table
- database table- Returns:
- list of DB fields with table column descriptions
- Throws:
SQLException
- if table description cannot be read from database
-
getUrl
Get database connection URL.- Returns:
- database connection URL
-
getUser
Get user.- Returns:
- user
-
getPass
Get password.- Returns:
- password
-
getDriver
Get driver class.- Returns:
- driver class
-
getDataSourceClassName
Get data-source class name.- Returns:
- data-source class name
-