Need help in setting application name with JPA (Ec

2019-08-04 08:44发布

问题:

hello everybody i am using JPA with EclipseLink and oracle as DB and i need to set the property v$session of jdbc4 it allows to set an identification name to the application for auditing purposes but i had no lucky setting it up....i have been trying through entitiyManager following the example in this page: http://wiki.eclipse.org/Configuring_a_EclipseLink_JPA_Application_(ELUG) it does not show any error but does not set the application name at all... when i see the audit in oracle it is not being audited with the name i set by code "Customers" but with OS_program_name=JDBC Thin Client it means that the property in the code is not being set properly and i have no idea where the issue is, the code i am using is the following :

    emProperties.put("v$session.program","Customers");
    factory=Persistence.createEntityManagerFactory("clients",emProperties);
    em=factory.createEntityManager(emProperties);        
    em.merge(clients);

does anybody know how to do it or any idea....

thanks.-

回答1:

v$session.program is a JDBC connection property, but Persistence.createEntityManagerFactory gets persistence unit properties. There is no direct way to pass arbitrary JDBC property into entity manager.

However, in EclipseLink you can use SessionCustomizer:

public class ProgramCustomizer extends SessionCustomizer {
    @Override
    public void customize(Session s) throws Exception {
        s.getDatasourceLogin().setProperty("v$session.program", "Customers");
        super.customize(s);
    }
}

-

emProperties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, "ProgramCustomizer");