2016-11-23 87 views
0

我正在使用postgresql,但在从数据库中的表中获取记录时,我已经收到此异常FATAL: sorry, too many clientsPostgres + JDBC模板太多客户端已经例外

休耕是连接类:

<context:component-scan base-package="com.trinity" /> 
     <context:property-placeholder location="classpath:/jdbc.properties" order="1" ignore-unresolvable="true"/> 
     <beans:bean id="dataSourcePOSTGRESQL" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <beans:property name="driverClassName" value="${pg_jdbc.driverClassName}" /> 
     <beans:property name="url" value="${pg_jdbc.url}" /> 
     <beans:property name="username" value="${pg_jdbc.username}" /> 
     <beans:property name="password" value="${pg_jdbc.password}" /> 
    </beans:bean> 

jdbc.propertis

pg_jdbc.driverClassName=org.postgresql.Driver 
pg_jdbc1.url1=jdbc:postgresql://localhost:5433/iotdb 
pg_jdbc.username=postgres 
[email protected] 

DAO类:

public List<Map<String, Object>> getAllDevicesNames(Integer companyId) { 

    String sql = "select device_configuration_id,device_name,device_id from iot.device_configuration where company_id = ? "; 
    List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql,new Object[]{companyId}); 
    return queryForList; 

} 
+1

您使用的是连接池吗?你知道你有多少个开放的连接吗? – chrylis

+1

最有可能的是,你没有关闭你的连接,但没有看到你的连接代码(s),我们不能肯定地说。 –

+0

(注意,如果你使用Spring Boot,你可以跳过配置,它甚至会自动为你配置连接池和JDBC模板。) – chrylis

回答

-1

使用此查询来查找连接数

SELECT sum(numbackends) FROM pg_stat_database; 

标准100最大数据库连接服务器可能与数据库服务器可能不够用于生产使用。为了增加在Linux安装此Postgres的默认情况下,您可以按照下列步骤操作:

/var/lib/pgsql/data/postgresql.conf 

,你会发现这两个设置 在100,分别为24 MB。您可以将其更改为以下:

max_connections = 300 
shared_buffers = 80MB 
+1

如果他在开发环境中有很多连接,那么他几乎肯定会将它们泄漏到某个地方。 – chrylis

1

以前在我postgres.cfg文件

max_connections = 100 
shared_buffers = 128kb 

现在,我改为

max_connections = 300 
shared_buffers = 80MB 

这是工作的罚款。

+0

如果它按照预期的方式工作,请接受答案:) – Srinivasu