5

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • You can create script that would output 3 artifacts for each environment with the same source code but different orm.xml so you won't need to change schema name at runtime – Timmo May 10 '12 at 12:11
  • Refer http://stackoverflow.com/q/5104185/366964 & http://stackoverflow.com/q/9315593/366964 might help. – Nayan Wadekar May 10 '12 at 12:21

2 Answers2

8

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);
  } 
}
zb226
  • 9,586
  • 6
  • 49
  • 79
Billy Bob Bain
  • 2,894
  • 18
  • 13
1

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

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348