簡體   English   中英

在運行時配置HikariCP + Hibernate + GuicePersist(JPA)

[英]Configure HikariCP + Hibernate + GuicePersist(JPA) at Runtime

我有一個使用GuicePersist,Hibernate和HikariCP的java8桌面應用程序與Postgres DB進行通信。 我已成功使用此META-INF / persistence.xml讓我的應用程序向數據庫發送/接收數據:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <!-- A JPA Persistence Unit -->
    <persistence-unit name="myJPAunit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.123fakestreet.bogus.tomb.impl.postgres.model.movies</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <!-- SQL stuff -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

            <!-- hikari CP -->
            <property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />
            <property name="hibernate.hikari.minimumIdle" value="20" />
            <property name="hibernate.hikari.maximumPoolSize" value="100" />
            <property name="hibernate.hikari.idleTimeout" value="30000" />
            <property name="hibernate.hikari.dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
            <property name="hibernate.hikari.dataSource.url" value="jdbc:postgresql://192.168.100.75:5432/mpDb" />
            <property name="hibernate.hikari.username" value="cowboy" />
            <property name="hibernate.hikari.password" value="bebop" />

            <!-- Disable the second-level cache  -->
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

            <!-- Default is false for backwards compatibility.  Should be used on all new projects -->
            <property name="hibernate.id.new_generator_mappings" value="true"/>

        </properties>
    </persistence-unit>
</persistence>

棘手的部分是在運行時配置我的Guice JpaPersistModule。 據我所知,我應該能夠通過在Map對象中設置屬性來覆蓋META-INF / persistence.xml文件中的屬性,如下所示:

Map<String, String> properties = new HashMap<>();
    properties.put("myJPAunit.hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
    properties.put("myJPAunit.hibernate.hikari.dataSource.user", "cowboy");
    properties.put("myJPAunit.hibernate.hikari.dataSource.password", "bebop");

然后jpaModule.properties(屬性),然后將這一切傳遞給GuicePersist。

我在地圖上嘗試了一些不同的屬性名稱組合,但到目前為止我沒有運氣。 我還查看了有關此主題的pgsimpledatasource文檔和hikariCP文檔,但我的數據源屬性仍未設置。

有人可以幫忙嗎? 謝謝一堆。

嘗試從JPA屬性中刪除持久性單元名稱,而不是:

Map<String, String> properties = new HashMap<>();
properties.put("myJPAunit.hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
properties.put( + "myJPAunit.hibernate.hikari.dataSource.user", "cowboy");
properties.put( + "myJPAunit.hibernate.hikari.dataSource.password", "bebop");

你應該這樣:

Map<String, String> properties = new HashMap<>();
properties.put("hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
properties.put("hibernate.hikari.dataSource.user", "cowboy");
properties.put("hibernate.hikari.dataSource.password", "bebop");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM