'EntityManagerFactory is initialing
I`m DONT use EJB/Spring etc.
Only Jpa + Servlet.
I`m trying to create
public abstract class AbstractEntityManagerFactory {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateTask");
public static EntityManager getEntityManager(){
return emf.createEntityManager();
}
When I run it through
public static void main(String[] args)
It works well.
But if using TomCat 9.0.58 - I get an error:
05-Apr-2022 22:58:58.985 INFO [http-nio-8080-exec-4] org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService HHH000369: Error stopping service [class org.hibernate.hikaricp.internal.HikariCPConnectionProvider]
java.lang.NullPointerException: Cannot invoke "com.zaxxer.hikari.HikariDataSource.close()" because "this.hds" is null
at org.hibernate.hikaricp.internal.HikariCPConnectionProvider.stop(HikariCPConnectionProvider.java:122)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:390)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.cleanup(EntityManagerFactoryBuilderImpl.java:1449)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1508)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.aimprosoft.hopak.factory.AbstractEntityManagerFactory.<clinit>(AbstractEntityManagerFactory.java:9)
My persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="HibernateTask">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.hikari.jdbcUrl" value="jdbc:mysql://localhost:3306/Departments"/>
<property name="hibernate.hikari.username" value="root"/>
<property name="hibernate.hikari.password" value="ISBN97859910"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
<property name="hibernate.hikari.connectionTimeout" value="20000"/>
<property name="hibernate.hikari.idleTimeout" value="150000"/>
<property name="hibernate.hikari.maximumPoolSize" value="10"/>
<property name="hibernate.hikari.poolName" value="HikariPool"/>
<property name="hibernate.hikari.autoCommit" value="true"/>
<property name="hibernate.hikari.maxLifetime" value="90000"/>
</properties>
</persistence-unit>
</persistence>
How can I fix this?
Solution 1:[1]
Try using...
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Departments"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="password"/>
...instead of hibernate.hikari.*
properties. That helped in my setup.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael |