tags:

views:

1192

answers:

1

Hello guys, I am new to openJPA.

I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml. For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.

And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.

FYI I am using WS app. server 7 and RAD 7.5

Any type of help would be highly appreciated.

+3  A: 

You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.

Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.

Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.

For example:

<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>

and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):

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

Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.

Of course, you can use JNDI data sources with Spring also.

cletus
Thanks for the reply cletusi AM using JSF actually with IBM RAD's openJPA.Could you explain more about how to solve my Issue, i.e if I create the datasource with the same name on each server then I need the datasource name under persistence.xml right?IF so, could you please help me wit some example.
Links added to Websphere JNDI data source examples.
cletus