问题
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?
回答1:
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>
回答2:
Please refer to the following thread explaining how to setup spring bean for HikariCP.
How to set up datasource with Spring for HikariCP?
来源:https://stackoverflow.com/questions/28856513/jpa-custom-connection-pool