3

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>

Then I decided to use HikariCp connection pooler after reading this

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

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>

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: Method not found: setMinimumPoolSize

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?

gkiko
  • 2,283
  • 3
  • 30
  • 50
  • 1
    hibernate.* files are nothing to do with JPA, and should not be used if wanting to stay portable. Any connection pool can either use internal JPA implementation code (like you try), or you can just set up a javax.sql.DataSource which is configured to provide pooling (and then that can be used with ANY JPA implementation) – Neil Stockton Mar 04 '15 at 14:56
  • If you are dealing with JPA then worry only about the persistence.xml file for the configuration. if you are putting something in the hibernate.* files then you are configuring Hibernate specific settings which is not what you want. You'll use JNDI for the data source part, if youi have an enterprise server(Jboss, weblogic, websphere) you'll get to create a datasource in the server admin console(including the pool configuration) and assign a JNDI name to it. Instead of configuring the connections, connection pools etc in the persistence.xml you just give the jndi name in the same file. – Zeus Mar 04 '15 at 15:20
  • Is _setting up javax.sql.DataSource_ same as _configuring JNDI_? Should I configure a specific Catalina file on every server I deploy my web-app in this case? – gkiko Mar 05 '15 at 05:51

1 Answers1

4

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>

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

<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>
gkiko
  • 2,283
  • 3
  • 30
  • 50
  • Also, if you are using Hibernate 4.3.6+ they provide their own ConnectionProvider for HikariCP now. See update here https://github.com/brettwooldridge/HikariCP/wiki/Hibernate4 – brettw Mar 05 '15 at 14:27
  • I don't like that they are using hikari v1.3.5. Maybe I shouldn't be concerned about that. – gkiko Mar 05 '15 at 17:18
  • Oh, I didn't realize they were locked to that version. If you can, open a bug request for them to update. – brettw Mar 06 '15 at 03:59