2016-08-15 41 views
7

我在大學項目中首次使用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屬性,但如果我這樣做,似乎我只會得到更多的錯誤。有人能幫我嗎?

+0

如果您通過getCurrentSession()獲得了會話,請勿使用'session.close()'。這可能會耗盡你的連接池。 – 11thdimension

+0

感謝您的回答,但它沒有奏效。仍然得到相同的例外。 – Alessandro

+0

這意味着你的連接池真的耗盡了所有連接,是否有太多長時間運行的數據庫調用? – 11thdimension

回答

7

試試這個:

的HibernateUtil:

public static Session getHibernateSession() { 

    final SessionFactory sf = new Configuration() 
     .configure("yourConfig.cfg.xml").buildSessionFactory(); 

    // factory = new Configuration().configure().buildSessionFactory(); 
    final Session session = sf.openSession(); 
    return session; 
    } 

,而是使用SessionFactory的使用Session:

final Session session = HibernateUtil.getHibernateSession(); 

然後用:

Transaction tx = session.beginTransaction(); 
// all your methods 
tx.commit(); 
session.close(); 

希望這有助於。

如果你不需要它,你實際上可能不會在你的hbm中擁有連接池屬性。

+0

感謝您的回答,但仍然沒有結果。 – Alessandro

+0

@Alessandro你嘗試並從你的配置XML中刪除該屬性? –

+0

@Alessandro而不是在POJO類中保存方法,你可以嘗試在主要方法中使用它。 –

8

您的連接池已耗盡連接,因爲您在池中使用單個連接。

<property name="connection.pool_size">1</property> 

更改此屬性的東西,你會舒服,像100

+1

再次感謝您的回答,但正如我在原文中所說的那樣,這似乎沒有幫助。 – Alessandro

+0

這實際上爲我工作。 –

+0

也在這裏工作,謝謝! –

5
<property name="connection.pool_size">1</property> 

嘗試更改連接池的大小,以更大的尺寸,像100左右。

<property name="connection.pool_size">100</property> 
+1

這似乎是[現有的答案]的重複(http://stackoverflow.com/questions/38960921/java-hibernate-exception-the-internal-connection-pool-has-reached-its-maximum/38961145#38961145 )。 – Pang