使用hib5和c3po连接池在java应用程序中设置vsession.program

nukf8bse  于 2023-01-07  发布在  Java
关注(0)|答案(2)|浏览(98)

我正在尝试从我的java应用程序设置会话程序名。我正在使用一个实体管理器从用户输入传递连接凭据。
我需要能够在连接时设置oracle会话中的程序名。现在它只显示为“JDBC瘦客户端”。我尝试过在java中设置实体管理器属性,并尝试了persistence.xml中的许多变体。

Java代码片段

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

持久性.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>

我在使用何种技术方面受到限制,因此无法按照建议使用EclipseLink here
是否有其他方法可以在java应用程序或persistence.xml文件中设置此值?

wixjitnu

wixjitnu1#

hibernate.connection.x属性是传递给JDBC的连接属性。您使用的属性也是JDBC属性,因此您应该尝试如下设置。我使用的是这种方法。
编号

2jcobegt

2jcobegt2#

您也可以这样设置它配置application.yml如下:

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

只有在自动配置数据源而不是手动创建数据源bean的情况下,这种方法才有效。

@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();

    }
}

相关问题