Joel Joel - 1 year ago 115
Java Question

How to override persistence.xml properties in OpenJPA

I have the following property in my persistence.xml :

<property name="openjpa.ConnectionProperties"
value="DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/c,user=foo,password=foo,autocommit=false,automaticTestTable=testtable,idleConnectionTestPeriod=60"/>


I am trying to override it using a system property, as per the docs, so I have set:

-Dopenjpa.ConnectionProperties=DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/bar,user=bar,password=bar,autocommit=false,automaticTestTable=testtable,idleConnectionTestPeriod=60


But it doesn't work: OpenJPA always reads the property value from persistence.xml

Only when the property in persistence.xml is removed does it read the value from the system property.

Is this expected behaviour and if so what's the correct way to override a property from persistence.xml?

Answer Source

OpenJPA doesn't look at SystemProperties by default when creating an EM/EMF. Try passing in System.getProperties() in when creating your EMF.

Persistence.createEntityManagerFactory("pu_Name", System.getProperties());

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download