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
Fields -
Method Summary
Modifier and TypeMethodDescriptionintcountRecords(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 DatabaseManagerAccess 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.voidInitialize DB manager.booleanisH2Db()Is a Java H2 database used?booleanHas this database manager been initialized?booleanIs a Maria database used?booleanIs a MySQL database used?booleanIs an Oracle database used?booleanIs a PostgreSQL database used?booleanIs a PostgreSQL database with NG driver used?booleanIs databse unsupported?voidrelease()Resource database pool resources.voidRetire a global DB connection.voidupdateLanguage(String lang, int dbUserId) Update language.
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOGLogger. -
POOL_NAME_POSTFIX
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 useDBUtilsroll-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
-