Repository navigation
[8.x] Support read replicas via play.ebean.readOnlyDatasources - #893
Merged
Merged
Conversation
Ebean can run queries that are executed outside of a transaction against
a separate read-only DataSource, typically a read replica. However,
play-ebean only ever provided the main DataSource to Ebean, so there was
no way to set up a read replica via Play's configuration.
The new play.ebean.readOnlyDatasources setting maps an Ebean server to a
Play database, whose DataSource then becomes the read-only DataSource of
that Ebean server, e.g.:
db.replica.url = "jdbc:postgresql://replica-host/app"
play.ebean.readOnlyDatasources.default = "replica"
This way Play manages the read replica's connection pool like that of
any other database.
Also run the tests sequentially, because Ebean registers databases
JVM-wide by name and the tests start applications using the same names.
Contributor
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
Explicitly read-only transactions, e.g. TxScope.required().setReadOnly(true), use the read-only DataSource as well. Say so in the docs and cover it in the test.
This was referenced Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #892 to
8.x.Ebean can run queries that are executed outside of a transaction, as well as explicitly read-only transactions, against a separate read-only
DataSource, typically a read replica. However, play-ebean only ever handed Ebean the mainDataSource(Play's connection pool), so there was no way to set up a read replica via Play's configuration:db.default.readOnlyUrlis not a Play setting and gets silently ignored.datasource.default.readOnlyUrlvia Ebean's own properties makes Ebean create a separate connection pool, which fails withDataSource user is not set?unless the credentials get duplicated into Ebean's properties as well, because play-ebean never fills Ebean's mainDataSourceConfig.ServerConfigStartupadditionally requires a plain JDBC URL. A Play stylepostgres://...URL makes the JDBC driver return anullconnection, which leads to theNullPointerExceptionreported in Cannot configure Ebean read replica with Play configuration #566.This PR adds the
play.ebean.readOnlyDatasourcessetting, which maps an Ebean server to a Play database. That database'sDataSourcethen becomes the read-onlyDataSourceof the Ebean server:This way Play manages the read replica's connection pool like that of any other database (credentials, URL formats,
hikaricpsettings, shutdown, ...). TheDataSourceis passed to Ebean without the wrapper play-ebean uses for the mainDataSource(which disables auto-commit), since Ebean's implicit read-only transactions work best with auto-commit (Play's default).Misconfigurations fail at startup with a
ConfigExceptioninstead of being silently ignored: an unknown Play database name as well as an unknown Ebean server name.Ebean supports only one read-only
DataSourceper Ebean server. The docs explain how to spread reads across multiple read replicas via the JDBC URL (e.g. PostgreSQL'sloadBalanceHosts/targetServerType), a load balancer or a reader endpoint.The tests now run sequentially (
Test / parallelExecution := false), because Ebean registers databases JVM-wide by name and the test classes start applications using the same Ebean server name.Testing
EbeanReadOnlyDatasourceTest,EbeanParsedConfigTest), passing on Java 17 and 11.