简体   繁体   中英

How to define - environment specifc mongo db configuration in play framework with JpaApi?

I am working on a project where I am using play framework along with mongo db. As of now I have hardcoded the value for local db connection in persistence.xml file and given the jpa.default value as persistenceUnitName and I am using the play's JpaApi for the db operations (which inherently uses the entity manager).

I am not able to identify how to define environment (prod, dev, stage) specific db properties like host, url etc. in application.conf or any other file.

application.conf entry - jpa.default=my-local-jpa

 <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="my-local-jpa" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties> <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/> <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/> <property name="hibernate.ogm.datastore.port" value="27017"/> <property name="hibernate.ogm.datastore.database" value="my_db"/> <property name="hibernate.ogm.datastore.safe" value="true"/> <property name="hibernate.ogm.datastore.create_database" value="true" /> </properties> </persistence-unit> </persistence> 

There would be different solutions. It depends on your environment.

If you are using WildFly / JEE container, you can configure a WildFly NoSQL subsystem, providing there the references to the remote datastore. It would be the equivalent of a SQL datasource, but for a NoSQL datastore. See Using WildFly NoSQL

If you are using a web container, there would be different strategies. You can create different war(s), one for each environment, for instance using maven profiles. Alternatively, you can configure your Spring context in order to use an external property file. See this question .

If you deploy it in a PASS, such as OpenShift, you can mount the persistence.xml file as a config map. See Config Map - OpenShift doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM