playframework - Declaring non-default JNDI connection for JPA access in Play for Scala -
the following snippet works in play scala:
class mydao @inject() (jpaapi: jpaapi) { @transactional def somemethod = { jpaapi.withtransaction { // ....
in application.conf
defined db.default.jndiname=defaultds
, jpa.default=defaultpersistenceunit
.
now, need define jndi connection db.another.jndiname=anotherds
jpa.another=anotherpersistenceunit
.
where persistence.xml
is:
<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.hanacolumnstoredialect"/> </properties> </persistence-unit> <persistence-unit name="anotherpersistenceunit" transaction-type="resource_local"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <non-jta-data-source>anotherds</non-jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.hanacolumnstoredialect"/> </properties> </persistence-unit> </persistence>
how inject anotherds
in application can used jpaapi?
you can specify multiple jpa configurations in application.conf
:
db.default.jndiname=defaultds ... // other configuration db.default jpa.default=defaultpersistenceunit db.another.jndiname=anotherds ... // other configuration db.another jpa.another=anotherpersistenceunit
in dao, inject jpaapi
you're doing. use jpaapi#em(string)
entitymanager
specific persistence unit name:
class mydao @inject() (jpaapi: jpaapi) { def somemethodwithdefault = { val em = jpaapi.em("default") // em entitymanager jpaapi.withtransaction { ... } } def somemethodwithanother = { val em = jpaapi.em("another") // em entitymanager jpaapi.withtransaction { ... } }
also, @transactional
annotation unnecessary if you're using jpaapi#withtransaction
.
Comments
Post a Comment