Do we support multiple database connection in same project using JPA in Play 2.4.x?

189 views
Skip to first unread message

Mitesh Sharma

unread,
Aug 13, 2015, 2:26:46 AM8/13/15
to play-framework
Hey guys,
Do we support multiple databases in single project in play 2.4.x using JPA? If yes, can someone point me to right documentation. I am not able to find it. If some github project is pointed out it will be of great help.

Thanks in anticipation.
Regards,
Mitesh

Mitesh Sharma

unread,
Aug 16, 2015, 5:20:19 AM8/16/15
to play-framework
Bumping this, anyone has any link for 2.4.x for multiple db support.

Johan Dahlberg

unread,
Aug 18, 2015, 6:32:44 AM8/18/15
to play-framework
Yes, Play support multiple databases.

For example in application.conf
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:file:E:/myproject/setup/db/monitor"
db.default.user=sa
db.default.password=sa
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

db.seconddb.driver=com.mysql.jdbc.Driver
db.seconddb.url="jdbc:mysql://localhost/dbname"
db.seconddb.username="root"
db.seconddb.password="secret"
db.seconddb.jndiName=SecondDS
jpa.seconddb=secondPersistenceUnit

And when you use JPA to access the EntityManager you write like this:

JPA.em("default")
JPA.em("seconddb")

/Johan Dahlberg

mitesh sharma

unread,
Aug 18, 2015, 6:39:26 AM8/18/15
to play-fr...@googlegroups.com
Thanks a lot Johan. What is difference between file secondPersistenceUnit and defaultPersistenceUnit.

My defaultPersistenceUnit file mentioned below. 

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>

</persistence>

Regards,
Mitesh

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/IxKmJ7lUGEE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/87d460ac-a2a4-4c2e-ae43-4c54db45b41e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Johan Dahlberg

unread,
Aug 18, 2015, 6:51:46 AM8/18/15
to play-fr...@googlegroups.com

I'm not sure if two separate are necessary because I haven't tried using two databases. But since you have a data source defined in the default I guess that you need to have a separate where you have the other data source.

/Johan Dahlberg


mitesh sharma

unread,
Aug 18, 2015, 7:07:13 AM8/18/15
to play-fr...@googlegroups.com
Thanks a lot, will give it a try. 

Reply all
Reply to author
Forward
0 new messages