Package tigase.db.jdbc
Class DataRepositoryImpl
- java.lang.Object
-
- tigase.db.jdbc.DataRepositoryImpl
-
- All Implemented Interfaces:
DataRepository,DataSource,Repository,RepositoryVersionAware,StatisticsProviderIfc
public class DataRepositoryImpl extends java.lang.Object implements DataRepository, StatisticsProviderIfc, RepositoryVersionAware
Created: Sep 3, 2010 5:55:41 PM- Author:
- Artur Hefczyc
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface tigase.db.DataRepository
DataRepository.dbTypes
-
Nested classes/interfaces inherited from interface tigase.db.Repository
Repository.Meta, Repository.SchemaId
-
Nested classes/interfaces inherited from interface tigase.db.util.RepositoryVersionAware
RepositoryVersionAware.SchemaVersion
-
-
Field Summary
Fields Modifier and Type Field Description static intDB_CONN_TIMEOUTstatic java.lang.StringDB_CONN_TIMEOUT_PROP_KEYstatic java.lang.StringDERBY_CHECK_TABLE_QUERYstatic java.lang.StringDERBY_CONNVALID_QUERYstatic java.lang.StringJDBC_CONNVALID_QUERYstatic java.lang.StringJDBC_SCHEMA_VERSION_QUERYstatic java.lang.StringMYSQL_CHECK_TABLE_QUERYstatic java.lang.StringOTHER_CHECK_TABLE_QUERYstatic java.lang.StringPGSQL_CHECK_TABLE_QUERYstatic intQUERY_TIMEOUTstatic java.lang.StringQUERY_TIMEOUT_PROP_KEYstatic java.lang.StringSP_STARTS_WITHstatic java.lang.StringSQLSERVER_CHECK_TABLE_QUERY-
Fields inherited from interface tigase.db.DataRepository
UTC_CALENDAR
-
-
Constructor Summary
Constructors Constructor Description DataRepositoryImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanautomaticSchemaManagement()voidcheckConnectivity(java.time.Duration watchdogTime)This method is called by data source bean watchdog mechanism to ensure that there is proper connectivity to underlying data storage.booleancheckTable(java.lang.String tableName)The method checks whether a table for the given name exists in the database.booleancheckTable(java.lang.String tableName, java.lang.String createTableQuery)The method checks whether a table for the given name exists in the database and if it does not, it automatically creates it.voidcommit()Commits current transaction on the DataRepository connection.java.sql.StatementcreateStatement(BareJID user_id)Creates a SQL statement on which SQL queries can be executed later by the higher repository layer.voidendTransaction()Ends current transaction on the DataRepository connection.DataRepository.dbTypesgetDatabaseType()Returns type of DataRepository databaseprotected intgetParam(java.lang.String key, java.util.Map<java.lang.String,java.lang.String> params, int def)intgetPoolSize()java.sql.PreparedStatementgetPreparedStatement(int hashCode, java.lang.String stIdKey)Returns a prepared statement for a given key.java.sql.PreparedStatementgetPreparedStatement(BareJID user_id, java.lang.String stIdKey)Returns a prepared statement for a given key.java.lang.StringgetResourceUri()Returns a DB connection string or DB connection URI.java.util.Optional<Version>getSchemaVersion(java.lang.String component)Method obtains version of the schema for particular component stored in the database.voidgetStatistics(java.lang.String compName, StatisticsList list)voidinitialize(java.lang.String resource_uri)The method is called to initialize the data repository.voidinitPreparedStatement(java.lang.String key, java.lang.String query)Initializes a prepared statement for a given query and stores it internally under the given id key.voidinitPreparedStatement(java.lang.String key, java.lang.String query, int autoGeneratedKeys)Initializes a prepared statement for a given query and stores it internally under the given id key.voidinitRepository(java.lang.String resource_uri, java.util.Map<java.lang.String,java.lang.String> params)Deprecated.static DataRepository.dbTypesparseDatabaseType(java.lang.String resource_uri)voidrelease(java.sql.Statement stmt, java.sql.ResultSet rs)A helper method to release resources from the statement and result set.voidreleaseRepoHandle(DataRepository repo)voidrollback()Rolls back started transaction on the DataRepository connection.voidstartTransaction()Starts transaction on the DataRepository connection.DataRepositorytakeRepoHandle(BareJID user_id)ReturnsDataRepositoryinstance.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface tigase.db.DataRepository
getTimestamp, getTimestamp, setTimestamp
-
Methods inherited from interface tigase.db.DataSource
checkSchemaVersion
-
Methods inherited from interface tigase.db.util.RepositoryVersionAware
getVersion, updateSchema
-
-
-
-
Field Detail
-
DERBY_CONNVALID_QUERY
public static final java.lang.String DERBY_CONNVALID_QUERY
- See Also:
- Constant Field Values
-
JDBC_CONNVALID_QUERY
public static final java.lang.String JDBC_CONNVALID_QUERY
- See Also:
- Constant Field Values
-
JDBC_SCHEMA_VERSION_QUERY
public static final java.lang.String JDBC_SCHEMA_VERSION_QUERY
- See Also:
- Constant Field Values
-
MYSQL_CHECK_TABLE_QUERY
public static final java.lang.String MYSQL_CHECK_TABLE_QUERY
- See Also:
- Constant Field Values
-
PGSQL_CHECK_TABLE_QUERY
public static final java.lang.String PGSQL_CHECK_TABLE_QUERY
- See Also:
- Constant Field Values
-
DERBY_CHECK_TABLE_QUERY
public static final java.lang.String DERBY_CHECK_TABLE_QUERY
- See Also:
- Constant Field Values
-
SQLSERVER_CHECK_TABLE_QUERY
public static final java.lang.String SQLSERVER_CHECK_TABLE_QUERY
- See Also:
- Constant Field Values
-
OTHER_CHECK_TABLE_QUERY
public static final java.lang.String OTHER_CHECK_TABLE_QUERY
- See Also:
- Constant Field Values
-
SP_STARTS_WITH
public static final java.lang.String SP_STARTS_WITH
- See Also:
- Constant Field Values
-
QUERY_TIMEOUT_PROP_KEY
public static final java.lang.String QUERY_TIMEOUT_PROP_KEY
- See Also:
- Constant Field Values
-
QUERY_TIMEOUT
public static final int QUERY_TIMEOUT
- See Also:
- Constant Field Values
-
DB_CONN_TIMEOUT_PROP_KEY
public static final java.lang.String DB_CONN_TIMEOUT_PROP_KEY
- See Also:
- Constant Field Values
-
DB_CONN_TIMEOUT
public static final int DB_CONN_TIMEOUT
- See Also:
- Constant Field Values
-
-
Method Detail
-
automaticSchemaManagement
public boolean automaticSchemaManagement()
- Specified by:
automaticSchemaManagementin interfaceDataSource
-
getSchemaVersion
public java.util.Optional<Version> getSchemaVersion(java.lang.String component)
Description copied from interface:DataSourceMethod obtains version of the schema for particular component stored in the database.- Specified by:
getSchemaVersionin interfaceDataSource- Parameters:
component- name of the component for which we want to get the schema version- Returns:
- an optional value of the version.
-
checkTable
public boolean checkTable(java.lang.String tableName) throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryThe method checks whether a table for the given name exists in the database.- Specified by:
checkTablein interfaceDataRepository- Parameters:
tableName- is aStringvalue of the table name to check- Returns:
truebooleanvalue if the table exist in the database andfalseif the table was not found.- Throws:
java.sql.SQLException- if there was a problem accessing database.
-
checkTable
public boolean checkTable(java.lang.String tableName, java.lang.String createTableQuery) throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryThe method checks whether a table for the given name exists in the database and if it does not, it automatically creates it.- Specified by:
checkTablein interfaceDataRepository- Parameters:
tableName- is aStringvalue of the table name to checkcreateTableQuery- is aStringwith the query to create table- Returns:
truebooleanvalue if the table exist in the database andfalseif the table was not found.- Throws:
java.sql.SQLException- if there was a problem accessing database.
-
checkConnectivity
public void checkConnectivity(java.time.Duration watchdogTime)
Description copied from interface:DataSourceThis method is called by data source bean watchdog mechanism to ensure that there is proper connectivity to underlying data storage.- Specified by:
checkConnectivityin interfaceDataSource- Parameters:
watchdogTime- time which should pass between checks
-
createStatement
public java.sql.Statement createStatement(BareJID user_id) throws java.sql.SQLException
Description copied from interface:DataRepositoryCreates a SQL statement on which SQL queries can be executed later by the higher repository layer.- Specified by:
createStatementin interfaceDataRepository- Parameters:
user_id- user id for which the statement has to be created. This is an optional parameter and null can be provided. It is used mainly to group queries for the same user on the same DB connection.- Returns:
- a newly created
Statement - Throws:
java.sql.SQLException- if a JDBC error occurs.
-
getPreparedStatement
public java.sql.PreparedStatement getPreparedStatement(BareJID user_id, java.lang.String stIdKey) throws java.sql.SQLException
Description copied from interface:DataRepositoryReturns a prepared statement for a given key.- Specified by:
getPreparedStatementin interfaceDataRepository- Parameters:
user_id- user id for which the statement has to be created. This is an optional parameter and null can be provided. It is used mainly to group queries for the same user on the same DB connection.stIdKey- is a statement identification key.- Returns:
- a
PreparedStatementfor the given id key or null if such a statement does not exist. - Throws:
java.sql.SQLException
-
getPreparedStatement
public java.sql.PreparedStatement getPreparedStatement(int hashCode, java.lang.String stIdKey) throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryReturns a prepared statement for a given key.- Specified by:
getPreparedStatementin interfaceDataRepository- Parameters:
hashCode- user for selection of connection to use. It is used mainly to group queries for the same user on the same DB connection.stIdKey- is a statement identification key.- Returns:
- a
PreparedStatementfor the given id key or null if such a statement does not exist. - Throws:
java.sql.SQLException
-
getResourceUri
public java.lang.String getResourceUri()
Description copied from interface:DataRepositoryReturns a DB connection string or DB connection URI.- Specified by:
getResourceUriin interfaceDataRepository- Specified by:
getResourceUriin interfaceDataSource- Returns:
- a
Stringvalue representing database connection string.
-
getDatabaseType
public DataRepository.dbTypes getDatabaseType()
Description copied from interface:DataRepositoryReturns type of DataRepository database- Specified by:
getDatabaseTypein interfaceDataRepository- Returns:
- a value of
dbTypes
-
initPreparedStatement
public void initPreparedStatement(java.lang.String key, java.lang.String query) throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryInitializes a prepared statement for a given query and stores it internally under the given id key. It can be retrieved later usinggetPreparedStatement(stIdKey)method.- Specified by:
initPreparedStatementin interfaceDataRepository- Parameters:
key- is a statement identification key.query- is a query for the prepared statement.- Throws:
java.sql.SQLException
-
initPreparedStatement
public void initPreparedStatement(java.lang.String key, java.lang.String query, int autoGeneratedKeys) throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryInitializes a prepared statement for a given query and stores it internally under the given id key. It can be retrieved later usinggetPreparedStatement(stIdKey)method.- Specified by:
initPreparedStatementin interfaceDataRepository- Parameters:
key- is a statement identification key.query- is a query for the prepared statement.autoGeneratedKeys- defines if statement should return auto generated keys- Throws:
java.sql.SQLException
-
initialize
public void initialize(java.lang.String resource_uri) throws DBInitExceptionDescription copied from interface:DataSourceThe method is called to initialize the data repository.- Specified by:
initializein interfaceDataSource- Parameters:
resource_uri- value in most cases representing the database connection string.- Throws:
DBInitException
-
parseDatabaseType
public static DataRepository.dbTypes parseDatabaseType(java.lang.String resource_uri)
-
initRepository
@Deprecated public void initRepository(java.lang.String resource_uri, java.util.Map<java.lang.String,java.lang.String> params) throws DBInitExceptionDeprecated.Description copied from interface:RepositoryMethod is deprecated and should not be user any more.
The method is called to initialize the data repository. Depending on the implementation all the initialization parameters can be passed either viaresource_uriparameter as the database connection string or viaparamsmap if the required repository parameters are more complex or both.- Specified by:
initRepositoryin interfaceRepository- Parameters:
resource_uri- value in most cases representing the database connection string.params- is aMapwith repository properties necessary to initialize and perform all the functions. The initialization parameters are implementation dependent.- Throws:
DBInitException- if there was an error during repository initialization. Some implementations, though, perform so called lazy initialization so even though there is a problem with the underlying repository it may not be signaled through this method call.
-
release
public void release(java.sql.Statement stmt, java.sql.ResultSet rs)Description copied from interface:DataRepositoryA helper method to release resources from the statement and result set. This is most common operation for all database calls, therefore it does make sense to add such a utility method to the API.- Specified by:
releasein interfaceDataRepository- Parameters:
stmt- aStatementvariable to release resources for. Might be null.rs- aResultSetvariable to release resources for. Might be null.
-
takeRepoHandle
public DataRepository takeRepoHandle(BareJID user_id)
Description copied from interface:DataRepositoryReturnsDataRepositoryinstance. If this is a repository pool then it returns particular instance from the pool. It this is a real repository instance it returns itself. This is exclusive take, no other thread may use this handle until it is returned to the pool.- Specified by:
takeRepoHandlein interfaceDataRepository- Parameters:
user_id- is user account ID for which we acquire the handle.- Returns:
- DataRepository instance.
-
startTransaction
public void startTransaction() throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryStarts transaction on the DataRepository connection. Please note that calling this method on the repository pool has no effect. You have to obtain particular repository handle first, before you can start transaction.- Specified by:
startTransactionin interfaceDataRepository- Throws:
java.sql.SQLException
-
commit
public void commit() throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryCommits current transaction on the DataRepository connection. Please note that calling this method on the repository pool has no effect. You have to obtain particular repository handle first, before you can start transaction.- Specified by:
commitin interfaceDataRepository- Throws:
java.sql.SQLException
-
rollback
public void rollback() throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryRolls back started transaction on the DataRepository connection. Please note that calling this method on the repository pool has no effect. You have to obtain particular repository handle first, before you can start transaction.- Specified by:
rollbackin interfaceDataRepository- Throws:
java.sql.SQLException
-
endTransaction
public void endTransaction() throws java.sql.SQLExceptionDescription copied from interface:DataRepositoryEnds current transaction on the DataRepository connection. Please note that calling this method on the repository pool has no effect. You have to obtain particular repository handle first, before you can start transaction.- Specified by:
endTransactionin interfaceDataRepository- Throws:
java.sql.SQLException
-
releaseRepoHandle
public void releaseRepoHandle(DataRepository repo)
- Specified by:
releaseRepoHandlein interfaceDataRepository
-
getStatistics
public void getStatistics(java.lang.String compName, StatisticsList list)- Specified by:
getStatisticsin interfaceStatisticsProviderIfc
-
getPoolSize
public int getPoolSize()
- Specified by:
getPoolSizein interfaceDataRepository
-
getParam
protected int getParam(java.lang.String key, java.util.Map<java.lang.String,java.lang.String> params, int def)
-
-