Configuration
Connection configuration
Configuration for how to connect to the database and which dialect to use is located under akka.persistence.r2dbc.connection-factory
.
Selecting a database dialect is done by first assigning one of the existing dialect blocks and then overriding specific configuration keys to specific values for your environment:
- Postgres
-
source
akka.persistence.r2dbc.connection-factory = ${akka.persistence.r2dbc.postgres} akka.persistence.r2dbc.connection-factory { host = "localhost" host = ${?DB_HOST} database = "postgres" database = ${?DB_NAME} user = "postgres" user = ${?DB_USER} password = "postgres" password = ${?DB_PASSWORD} # ssl { # enabled = on # mode = "VERIFY_CA" # root-cert = "/path/db_root.crt" # } }
- Yugabyte
- H2
- SQLServer
Full set of settings that can be overridden for each of the dialects, and their default values:
- Postgres
-
source
# the connection can be configured with a url, eg: "r2dbc:postgresql://<host>:5432/<database>" url = "" # The connection options to be used. Ignored if 'url' is non-empty host = "localhost" port = 5432 database = "postgres" user = "postgres" password = "postgres" ssl { enabled = off # See PostgresqlConnectionFactoryProvider.SSL_MODE # Possible values: # allow - encryption if the server insists on it # prefer - encryption if the server supports it # require - encryption enabled and required, but trust network to connect to the right server # verify-ca - encryption enabled and required, and verify server certificate # verify-full - encryption enabled and required, and verify server certificate and hostname # tunnel - use a SSL tunnel instead of following Postgres SSL handshake protocol mode = "" # Server root certificate. Can point to either a resource within the classpath or a file. root-cert = "" # Client certificate. Can point to either a resource within the classpath or a file. cert = "" # Key for client certificate. Can point to either a resource within the classpath or a file. key = "" # Password for client key. password = "" } # Maximum time to create a new connection. connect-timeout = 3 seconds # Configures the statement cache size. # 0 means no cache, negative values will select an unbounded cache # a positive value will configure a bounded cache with the passed size. statement-cache-size = 5000 # Abort any statement that takes more than the specified amount of time. # This timeout is handled by the database server. # This timeout should be less than `close-calls-exceeding`. statement-timeout = off # Possibility to programatically amend the ConnectionFactoryOptions. # Enable by specifying the fully qualified class name of a # `akka.persistence.r2dbc.ConnectionFactoryProvider.ConnectionFactoryOptionsProvider`. # The class can optionally have a constructor with an ActorSystem parameter. options-provider = ""
- Yugabyte
- H2
- SQLServer
Connection pool settings are the same across the different dialects, but are defined in-line in the connection factory block:
akka.persistence.r2dbc.connection-factory = {
# Initial pool size.
initial-size = 10
# Maximum pool size.
max-size = 30
}
The following connection pool settings be overridden directly in the connection-factory
block:
source# Initial pool size.
initial-size = 5
# Maximum pool size.
max-size = 20
# Maximum idle time of the connection in the pool.
# Background eviction interval of idle connections is derived from this property
# and max-life-time.
max-idle-time = 30 minutes
# Maximum lifetime of the connection in the pool.
# Background eviction interval of connections is derived from this property
# and max-idle-time.
max-life-time = 60 minutes
# Maximum time to acquire connection from pool.
acquire-timeout = 5 seconds
# Number of retries if the connection acquisition attempt fails.
# In the case the database server was restarted all connections in the pool will
# be invalid. To recover from that without failed acquire you can use the same number
# of retries as max-size of the pool
acquire-retry = 1
# Validate the connection when acquired with this SQL.
# Enabling this has some performance overhead.
# A fast query for Postgres is "SELECT 1"
validation-query = ""
# Maximum SQL statement execution duration. The current connection is closed if exceeded,
# and will not be reused by the pool.
# This timeout is handled on the client side and should be used in case the database server
# is unresponsive or the connection is broken but not closed.
# It can be used in combination with `statement-timeout`, which should be less than this
# timeout.
# The timeout is needed to handle some failure scenarios, when the database server is
# terminated in a non graceful way and there is a load balancer in front. The client
# connection and current execution in progress would not be completed without this timeout,
# resulting in a "connection leak".
# Set to "off" to disable this timeout.
close-calls-exceeding = 20 seconds
Journal configuration
Journal configuration properties are by default defined under akka.persistence.r2dbc.journal
.
See Journal plugin configuration.
Snapshot configuration
Snapshot store configuration properties are by default defined under akka.persistence.r2dbc.snapshot
.
See Snapshot store plugin configuration.
Durable state configuration
Durable state store configuration properties are by default defined under akka.persistence.r2dbc.state
.
See Durable state plugin configuration.
Query configuration
Query configuration properties are by default defined under akka.persistence.r2dbc.query
.
See Query plugin configuration.
Multiple plugins
To enable the plugins to be used by default, add the following lines to your Akka application.conf
:
sourceakka.persistence.journal.plugin = "akka.persistence.r2dbc.journal"
akka.persistence.snapshot-store.plugin = "akka.persistence.r2dbc.snapshot"
akka.persistence.state.plugin = "akka.persistence.r2dbc.state"
Note that all plugins have a shared root config section akka.persistence.r2dbc
, which also contains the Connection configuration for the connection pool that is shared for the plugins.
You can use additional plugins with different configuration. For example if more than one database is used. Then you would define the configuration such as:
sourcesecond-r2dbc = ${akka.persistence.r2dbc}
second-r2dbc {
# chose dialect unless using the same as the default
# connection-factory = ${akka.persistence.r2dbc.postgres}
connection-factory {
# specific connection properties here
# Note if using H2 and customizing table names you will need to repeat the custom table names
# for the second config in this config block, see reference.conf for the table name config keys.
}
journal {
# specific journal properties here
}
snapshot {
# specific snapshot properties here
}
state {
# specific durable state properties here
}
query {
# specific query properties here
}
}
To use the additional plugin you would override the plugin id.
- Scala
- Java
-
source
public class MyEntity extends EventSourcedBehavior<MyEntity.Command, MyEntity.Event, MyEntity.State> { @Override public String journalPluginId() { return "second-r2dbc.journal"; } @Override public String snapshotPluginId() { return "second-r2dbc.snapshot"; } }
It is similar for DurableStateBehavior
, override durableStateStorePluginId
with "second-r2dbc.state"
.
For queries and Projection SourceProvider
you would use "second-r2dbc.query"
instead of the default R2dbcReadJournal.Identifier()
.
For additional details on multiple plugin configuration for projections see the Akka R2DBC projection docs