---
title: 'Realms'
weight: 50
---
## Realms
Realms implement multi-tenant capabilities. A Strolch agent can have an
arbitrary number of realms configured and each realm has its own persistence
configuration, allowing to separate mandates completely.
A realm can run in one of the following modes:
* EMPTY
This is a transient data store mode, where no model changes are
persisted - they are only kept in memory. When the Strolch agent is
started, this realm is empty as no data is loaded.
* TRANSIENT
This is the same as EMPTY, but with the difference that when the Strolch
agent is started, a model file is parsed and the in-memory realm is
populated with the elements parsed from the model file.
* CACHED
In this mode, all data is stored in-memory, and any changes made are
written back to the persistence layer. This allows for fast in-memory
qeuries, but makes sure no data is lost when the agent is restarted.
Realms are mostly hidden from a developer as a `StrolchTransaction` exposes
all important operations needed to access Strolch objects. A developer will
however need to configure the realms for their specific project. If the
project only requires one realm, then the `defaultRealm` can be used, where the
developer only is required to configure the mode and any relevant model file.
If the mode is `CACHED`, then the `PersistenceHandler` component is required to be
configured, so that the DAOs know how to access the underlying database.
The configuration in the `StrolchConfiguration.xml` file is as follows:
```xml
...
RealmHandler
li.strolch.agent.api.RealmHandler
li.strolch.agent.impl.DefaultRealmHandler
PrivilegeHandler
EMPTY|TRANSIENT|CACHED
StrolchModel.xml
...
```
## Multi-Realm
A multi-realm configuration would be as follows.
{{% notice tip %}}
Note how the defaultRealm is still enabled, and has its configuration as
before. Further the PostgreSQL PersistenceHandler is configured to show how the
realms are connected to the persistence handler:
{{% /notice %}}
```xml
...
RealmHandler
li.strolch.agent.api.RealmHandler
li.strolch.agent.impl.DefaultRealmHandler
PrivilegeHandler
PersistenceHandler
defaultRealm, cachedRealm
TRANSIENT
DefaultRealm.xml
CACHED
EMPTY
PersistenceHandler
li.strolch.persistence.api.PersistenceHandler
li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler
true
true
jdbc:postgresql://localhost/testdb2
testuser2
test
1
...
```
## Access realm
Accessing a realm is done in multiple ways. Important is to note, that a user
should use the `StrolchTransaction` object, instead of accessing the Realm directly.
Opening a transaction is done from a `Service` by calling one of the
`openTx()`-methods. Nevertheless, the realm can be accessed as follows:
```java
ComponentContainer container = getAgent().getContainer();
StrolchRealm realm = container.getRealm(StrolchConstants.DEFAULT_REALM);
try(StrolchTransaction tx = realm.openTx()) {
Resource resource = tx.getResourceBy("TestType", "MyTestResource");
...
}
```