jpa自定义连接池 [英] jpa custom connection pool

查看:382
本文介绍了jpa自定义连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的网络应用程序中成功集成了hibernate。我很满意我的 persistence.xml 配置

I've successfully integrated hibernate in my web app. I was happy with my persistence.xml configuration

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="PU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.url" value="jdbc:sqlite:/tmp/database.db" />
            <property name="hibernate.connection.driver_class" value="org.sqlite.JDBC" />
        </properties>
    </persistence-unit>
</persistence>

然后我决定在阅读


内置连接池不适用于生产环境

The built-in connection pool is not intended for production environments

这个例子我设法使它与新的持久性.xml

With this example I managed to make it work partially with the new persistence.xml

<persistence-unit name="PU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.provider_class" value="com.zaxxer.hikari.hibernate.HikariConnectionProvider" />
            <property name="hibernate.hikari.minimumPoolSize" value="20" />
            <!-- <property name="hibernate.hikari.maximumPoolSize" value="100" /> -->
            <property name="hibernate.hikari.idleTimeout" value="30000" />
            <property name="hibernate.hikari.dataSourceClassName" value="org.sqlite.SQLiteDataSource" />
            <property name="hibernate.hikari.dataSource.url" value="jdbc:sqlite:/tmp/database.db" />
            <!-- <property name="hibernate.hikari.dataSource.user" value="" />
            <property name="hibernate.hikari.dataSource.password" value="" /> -->
        </properties>
    </persistence-unit>

但是如果我尝试设置 minimumPoolSize maximumPoolSize 用户密码

But I get error if I try to set minimumPoolSize, maximumPoolSize, user and password. If comment them out everything works great.


org.hibernate.HibernateException:java.lang.RuntimeException:java.beans.IntrospectionException:方法不是找到:setMinimumPoolSize

org.hibernate.HibernateException: java.lang.RuntimeException: java.beans.IntrospectionException: Method not found: setMinimumPoolSize

如何配置jpa使用hikaricp pool的hibernate?我希望不要在我的代码中分散hibernate特定的东西,因为我想保留ORM层抽象。
我发现了很多令人困惑的材料,并且提出了更多的问题。 persistence.xml,hibernate.properties和hibernate.cfg.xml如何相互关联?什么是JNDI以及如何使用它?什么是这个 bean配置?

How can I configure jpa to use hibernate with hikaricp pool? I prefer not to scatter hibernate-specific stuff in my code as I want to keep ORM layer abstract. I found a lot of confusing materials and got more questions than aswers. How are persistence.xml, hibernate.properties and hibernate.cfg.xml related to each other? What is JNDI and how to use it? And what is this bean configuration?

推荐答案

对不起,原来的问题。经过更多研究,我找到了解决方案。
这是工作 persistence.xml 。我认为用户密码不能在sqlite中设置。 minimumPoolSize - > minimumIdle

Sorry for the original question. After more research I found the solution. This is working persistence.xml. I think user and password can't be set in sqlite. minimumPoolSize -> minimumIdle

<properties>
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
    <property name="hibernate.hbm2ddl.auto" value="validate" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
    <property name="hibernate.show_sql" value="false" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.connection.provider_class" value="com.zaxxer.hikari.hibernate.HikariConnectionProvider" />
    <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.sqlite.SQLiteDataSource" />
    <property name="hibernate.hikari.dataSource.url" value="jdbc:sqlite:/tmp/database.db" />
</properties>

由于@neil和@zeus在这里提出了另一个使用JNDI的配置

As @neil and @zeus suggested here is another configuration using JNDI

<properties>
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
    <property name="hibernate.hbm2ddl.auto" value="validate" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SQLiteHikari"/>
</properties>

src-> main-> webapp-> META-INF-> context.xml

src->main->webapp->META-INF->context.xml

<Context antiJARLocking="true" path="/nbs">
    <Resource name="jdbc/SQLiteHikari" 
        auth="Container"
        factory="com.zaxxer.hikari.HikariJNDIFactory"
        type="javax.sql.DataSource"
        minimumIdle="20"
        maximumPoolSize="100"
        connectionTimeout="300000"
        dataSourceClassName="org.sqlite.SQLiteDataSource"
        dataSource.url="jdbc:sqlite:/tmp/database.db" />
</Context>

这篇关于jpa自定义连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆