Using read-only DataSource in Play with Play-Ebean plugin

282 views
Skip to first unread message

michae...@lookout.com

unread,
Sep 8, 2021, 1:27:56 PM9/8/21
to Ebean ORM
Hi,

I'm interested in using the read-only datasource feature added by https://github.com/ebean-orm/ebean/issues/1232.  I'm using Play Framework with the Play-Ebean plugin and HikariCP for connection pooling.  I can't quite figure out how to enable the read-only feature, though.  Since I'm not explicitly creating my default datasource (i.e. I'm handling it through config), I'm not sure where to hook in and setup the read-only datasource.  Any suggestions?

Thanks,
Mike

Rob Bygrave

unread,
Sep 8, 2021, 5:01:40 PM9/8/21
to ebean@googlegroups

>  hook in and setup the read-only datasource

On DatabaseConfig we call databaseConfig.setReadOnlyDataSource() passing the DataSource. Noting this DataSource should be autoCommit=true.



Ebean will then automatically use that DataSource when queries are executed outside of an explicit transaction.  Also when we use @Transactional(readOnly=true) that will  also use the read only DataSource.

Note that there is a performance benefit of using the read-only DataSource even when hitting the same database (rather than a read replica database) due to the ability to use autoCommit=true [more noticeable when there are lots of fast queries].


> HikariCP for connection pooling

Just a note to say that a team I am working in has migrated away from HikariCP to Ebean's pool due to Ebean's pool being more robust at handling RDS instance upgrade/failover. That is, one of the nice tricks in the Ebean pool is that it validates on error (and in the background) - it hence does not need any validate on borrow/return. This approach makes the ebean pool very robust and also fast.  That plus it auto-sizes itself and is fast due to the simple but fast internal "array slot" style buffer.  Anyway, by all means use HikariCP but note that I am comfortable stating that Ebean's DataSource is in my observations more robust and definitely what I recommend to people.

Fun fact, Ebean's DataSource implementation is actually older than Ebean at around 18 years old - Ebean is around 15 years old.


Cheers, Rob.

--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ebean/e59a3da9-45cf-4800-a8eb-fc04d03a1469n%40googlegroups.com.

Michael Mole

unread,
Sep 8, 2021, 8:36:58 PM9/8/21
to eb...@googlegroups.com
Thanks for the info, and I'll definitely consider using Ebean's pool!

One followup question - Do you know what the Play/Guice mechanism is for getting access to the DatabaseConfig object?

Thanks again,
Mike

You received this message because you are subscribed to a topic in the Google Groups "Ebean ORM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ebean/au-AdBKFoF8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ebean+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ebean/CAC%3Dts-F0cXJdAcsi7OiU83OBrpDfYGyLD2exQqo%3D-eAhFpWrpQ%40mail.gmail.com.


--


Mike Molé (he/him/his)
Senior Staff Software Engineer
michae...@lookout.com
 +1-908-770-0816 | www.lookout.com

------------------------------------------------------
% gpg --keyserver keys.gnupg.net --recv-key 1E682FE5
------------------------------------------------------

Rob Bygrave

unread,
Sep 8, 2021, 8:51:31 PM9/8/21
to ebean@googlegroups
> Do you know what the Play/Guice mechanism is for getting access to the DatabaseConfig object?

No  I don't.  I am hoping there is another helpful person that can jump in and help with that.

Cheers, Rob.

michae...@lookout.com

unread,
Sep 13, 2021, 10:22:06 AM9/13/21
to Ebean ORM
I figured out what to do.  If you're using the Play-Ebean plugin, you need to implement https://github.com/payintech/play-ebean/blob/master/play-ebean/src/main/java/play/db/ebean/orm/EbeanServerExtraConfig.java.  Then register the fully qualified classname in the extra-config section as described here, https://github.com/payintech/play-ebean/blob/master/README.md#configure-the-module.  This will allow you to programmatically adjust the config before the Ebean server objects are created.

The other hiccup I encountered was that I used Play configuration to create a read-only Hikari pool/datasource.  Since the EbeanServerExtraConfig does not support injecting that dependency, I accessed it via JNDI instead.

For example:

public class CustomEbeanServerExtraConfig implements EbeanServerExtraConfig {

    private static final Logger.ALogger logger = Logger.of(CustomEbeanServerExtraConfig.class);

    @Override
    public void applyExtraConfiguration(ServerConfig ebeanServerConfig, Config playConfig) {
        try {
            DataSource roDs = (DataSource) play.api.libs.JNDI.initialContext().lookup("readOnly");
            ebeanServerConfig.setReadOnlyDataSource(roDs);
            logger.info("Configured read-only datasource");
        } catch (NamingException e) {
            logger.warn("Unable to configure read-only datasource");
        }
    }
}

---

(from application.conf)
      # Extra Ebean server configuration
      # Use full classpath (ie: ebean.DemoEbeanServerExtraConfig)
      extra-config = [
        db.CustomEbeanServerExtraConfig
      ]

---

(from my env-specific application.conf extension)
db {
  default {
    driver = "org.mariadb.jdbc.Driver"
    url = "jdbc:mysql://<aws-aurora-writer-endpoint>:3306/mydb"
    username = <redacted>
  }

  readOnly {
    driver = "org.mariadb.jdbc.Driver"
    url = "jdbc:mysql://<aws-aurora-reader-endpoint>:3306/mydb"
    username = <redacted>
    readOnly = true
    jndiName = readOnly
  }
}


Rob Bygrave

unread,
Sep 13, 2021, 4:30:24 PM9/13/21
to ebean@googlegroups
Excellent.  Thanks for posting back those details, that will help others down the track.

Cheers, Rob. 

Reply all
Reply to author
Forward
0 new messages