eXo Platform 3.5

## Chapter 49. DBCleanService

## Description

It is special service for data removal from database. The article shortly describes the principles of work DBCleaner under all databases

### Note

Code that invokes methods of DBCleanService must have JCRRuntimePermissions.MANAGE_REPOSITORY_PERMISSION permission;

There are several methods of DBCleanerService:

public static void cleanWorkspaceData(WorkspaceEntry wsEntry) | Clean workspace data from database
public static void cleanRepositoryData(RepositoryEntry repoEntry) | Cleanup repository data from database
public static DBCleaner getWorkspaceDBCleaner(Connection jdbcConn, WorkspaceEntry wsEntry) | Returns database cleaner of workspace.
public static DBCleaner getRepositoryDBCleaner(Connection jdbcConn, RepositoryEntry repoEntry) | Returns database cleaner of repository. Returns null in case of multi-db configuration.

The cleaning is a part of restoring from backup and it is used in the following restore phases:

clean |
    dbCleaner.executeCleanScripts();
restore | does nothing with DBCleaner
commit |
    dbCleaner.executeCommitScripts();

    connection.commit();
rollback |
    connection.rollback();

    dbCleaner.executeRollbackScripts();

    connection.commit();

Different approaches are used for database cleaning depending on database and JCR configuration.

## Need to clean only single workspace

Simple cleaning records from JCR table is used in case of single-db configuration.

executeCleanScripts() | removing all records from the database. Foreign key of JCR_SITEM table is also removed
executeCommitScripts() | adding foreign key
executeRollbackScripts() |

executeCleanScripts() | removing all records from the database. Foreign key of JCR_SITEM table is also removed
executeCommitScripts() | adding foreign key
executeRollbackScripts() | adding foreign key

Either removing or renaming JCR tables are used in case of mult-db configuration.

executeCleanScripts() | removing tables JCR_MVALUE, JCR_MREF, JCR_MITEM, initializing new tables without foreign key of JCR_MITEM table, adding root
executeCommitScripts() | adding foreign key
executeRollbackScripts() |

executeCleanScripts() | renaming current tables, initializing new tables without foreign key of JCR_MITEM table, adding root node, removing indexes for some databases
executeCommitScripts() | renaming tables, adding indexes
executeRollbackScripts() | removing previously renamed tables, adding indexes, adding foreign key

## Need to clean the whole repository

In case of single-db all workspaces will be processed simultaneously as in case of single workspace multi-db configuration. For multi-db every workspace will be processed separately as in case of single workspace multi-db configuration.