Setting JPA persistence properties dynamically

Viewed 12973

Let's say I have the following persistence.xml with the connection url, user & password all hard-coded.

The following is for Hibernate 3.2. For Hibernate 3.5 ++, we have to change "hibernate.connection" to "javax.persistence". But let me ask this question regardless of the literals "hibernate.connection" or "javax.persistence".

<persistence-unit name="obamacare" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="hibernate.archive.autodetection" value="class, hbm"/>
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="hibernate.connection.url" value="blah blah blah"/>
    <property name="hibernate.connection.username" value="careuser"/>
    <property name="hibernate.connection.password" value="carepass"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
    <property name="hibernate.show_sql" value="true"/>
  </properties>
  </persistence-unit>
</persistence>

However, we need to set the url, user & password dynamically. There is a proposed authentication server which serves the url, user & password. So that we do not need to individually configure the myriads of webapps that use some form of jdbc, hibernate or JPA. Besides the security issue of not wanting to store/manage passwords on visible text files.

As far as JPA is concerned, how can I set these JPA properties dynamically? I am seeking two sets of answers:

  1. For a solution that is JPA vendor independent (toplink, eclipselink, hibernate, etc) - Is there any JPA functionality that would let me set these three properties dynamically?

  2. If I am allowed to depend totally on Hibernate, besides the possible JPA only avenue, is there a way to achieving this without involving Spring framework (which seems like a huge monstrosity with tentacles all over the place)?

I would be elated if you also wish to throw in two cents/quids/rupees on JNDI and how I could use it to replace the functionality of persistence.xml properties. However, that is not the priority of the question.

2 Answers
Related