Reputation: 33
I am facing above issue, whenever I'm adding '@GeneratedValue' annotation to Id field. See below code snippet which I have used for saving entity using hibernate.
public class HibernateTest {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.setName("User1");
try (SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession()){
session.beginTransaction();
session.save(emp1);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Below is my Entity class
@Entity
@Table (name = "Employee_Details")
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() {
return name;
}
public void setName(String name) { this.name = name; }
}
And in hibernate.cfg.xml, connection pool size is set to 1
<property name="connection.pool_size">1</property>
I am closing the session as well as sessionFactory instance each time. Still facing below exception in logs
org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.poll(DriverManagerConnectionProviderImpl.java:322)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:189)
at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:48)
at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
If I remove 'GeneratedValue' annotation from Id field, this works fine. Can anyone tell me, why is this failing? And how to I resolve it?
Upvotes: 0
Views: 1874
Reputation: 71
I think it uses a connection to determine the 'id'. Maybe setting the number of connections to 2 instead of 1 solves the problem.
Upvotes: 0
Reputation: 85
I had this problem and solved it by replacing:
@GeneratedValue(strategy = GenerationType.AUTO)
with:
@GeneratedValue(strategy = GenerationType.IDENTITY)
Hope it works for you too!
Upvotes: 3