2012-02-29 103 views
4

我使用的是Spring應用程序和我得到以下異常爲:PostgreSQL的連接限制非超級用戶

Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection. 

當我手動嘗試使用DBVisualizer中連接到數據庫,我收到以下錯誤

An error occurred while establishing the connection: 

Long Message: 
FATAL: connection limit exceeded for non-superusers 

Details: 
   Type: org.postgresql.util.PSQLException 
   Error Code: 0 
   SQL State: 53300 

這裏是我的春天context.xml文件

<jee:jndi-lookup id="dataSource1" jndi-name="jdbc/PmdDS"/> 


    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource1" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="current_session_context_class">thread</prop> 
       <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> 

      </props> 
     </property> 
    </bean> 

我的問題是我收到此錯誤,因爲我還沒有添加以下行春季-context.xml中

<prop key="hibernate.connection.release_mode">auto</prop> 

將加入這一行能解決我的問題。我擔心我的應用程序正在創建連接,但沒有釋放數據庫連接,因爲我沒有在spring-context.xml中添加上述行.. 注意我沒有使用HibernateTemplate。我使用sessionFactory.getCurrentSession().createQuery("").list()解僱我的查詢 我的context.xml詳情

<Context> 
    Specify a JDBC datasource 
    <Resource name="jdbc/PmdDS" 
       auth="Container" 
       type="javax.sql.DataSource" 
       username="sdfsfsf" 
       password="sfsdfsdf" maxActive="-1" 
       driverClassName="org.postgresql.Driver" 
       url="jdbc:postgresql://111.11.11.11:5432/test"/> 


</Context> 

請提出解決方案

回答

2

的問題是數據源中的配置

爲默認值最大連接數量高於postgres中設置的最大數量,當hibernate要求另一個連接時,數據源嘗試創建一個連接。

你可以顯示你的數據源的配置嗎?

+0

我在編輯中添加的是context.xml項一些儲備連接 – Rajesh 2012-02-29 11:34:49

+0

嘗試設置maxActive = 「-1」的值小於100(postgres默認值)。通常10到20就足夠了。 – 2012-02-29 12:06:52

2

(記錄我把同樣的問題的步驟)

  1. 檢查你的數據源 - 尋找maxActive號。
  2. 檢查您的Postgresql設置:SELECT * FROM pg_settings - 尋找max_connections

如果第一個數大於第二個數,則說明您有問題。降低第一個或增加第二個(在您的實例的postgresql.conf中)。

另外,不要忘記保持對其他服務器訪問同一個數據庫,以及一些數據庫管理工具:)