I'm using Hibernate for the first time for a university project, and I'm a little newbie. I think I followed all the instructions given by my professor and some textbooks that I read, but I continue to receive the Exception, which is in the title:
Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
What I'm trying to do is just save the object ( AbitazioneDB ) in the MySql database that I already created. This is my configuration file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/AllarmiDomesticiDB</property> <property name="hibernate.connection.username">root</property> <property name="connection.password">password</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <mapping class="it.allarmiDomestici.centraleOperativaRemota.dbWrapper.AbitazioneDB" /> </session-factory> </hibernate-configuration>
This is my AbitazioneDB class (I will omit getters and setters):
@Entity @Table(name="abitazioni") public class AbitazioneDB { @Id @GeneratedValue @Column(name="id") private Integer id; @Column(name="indirizzo") private String indirizzo; @Column(name="nome_proprietario") private String nomeProprietario; @Column(name="tel_proprietario") private String telProprietario; public AbitazioneDB(){ super(); } public AbitazioneDB save(){ SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.getCurrentSession(); session.beginTransaction(); Integer id = (Integer) session.save(this); this.setId(id); session.getTransaction().commit(); session.close(); return this; } }
This is my HibernateUtil class:
public class HibernateUtil { private static SessionFactory sessionFactory; private static SessionFactory createSessionFactory() { sessionFactory = new Configuration().configure().buildSessionFactory(); return sessionFactory; } public static SessionFactory getSessionFactory() { if (sessionFactory == null) sessionFactory = createSessionFactory(); return sessionFactory; } }
And this is my TestHibernate class using the main method:
public class TestHibernate { public static void main(String[] args) { AbitazioneDB ab = new AbitazioneDB(); ab.setIndirizzo("Via Napoli, 29"); ab.setNomeProprietario("Mario Rossi"); ab.setTelProprietario("3333333333"); ab.save(); System.out.println("Ok!"); } }
When I run TestHibernate , I always get this exception, and I have no idea why. Maybe I should change the connection.pool_size property, but if I do, it seems like I'm getting more errors. Can anybody help me?
java mysql hibernate
Alessandro
source share