Ralph
Ralph

Reputation: 120811

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.

Upvotes: 5

Views: 7077

Answers (2)

Billy Bob Bain
Billy Bob Bain

Reputation: 2895

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);
  } 
}

Upvotes: 8

Jens Schauder
Jens Schauder

Reputation: 81940

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

Upvotes: 1

Related Questions