Class SystemPersistenceManagerImpl
java.lang.Object
de.xima.fc.persistence.impl.SystemPersistenceManagerImpl
- All Implemented Interfaces:
SystemPersistenceManager
@Eager
@ApplicationScoped
public class SystemPersistenceManagerImpl
extends Object
implements SystemPersistenceManager
Implementation of the SystemPersistenceManager interface. This class is responsible for managing the lifecycle of the
EntityManagerFactory for the system persistence unit, as well as providing access to the DataSource and handling
schema and encryption migrations. It listens to events related to the health of the system database and reacts
accordingly by opening or closing the EntityManagerFactory. It also provides methods to suspend and resume the
persistence layer, which can be used during maintenance operations. The class ensures that all operations that modify
the state of the persistence layer are synchronized to prevent concurrent modifications and ensure thread safety.
- Since:
- 8.5.0
- Author:
- Michael Kohlsche
-
Field Summary
Fields inherited from interface SystemPersistenceManager
PERSISTENCE_UNIT_NAME -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidEnsures that the encryption test value is present and valid in the system database.Retrieves the DataSource for the system database.getDataSourceEncryptionState(ConnectionProperties connectionProperties, char[] password, String encryptionAlgorithm) Checks the encryption state of the data source defined by the given connection properties.Retrieves information about the system data source, such as database type, version, encryption settings, and other metadata.javax.persistence.EntityManagerFactoryRetrieves the EntityManagerFactory for of the system database.de.xima.fc.persistence.impl.EntityManagerFactoryRuntimebooleanbooleanbooleanvoidmigrateEncryption(char[] newEncryptionPassword, String newEncryptionAlgorithm, Set<String> additionalEncryptionScopes) Migrates the encryption of the system data source to use a new encryption password and algorithm.voidUpdates the schema of the system database to the latest version.voidOpens a new JPA transaction for the system database.voidReloads the encryption settings for the system data source.voidvoidresume()Resumes the system persistence layer after it has been suspended.voidsuspend()Suspends the system persistence layer, releasing any resources held by the EntityManagerFactory and DataSource.
-
Method Details
-
getDataSource
Description copied from interface:SystemPersistenceManagerRetrieves the DataSource for the system database. The returned DataSource is managed by this manager (and underlying connection pool) and should not be closed by the caller.- Specified by:
getDataSourcein interfaceSystemPersistenceManager- Returns:
- the
DataSourcefor the system database - Throws:
FcPersistenceException
-
getDataSourceInfo
Description copied from interface:SystemPersistenceManagerRetrieves information about the system data source, such as database type, version, encryption settings, and other metadata. This information can be used for diagnostics, monitoring, or to make decisions about how to interact with the database based on its capabilities and configuration.- Specified by:
getDataSourceInfoin interfaceSystemPersistenceManager- Returns:
- an
IDataSourceInfoobject containing metadata about the system data source - Throws:
FcPersistenceException- if there is an error while retrieving the data source information, such as if the data source cannot be accessed or if there is an error related to the database driver
-
getEntityManagerFactory
public javax.persistence.EntityManagerFactory getEntityManagerFactory() throws FcPersistenceExceptionDescription copied from interface:SystemPersistenceManagerRetrieves the EntityManagerFactory for of the system database. The returned EntityManagerFactory is managed by this manager and should not be closed by the caller.- Specified by:
getEntityManagerFactoryin interfaceSystemPersistenceManager- Returns:
- the EntityManagerFactory for the system database
- Throws:
FcPersistenceException
-
openTransaction
Description copied from interface:SystemPersistenceManagerOpens a new JPA transaction for the system database. The returnedIJpaTransactionrepresents a transaction context that must be closed by the caller to commit or roll back the transaction.- Specified by:
openTransactionin interfaceSystemPersistenceManager- Returns:
- a new
IJpaTransactionfor the system database - Throws:
FcPersistenceException
-
closeEntityManagerFactory
public void closeEntityManagerFactory()- Specified by:
closeEntityManagerFactoryin interfaceSystemPersistenceManager
-
openEntityManagerFactory
public void openEntityManagerFactory()- Specified by:
openEntityManagerFactoryin interfaceSystemPersistenceManager
-
suspend
public void suspend()Description copied from interface:SystemPersistenceManagerSuspends the system persistence layer, releasing any resources held by the EntityManagerFactory and DataSource. This method should be called before performing operations that require exclusive access to the database, such as schema migrations or encryption changes. After the necessary operations are completed, theSystemPersistenceManager.resume()method should be called to reinitialize the persistence layer.- Specified by:
suspendin interfaceSystemPersistenceManager
-
resume
public void resume()Description copied from interface:SystemPersistenceManagerResumes the system persistence layer after it has been suspended. This method triggers a system persistence layer check which will fire appropriate events to reinitialize the EntityManagerFactory and DataSource if they were released during suspension. This method should be called after performing operations that required exclusive access to the database, such as schema migrations or encryption changes, to restore the normal operation of the persistence layer.- Specified by:
resumein interfaceSystemPersistenceManager
-
reloadEntityManagerFactory
public void reloadEntityManagerFactory()- Specified by:
reloadEntityManagerFactoryin interfaceSystemPersistenceManager
-
reloadEncryptionSettings
Description copied from interface:SystemPersistenceManagerReloads the encryption settings for the system data source. This method should be called after changing the encryption settings (e.g., password, algorithm) to ensure that the new settings are applied and used for all subsequent database operations. This may involve reinitializing the DataSource and EntityManagerFactory to use the updated encryption configuration.- Specified by:
reloadEncryptionSettingsin interfaceSystemPersistenceManager- Throws:
FcPersistenceException- if there is an error related to the persistence layer while reloading the encryption settings, such as if the DataSource cannot be reinitialized with the new settings or if there is an error related to the database driver
-
migrateEncryption
public void migrateEncryption(char[] newEncryptionPassword, String newEncryptionAlgorithm, Set<String> additionalEncryptionScopes) throws FcPersistenceException Description copied from interface:SystemPersistenceManagerMigrates the encryption of the system data source to use a new encryption password and algorithm. This method should be used when changing the encryption settings for the system database. It reads all encrypted data with the old settings and re-encrypts it using the new settings.- Specified by:
migrateEncryptionin interfaceSystemPersistenceManager- Parameters:
newEncryptionPassword- the new encryption password to be usednewEncryptionAlgorithm- the new encryption algorithm to be usedadditionalEncryptionScopes- optional scopes for encryption; can be null or empty; if null, only unscoped data will be re-encrypted. If not null, data with the specified scopes will also be re-encrypted. Example scope: "form" for form data encryption.- Throws:
FcPersistenceException- f there is an error during the migration process
-
migrateSchema
Description copied from interface:SystemPersistenceManagerUpdates the schema of the system database to the latest version.- Specified by:
migrateSchemain interfaceSystemPersistenceManager- Throws:
SchemaMigrationException- if there is an error during the schema migration process
-
ensureEncryptionValueExists
Description copied from interface:SystemPersistenceManagerEnsures that the encryption test value is present and valid in the system database.- Specified by:
ensureEncryptionValueExistsin interfaceSystemPersistenceManager- Throws:
FcPersistenceException- if there is an error related to the persistence layer while checking the encryption value
-
getDataSourceEncryptionState
public DataSourceEncryptionState getDataSourceEncryptionState(ConnectionProperties connectionProperties, char[] password, String encryptionAlgorithm) throws FcPersistenceException Description copied from interface:SystemPersistenceManagerChecks the encryption state of the data source defined by the given connection properties. This method verifies whether the encryption settings (password, algorithm) match those used to encrypt the data in the database.- Specified by:
getDataSourceEncryptionStatein interfaceSystemPersistenceManager- Parameters:
connectionProperties- the connection properties defining the data source to checkpassword- the password to check against the system data source encryptionencryptionAlgorithm- the encryption algorithm to check against the system data source encryption- Returns:
- the encryption state of the system data source
- Throws:
FcPersistenceException
-
isSchemaMigrationRunning
public boolean isSchemaMigrationRunning() -
isEncryptionMigrationRunning
public boolean isEncryptionMigrationRunning() -
isSuspended
public boolean isSuspended() -
getEntityManagerFactoryRuntime
public de.xima.fc.persistence.impl.EntityManagerFactoryRuntime getEntityManagerFactoryRuntime()
-