1

I am using JPA with EclipseLink for PostGreSQL. Due to some product requirement I want to switch the schema runtime. I am planning to do it in "customize" method of SessionCustomizer as shown below

@Override
public void customize(Session session) throws Exception {
System.out.println("calling customize ###################");
if (this.schemaName != null || !this.schemaName.isEmpty()) {
    session.getLogin().setTableQualifier(schemaName);
}

I set MySessionCustomizer in peristence.xml.

But don't know when and who will call MySessionCustomizer's customize method. Please help me to understand this.

EDIT: Here is my persistence.xml

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="test" >
    <class>com.demo.Student</class>
    <properties>
      <property name="eclipselink.session.customizer" value="com.demo.MySessionCustomizer"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
      <property name="javax.persistence.jdbc.url"    value="jdbc:postgresql://localhost:5432/MyDB" />
      <property name="javax.persistence.jdbc.user" value="username" />
      <property name="javax.persistence.jdbc.password" value="password" />
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
  </persistence-unit>
</persistence>

I am able to connect to the database using this persistence.xml and read/write from public schema of the database. However, I want to read and write from different schema and I also want to be able to change the schema at runtime.

I tried using "?currentSchema=MYSCHEMA" in DB URL, but it didn't work.

Anuja Kale
  • 11
  • 4
  • 1
    Maybe show your persistence.xml, but your question isn't clear. Is this a general question on how persistence.xml files get loaded, or do you have a problem with the your customizer? The persistence.xml gets loaded by the JPA provider when you try to get an EntityManagerFactory - that is when your customizer would get called. If in a container, this might happen only when it is injecting an EntityManager and then the application makes a call on it. – Chris May 10 '17 at 14:15
  • I have put a print statement to understand when the customize method is getting called, but I don't think that the customize method is getting called at all in my application. I have added MySessionCustomizer class that extends SessionCustomizer to persistence.xml but am I still missing some step? – Anuja Kale May 10 '17 at 19:32
  • Would help if we saw what you meant by adding the class to your persistence.xml see https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_session_customizer.htm How are you checking that this persistence.xml is the one you are using for your application? How are you using the application - are you able to execute queries against the DB through JPA? – Chris May 10 '17 at 20:08
  • @Chris Please look at the EDIT part of the question. – Anuja Kale May 11 '17 at 03:47
  • looks like you are duplicating http://stackoverflow.com/a/29439998/496099 . If your customizer isn't getting called, then you may have a different persistence.xml file on the classpath that is getting pickedup. Turn on EclipseLink logging as it might show clues to what is going wrong for you. – Chris May 11 '17 at 14:09

0 Answers0