How to change persistence.xml at runtime

I am new to openJPA.

I have a scenario in which, depending on the server on which my application is running, I need to change the settings to persistance.xml. E.g. if it runs on server A, then it must use different databases (different URLs), different passwords, etc., and if the application runs on server B, then it must use different information.

And you could also tell me how this should be done using a data source or just putting properties in a persistence block.

FYI I am using the WS application. server 7 and RAD 7.5

Any help would be greatly appreciated.

+3
source share
1 answer

, persistence.xml. JNDI , . EAch JNDI, - persistence.xml.

, JPA DataSources . JNDI WebSphere 6.0/6.1 WebSphere + JNDI + Spring Framework + Hibernate.

Spring? , : persistence.xml, .

:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:database.properties"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.class}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

database.properties ( ):

database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:@localhost:1521:XE

persistence.xml , , JPA .

, JNDI Spring.

+4

Source: https://habr.com/ru/post/1710420/


All Articles