Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Databases with Play Framework 2.1.x

I have 2 databases that I need to connect to. I can easily connect to them in the application.conf file like so:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/db1"
db.default.user=postgres
db.default.password="password"

db.secondary.driver=org.postgresql.Driver
db.secondary.url="jdbc:postgresql://localhost/db2"
db.secondary.user=postgres
db.secondary.password="password"

ebean.default="models.db1.*"
ebean.secondary="models.db2.*"

I have my model classes in those packages, and it DDL generates the tables properly.

The problem lies in actually working with these entities. Anything not in the "default" package throws this error (using the Users table in the secondary database as an example)

If I try to query all the rows of the table:

List<Users> users = Users.find.all();

It throws this error:

[PersistenceException: models.db2.Users is NOT an Entity Bean registered with this server?]

Even though I am 100% sure that the Users table is there in the backend, it is a registered table the DDL works and makes this table properly, and I am importing the proper classes.

Is there a certain way I need to query model classes that aren't in the default package?

EDIT: I realize that the stack trace shows that it's trying to use the DefaultServer. How can I make it use the secondary server?

    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:989) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:946) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.find(DefaultServer.java:982) ~[avaje-ebeanorm-server.jar:na]
    at play.db.ebean.Model$Finder.all(Model.java:254) ~[play-java-ebean_2.10.jar:2.1.3]
like image 291
Bert B. Avatar asked Aug 19 '13 01:08

Bert B.


People also ask

Can an application have multiple databases?

Even if your company is still relatively small, it may already be in the process of outgrowing the database that you started with. As this happens, new applications will interface with a larger and more powerful database.

Can a project have multiple databases?

Generally, if one project consumes multiple databases, it is because it must consume different, often legacy sources of information that originated outside of this particular project. This is most common in Enterprise environments.

Can we connect 2 database in web application?

Yes. Give it a go. "Is it possible to connect multiple database with one application." Yes. Very straightforward - two connection strings, two different connections.

Is Play framework backend?

Play comes with two configurable server backends, which handle the low level work of processing HTTP requests and responses to and from TCP/IP packets. Starting in 2.6. x, the default server backend is the Akka HTTP server backend, based on the Akka-HTTP server. Prior to 2.6.


1 Answers

Ok, your application.conf seems to be correct.

You may use the secondary server like this:

EbeanServer secondary = Ebean.getServer("secondary");
secondary.find(User.class).findList();

Once you've got your secondary server, you may treat it just as you treat the Ebean singleton.

like image 172
serejja Avatar answered Oct 02 '22 16:10

serejja