Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - EclipseLink - How to configure Database Schema name at runtime

I implement a Web Application (JEE6, EJB WebProfile) that uses an Oracle DB. My Problem is, that I need to change the used Database Schema (name) without recompile/repackage the application. So what I want (this is only an idea, maybe someone has an better one), is to have some configuration (JNDI) within the Server, that specifics the Schema name. But how configure Eclipse Link to use an other schema name at runtime?

Details:

At the moment I use the orm.xml file to specify the Schema name. But the Application uses three different Schema names (one for development, one for integration test, and one for production), so I need to compile and package (maven) the application 3 times.

I have a JEE6 EJB WebProfile Application running on an Glassfish with using an Oracle DB and the Database Connection is handled by the Application Server and provieded to the Application via JNDI.

Does any body has an idea how to configure the database schema name at runtime.

like image 462
Ralph Avatar asked May 10 '12 12:05

Ralph


2 Answers

You can use an EclipseLink SessionCustomizer.

package some.java.package;

import org.eclipse.persistence.config.SessionCustomizer; 
import org.eclipse.persistence.sessions.Session; 
import org.eclipse.persistence.sessions.DatabaseLogin; 

public class MySessionCustomizer implements SessionCustomizer {

  private String schema = "some_schema";
  public MySessionCustomizer() {
      schema = ... // read from property, jndi, etc.
  }

  public void customize(Session session) { 
      session.getLogin().setTableQualifier(schema);
  } 
}
like image 140
Billy Bob Bain Avatar answered Sep 29 '22 08:09

Billy Bob Bain


Configure JPA to use a datasource. Then you can configure different datasources for the applications in your app server.

Alternatively you can create an EntitymanagerFactory by passing a set of properties: http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManagerFactory.html#createEntityManager%28java.util.Map%29

like image 40
Jens Schauder Avatar answered Sep 29 '22 10:09

Jens Schauder