2

I am working with Play! 2.4 and I am using Ebean as the ORM tool. All is good when i configure ebean through application.conf and use the default Ebeanserver created by play ebean.

What I would like to achieve though, is to be able to configure Ebeanserver dynamically and inject it. The application can be deployed in different production environments, in each environment the url, username, password may change. So I want to store the parameters that can vary in a separate configuration file and build the Ebeanserver object programatically.

I have tried followed this link: http://ebean-orm.github.io/docs/setup/guice , but I have not been successful. My configuration looks as follows:

EbeanServerProvider.java

public class EbeanServerProvider implements Provider<EbeanServer> {

    @Override
    public EbeanServer get() {

        ServerConfig config = new ServerConfig();
        config.setName("mysql");
        config.loadFromProperties();
        config.setDefaultServer(true);
        return EbeanServerFactory.create(config);
    }
}

binding the module using guice

bind(EbeanServer.class).toProvider(EbeanServerProvider.class).asEagerSingleton();

ebean.properties

datasource.default = mysql  
datasource.mysql.databaseDriver=com.mysql.jdbc.Driver
datasource.mysql.databaseUrl="jdbc:mysql://localhost/playdb"
datasource.mysql.username=root
datasource.mysql.password=""

application.conf

ebean.default = ["models.*]
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/playdb"
db.default.username=root
db.default.password=""

I guess the problem is that I am not clear on what configuration is needed by play and what is needed by ebean. There is not a lot of good documentation on using ebean and play with Dependency injection. Any help is greatly appreciated.

jesukumar
  • 1,139
  • 9
  • 19
  • I'm learning Play 2.4 + Ebean, and I realize that it's not necessary to configure again datasource.* on ebean.properties. It's enough with db.default.* on application.conf – Mario S Jan 17 '16 at 14:09

1 Answers1

0

Short answer:

The database configs you need are the ones you listed under application.conf. Create a new application.conf per server with the server's specific database connection info. These would not be committed to your projects git repository for security reasons.

Detailed answer:

If you are deploying the same application in different environments you can create a configuration per environment and stash it somewhere like /etc/default/. In your build.sbt, add a check for a the conf file on the server, like /etc/default/myproject.conf, your specific database connection info can go in there.

Update your build.sbt to check the conf. (Note, ${name.value} becomes the project name):

name := """myproject"""

javaOptions in Universal ++= Seq(
    s"-Dconfig.file=/etc/default/${name.value}.conf"
)

Pull down your latest changes from git on the server and run activator stage. When you run the executable ./target/universal/stage/bin/myproject, it will check for your server specific conf and use those settings.

If you want to set and use some default configurations in the project's application.conf, just add include "application.conf" at the top of your server specific conf. Your custom conf will override anything that was set in your default application.conf.

The /etc/default/myproject.conf file will be structured exactly the same way as your default application.conf - Just put the real connection values for that server in there.

Remember, there is an ebean plugin PlayEbean that you can add to the build.sbt file that will allow ebean to just work. I'm not totally clear on what you're trying to accomplish but it may not be necessary.

I hope I didn't totally miss what you were trying to ask.

BatteryAcid
  • 8,381
  • 5
  • 28
  • 40
  • Thank for the reply. I don't particularly prefer storing the conf files on the server itself. I may have not been entirely clear in my question here. I only recently got a clear picture of what i wanted to do, and so I created a new question . Here is the link: http://stackoverflow.com/questions/34350725/how-can-i-override-plays-default-ebean-server-configuration . – jesukumar Dec 18 '15 at 08:01