我在大學項目中首次使用Hibernate,而且我有點新手。我覺得我跟着我的教授和一些教程,我讀給所有的指示,但我不斷收到Exception,它是在標題:Java /休眠 - 異常:內部連接池已達到其最大尺寸,並且當前沒有連接
Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
我試圖做的僅僅是存儲對象( AbitazioneDB)到我已經創建的MySql數據庫中。這是我的配置文件:
<?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>
<!-- Connection to the database -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/AllarmiDomesticiDB</property>
<!-- Credentials -->
<property name="hibernate.connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<!-- Entity -->
<mapping class="it.allarmiDomestici.centraleOperativaRemota.dbWrapper.AbitazioneDB" />
</session-factory>
</hibernate-configuration>
這是我AbitazioneDB類(我會omitt getter和setter):
@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;
}
}
這是我的HibernateUtil類:
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;
}
}
這是我的TestHibernate類,與main
方法:
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!");
}
}
當我運行TestHibernate我總是那個例外,我不知道爲什麼。也許我應該改變connection.pool_size
屬性,但如果我這樣做,似乎我只會得到更多的錯誤。有人能幫我嗎?
如果您通過getCurrentSession()獲得了會話,請勿使用'session.close()'。這可能會耗盡你的連接池。 – 11thdimension
感謝您的回答,但它沒有奏效。仍然得到相同的例外。 – Alessandro
這意味着你的連接池真的耗盡了所有連接,是否有太多長時間運行的數據庫調用? – 11thdimension