Class TeamCityDatabaseManager
- java.lang.Object
-
- jetbrains.buildServer.serverSide.db.TeamCityDatabaseManager
-
- All Implemented Interfaces:
DBFunctionsProvider
public class TeamCityDatabaseManager extends Object implements DBFunctionsProvider
Database accessor.The responsibility of this class is to read/analyze TeamCity database settings, connect to the database and provides a DataSource instance.
It also manages some maintenance works - like creating/updating schema or HSQL compacting.
- Author:
- Leonid Bushuev
-
-
Constructor Summary
Constructors Constructor Description TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, String ipAddress, String applicationInfo, ServerVersionInfo serverVersion, DatabasePropertiesFile databasePropertiesFile, DatabaseSettings databaseSettings, DBAction<String[]> perSessionScript)
Private constructor shared by public constructors.TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, String ipAddress, String applicationInfo, ServerVersionInfo serverVersion, JdbcConnectionString connectionString, File systemDir, DBAction<String[]> perSessionScript)
Set up using a connection string.TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, DatabasePropertiesFile databasePropertiesFile, String... perSessionScript)
Reads/analyzes database settings and prepares internal variables for connecting.TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, DatabasePropertiesFile databasePropertiesFile, DBAction<String[]> perSessionScript)
Reads/analyzes database settings and prepares internal variables for connecting.TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, DatabaseSettings databaseSettings, DBAction<String[]> perSessionScript)
Set up using DatabaseSettings string.TeamCityDatabaseManager(JdbcDrivers jdbcDrivers, JdbcConnectionString connectionString, File systemDir, String... perSessionScript)
Set up using a connection string.
-
Method Summary
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jetbrains.buildServer.serverSide.db.DBFunctionsProvider
getWithDB
-
-
-
-
Constructor Detail
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @NotNull DatabasePropertiesFile databasePropertiesFile, @NotNull String... perSessionScript)
Reads/analyzes database settings and prepares internal variables for connecting. But doesn't connect.This method doesn't require an accessible database server.
- Parameters:
jdbcDrivers
- JDBC driver loader.databasePropertiesFile
- database properties file.perSessionScript
- the script that should be executed on connect for each connection.- Since:
- 2017.2.1
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @NotNull DatabasePropertiesFile databasePropertiesFile, @NotNull DBAction<String[]> perSessionScript)
Reads/analyzes database settings and prepares internal variables for connecting. But doesn't connect.This method doesn't require an accessible database server.
- Parameters:
jdbcDrivers
- JDBC driver loader.databasePropertiesFile
- database properties file.perSessionScript
- the script that should be executed on connect for each connection.- Since:
- 2017.2.1
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @NotNull JdbcConnectionString connectionString, @NotNull File systemDir, @NotNull String... perSessionScript)
Set up using a connection string.- Parameters:
jdbcDrivers
- JDBC driver loader.connectionString
- parsed connection string.systemDir
- system dir (used for HSQL only).perSessionScript
- the script that should be executed on connect for each connection.- Since:
- 2017.2.1
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @Nullable String ipAddress, @Nullable String applicationInfo, @Nullable ServerVersionInfo serverVersion, @NotNull JdbcConnectionString connectionString, @NotNull File systemDir, @NotNull DBAction<String[]> perSessionScript)
Set up using a connection string.- Parameters:
jdbcDrivers
- JDBC driver loader.ipAddress
- the optional IP address.applicationInfo
- the optional free-form server information.serverVersion
- the optional server version information.connectionString
- parsed connection string.systemDir
- system dir (used for HSQL only).perSessionScript
- the script that should be executed on connect for each connection.- Since:
- 2017.2.1
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @NotNull DatabaseSettings databaseSettings, @NotNull DBAction<String[]> perSessionScript)
Set up using DatabaseSettings string.- Parameters:
jdbcDrivers
- JDBC driver loader.databaseSettings
- database connection settings.perSessionScript
- the script that should be executed on connect for each connection.- Since:
- 2017.2.1
-
TeamCityDatabaseManager
public TeamCityDatabaseManager(@NotNull JdbcDrivers jdbcDrivers, @Nullable String ipAddress, @Nullable String applicationInfo, @Nullable ServerVersionInfo serverVersion, @Nullable DatabasePropertiesFile databasePropertiesFile, @NotNull DatabaseSettings databaseSettings, @NotNull DBAction<String[]> perSessionScript)
Private constructor shared by public constructors.
- Parameters:
jdbcDrivers
- JDBC driver loader.ipAddress
- the optional IP address.applicationInfo
- the optional free-form server information.serverVersion
- the optional server version information.databasePropertiesFile
- the optionaldatabase.properties
wrapper.databaseSettings
- database connection settings.perSessionScript
- the database action which evaluates to the per-session script once the manager is connected.- Since:
- 2017.2.1
-
-
Method Detail
-
getConnectionString
@NotNull public String getConnectionString()
-
getParsedConnectionString
@NotNull public JdbcConnectionString getParsedConnectionString()
-
connect
public void connect(boolean pooling) throws DBException
Connects do the database.- Throws:
DBException
- when something goes wrong.
-
changePoolingMode
public void changePoolingMode(boolean pooling)
-
isPooling
public boolean isPooling()
-
reconnectMaintenanceDBF
public void reconnectMaintenanceDBF()
-
createNewDBFunctions
@NotNull protected DBFunctions createNewDBFunctions()
-
getDatabaseProductName
@NotNull public String getDatabaseProductName()
-
getDatabaseProductVersion
@NotNull public Version getDatabaseProductVersion()
-
getDriverName
@NotNull public String getDriverName()
-
getDriverVersion
@NotNull public Version getDriverVersion()
-
activateHeartbeat
public boolean activateHeartbeat(@NotNull String nodeId, @NotNull Set<NodeResponsibility> supportedResponsibilities, boolean fixedResponsibilities, @NotNull ServerPaths serverPaths, @NotNull Heartbeat.AppType appType, @NotNull String accessToken, String uuid, @NotNull Date serverStartupTimestamp) throws DBException
Captures the TeamCity database lock- Throws:
DBException
- when something goes wrong.SecondTeamCityInstanceException
- when TeamCity already started with this database.Heartbeat.ExclusiveLockTakenError
- when exclusive lock to db was takenHeartbeat.MainServerWithDifferentVersionError
- when db is used by main server of a different versionHeartbeat.OtherNodeWithDifferentVersionError
- when db is used by secondary node of a different version
-
tryGetExclusiveLock
public boolean tryGetExclusiveLock()
-
isConnected
public boolean isConnected()
-
isHeartbeatActive
public boolean isHeartbeatActive()
-
getConnectionProperties
public Properties getConnectionProperties()
-
withMaintenanceDBF
public void withMaintenanceDBF(@NotNull DBActionNoResults action)
-
checkSchemaBelongsTeamCity
public boolean checkSchemaBelongsTeamCity()
-
createNewSchemaWithInitialData
public void createNewSchemaWithInitialData()
-
countActiveConnections
public int countActiveConnections()
-
beforeDisconnect
public void beforeDisconnect()
-
disconnect
public void disconnect()
-
isDatabaseExternal
public boolean isDatabaseExternal()
-
compactHsql
public void compactHsql()
-
withDB
@Nullable public <T> T withDB(@NotNull DBAction<T> action)
Description copied from interface:DBFunctionsProvider
Performs the specified function with a database. TheDBFunctionsProvider.getWithDB(DBAction)
is preferred because allows to avoid redundant actions casting to theDBAction
.- Specified by:
withDB
in interfaceDBFunctionsProvider
- Type Parameters:
T
- type of returning value.- Parameters:
action
- action (class that implement functionDBAction.run(jetbrains.buildServer.serverSide.db.DBFunctions)
) to perform.- Returns:
- value returned by action.
DBAction.run(jetbrains.buildServer.serverSide.db.DBFunctions)
. - See Also:
DBFunctionsProvider.withDB(DBActionNoResults)
,DBFunctionsProvider.runAndRetry(DBAction)
-
withDB
public void withDB(@NotNull DBActionNoResults action)
Description copied from interface:DBFunctionsProvider
Performs the specified procedure with a database.- Specified by:
withDB
in interfaceDBFunctionsProvider
- Parameters:
action
- action (class that implements procedureDBActionNoResults.run(jetbrains.buildServer.serverSide.db.DBFunctions)
) to perform.- See Also:
DBFunctionsProvider.withDB(DBAction)
,DBFunctionsProvider.runAndRetry(DBActionNoResults)
-
runAndRetry
public void runAndRetry(@NotNull DBActionNoResults action)
Description copied from interface:DBFunctionsProvider
Runs the given procedure in its own transaction.If this procedure became a deadlock victim or when
RetryException
occurred, it rolls the transaction back and re-runs the procedure (may be several times). Then, when success, commits the transaction.If an exception (not deadlock) occurred during the execution, the transaction will be rolled back the the occurred exception is thrown.
Warning! - there must be only database works with the given DBF instance inside the transaction. Using other database connections or performing file system operations inside this transaction can cause strange exceptions or even fall the system into stuck. Remember, that the commands inside can be performed several times.
- Specified by:
runAndRetry
in interfaceDBFunctionsProvider
- Parameters:
action
- action (class that implements procedureDBActionNoResults.run(jetbrains.buildServer.serverSide.db.DBFunctions)
) to perform.- See Also:
DBFunctionsProvider.runAndRetry(DBAction)
,DBFunctionsProvider.withDB(DBActionNoResults)
,RetryException
-
runAndRetry
public <T> T runAndRetry(@NotNull DBAction<T> action)
Description copied from interface:DBFunctionsProvider
Runs the given function in its own transaction.If this function became a deadlock victim or when
RetryException
occurred, it rolls the transaction back and re-runs the function (may be several times). Then, when success, commits the transaction.If an exception (not deadlock) occurred during the execution, the transaction will be rolled back the the occurred exception is thrown.
Warning! - there must be only database works with the given DBF instance inside the transaction. Using other database connections or performing file system operations inside this transaction can cause strange exceptions or even fall the system into stuck. Remember, that the commands inside can be performed several times.
- Specified by:
runAndRetry
in interfaceDBFunctionsProvider
- Type Parameters:
T
- type of returning value.- Parameters:
action
- action (class that implement functionDBAction.run(jetbrains.buildServer.serverSide.db.DBFunctions)
) to perform.- Returns:
- value returned by the function
DBAction.run(jetbrains.buildServer.serverSide.db.DBFunctions)
. - See Also:
DBFunctionsProvider.runAndRetry(DBActionNoResults)
,DBFunctionsProvider.withDB(DBAction)
,RetryException
-
getGuidGenerator
@NotNull public GuidGenerator getGuidGenerator()
-
getDialect
@NotNull public SQLDialect getDialect()
Description copied from interface:DBFunctionsProvider
Provides with SQL functions.- Specified by:
getDialect
in interfaceDBFunctionsProvider
- Returns:
- an instance of
SQLDialect
object, probably a shared instance.
-
getDatabaseType
@NotNull public DatabaseType getDatabaseType()
-
getDataSource
@Nullable public DataSource getDataSource()
-
getConnectionUrl
@NotNull public String getConnectionUrl()
-
getJdbcDrivers
@NotNull public JdbcDrivers getJdbcDrivers()
-
getExistingPermanentTables
@Nullable public com.google.common.collect.ImmutableSortedSet<String> getExistingPermanentTables()
-
getHeartbeat
@Nullable public Heartbeat getHeartbeat()
-
getHeartbeatStatus
@NotNull public String getHeartbeatStatus()
-
isHeartbeatPulsing
public boolean isHeartbeatPulsing()
-
getLastHeartbeatPulseTime
@Nullable public Date getLastHeartbeatPulseTime()
-
getLastHeartbeatException
@Nullable public Throwable getLastHeartbeatException()
-
-