Reputation: 19
I keep getting an exception:
org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
I read this question, but it didn't help me
So, here is my code:
HibernateManager
package beans.database;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import javax.ejb.Singleton;
/**
* The type Hibernate manager.
*
*/
@Singleton
public class HibernateManager {
private StandardServiceRegistry registry;
private SessionFactory sessionFactory;
/**
* Gets session.
*
*
*
* @return the session
*/
public Session getSession() {
if (sessionFactory == null) {
try {
// Create SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
final Session session = sessionFactory.openSession();
return session;
}
/**
* Shutdown.
*/
public void shutdown() {
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
How it use
@Singleton
@LocalBean
public class FieldChecker {
@EJB
HibernateManager hibernateManager;
public Boolean isUserFieldExist(String field, String value) {
final Session session = hibernateManager.getSession();
Criteria criteria = session.createCriteria(UserEntity.class);
criteria.add(Restrictions.eq(field, value));
criteria.setProjection(Projections.rowCount());
long count = (long) criteria.uniqueResult();
session.close();
return count != 0;
}
}
UserEntity
package entities;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;
@Entity
@Table(name = "user", schema = "pump_coin")
public class UserEntity {
private long id;
private String name;
private String email;
private Long phone;
private Double height;
private Double weight;
private Double foot;
private String unit;
private Double target;
private byte verification;
private Timestamp date;
private String password;
private Integer age;
private String sex;
@Column(name = "id")
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "phone")
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
@Column(name = "height")
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
@Column(name = "weight")
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Column(name = "foot")
public Double getFoot() {
return foot;
}
public void setFoot(Double foot) {
this.foot = foot;
}
@Column(name = "unit")
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Column(name = "target")
public Double getTarget() {
return target;
}
public void setTarget(Double target) {
this.target = target;
}
@Column(name = "verification")
public byte getVerification() {
return verification;
}
public void setVerification(byte verification) {
this.verification = verification;
}
@Column(name = "date")
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "sex")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
return id == that.id && verification == that.verification && Objects.equals(name, that.name) && Objects.equals(email, that.email) && Objects.equals(phone, that.phone) && Objects.equals(height, that.height) && Objects.equals(weight, that.weight) && Objects.equals(foot, that.foot) && Objects.equals(unit, that.unit) && Objects.equals(target, that.target) && Objects.equals(date, that.date) && Objects.equals(password, that.password) && Objects.equals(age, that.age) && Objects.equals(sex, that.sex);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email, phone, height, weight, foot, unit, target, verification, date, password, age, sex);
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://my/url</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="hibernate.c3p0.timeout">1800</property>
<mapping class="entities.UserEntity"/>
<mapping class="entities.CoinBalanceEntity"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
I have a suspicion that I somehow incorrectly inject HibernateManager and as a result I get a lot of connection factories.
Upvotes: 0
Views: 1154
Reputation: 5095
When using the Hibernate session you must make sure to ALWAYS close it, no matter what. Meaning your method should use the try-finally pattern.
public Boolean isUserFieldExist(String field, String value) {
final Session session = hibernateManager.getSession();
try {
Criteria criteria = session.createCriteria(UserEntity.class);
criteria.add(Restrictions.eq(field, value));
criteria.setProjection(Projections.rowCount());
long count = (long) criteria.uniqueResult();
return count != 0;
} finally {
session.close();
}
}
You have to do this everywhere you use create a new session. Though in general if you're using a comprehensive system with EJB injection, you should just inject an EntityManager
instead of managing it manually like this.
Upvotes: 1