1

I'm trying to set the session program name from my java app. I'm using an entity manager passing the connection credentials from user input.

I need to be able to set the program name in the oracle session when connected. Right now it only appears as "JDBC Thin Client". I've attempted setting the entity manager properties in java and tried numerous variations in my persistence.xml.

Java snippet

            props.put( hibernate.connection.url, "jdbc:oracle:thin:@localhost:1521:XE");
            props.put( "hibernate.connection.username", username );
            props.put( "hibernate.connection.password", password );
            props.put( "v$session.program", "MYPROGRAM" );

            emf = Persistence.createEntityManagerFactory( "MYDB", props );

            em = emf.createEntityManager();

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?><persistence version="1.0" xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">
<persistence-unit name="MYDB">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <!-- Hibernate properties -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.cache.use_second_level_cache" value="false"/>
        <!-- Connection Pool -->
        <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
        <property name="hibernate.c3p0.max_size" value="20" />
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.acquire_increment" value="5" />
        <property name="hibernate.c3p0.idle_test_period" value="300" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.timeout" value="100" />
        <property name="connectionProperties" value="v$session.program:MYPROGRAM" />
    </properties>
</persistence-unit>

I'm limited in what techonologies to use, so I cannot use EclipseLink as suggested here

Is there any other way to set this either in the java app or in the persistence.xml file?

Community
  • 1
  • 1
speedos82
  • 97
  • 1
  • 2
  • 7

2 Answers2

1

The properties hibernate.connection.X are the connection properties passed to JDBC. The property that you are using is also a JDBC one, so you should try to set as below. It worked for me.

code

<property name="hibernate.connection.v$session.program" value="MYPROGRAM" />

0

You can also set it like this Configure the application.yml like this:

spring:
     datasource:
          url: jdbc:oracle:thin:db
          username: foo
          password: bar
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
               jdbcUrl: ${spring.datasource.url}
               minimumIdle: 3
               maximumPoolSize: 15
               connectionTimeout: 10000
               validationTimeout: 3000
               poolName: ${spring.application.name:unknown}-gen-pool
               connection-init-sql: |-
                    BEGIN
                         DBMS_APPLICATION_INFO.SET_MODULE('module-info', 'action-info');
                    END;
          driver-class-name: oracle.jdbc.OracleDriver

This will only work if your datasource is being autoconfigured and not manually creating the datasource bean. my datasource config looked something like this

@Configuration
@EntityScan(basePackages = {"com.this.way.*", "com.that.way.*"})
@EnableJpaRepositories(
        entityManagerFactoryRef = "customNameEntityManagerFactory",
        basePackages = {"com.this.way.*", "com.that.way.*"}
)
@EnableTransactionManagement
public class DataSourceConfig {

    @Primary
    @Bean(name = {"entityManagerFactory", "customNameEntityManagerFactory"})
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder,
                                                                       final DataSource dataSource) {
        return builder.dataSource(dataSource)
                .packages("com.this.way.*", "com.that.way.*")
                .persistenceUnit("PERSISTENCE_UNIT")
                .build();

    }
}
Brian Akumah
  • 144
  • 3
  • 12