[英]HikariCP : Timeout after 30000 waiting for a connection
I am working on a Spring-MVC application where we are using HikariCP. 我正在使用HikariCP的Spring-MVC应用程序上工作。 We are using PostgreSQL as the database with 150 connections in PostgreSQL and 15 pool size for Hikari.
我们使用PostgreSQL作为数据库,其中PostgreSQL中有150个连接,而Hikari有15个池大小。 At times, we suddenly get error that timeout occured.
有时,我们突然收到发生超时的错误。 The error goes away in 10-20 seconds, but it slows down the entire server.
该错误会在10到20秒内消失,但会降低整个服务器的速度。 Many times it's not even busy.
很多时候它甚至都不忙。 I tried some other threads and added leak detection, but no help.
我尝试了其他一些线程并添加了泄漏检测,但没有帮助。 Any ideas what I am doing wrong or how to fix this?
任何想法我在做什么错或如何解决这个问题?
Error log : 错误日志:
java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection.
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:233)
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:110)
root-context.xml : root-context.xml:
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
<beans:property name="minimumIdle" value="2"/>
<beans:property name="maximumPoolSize" value="17" />
<beans:property name="maxLifetime" value="300000" />
<beans:property name="idleTimeout" value="25000" />
<beans:property name="leakDetectionThreshold" value="3000"/>
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:postgresql://localhost:5432/DB_NAME</beans:prop>
<beans:prop key="user">USERnamE</beans:prop>
<beans:prop key="password">PASSWORD</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan" value="com.ourapp.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">true</beans:prop>
<beans:prop key="cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
Any help would be nice. 你能帮忙的话,我会很高兴。 Thank you.
谢谢。 :-)
:-)
Update 更新
typical save and read : 典型的保存和读取:
@Repository
@Transactional
public class AppDetailsDAOImpl implements AppDetailsDAO {
private final SessionFactory sessionFactory;
@Autowired
public AppDetailsDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Long saveAppDetails(AppDetails appDetails, int personid) {
Session session = this.sessionFactory.getCurrentSession();
Person person = (Person) session.get(Person.class, personid);
if (person != null) {
person.getAppDetailsSet().add(appDetails);
appDetails.setUserApps(person);
Long saveid = (Long) session.save(appDetails);
session.flush();
return saveid;
}
return (long) 0;
}
@Override
public AppDetails getAppDetailsByAppId(Long appId) {
Session session = this.sessionFactory.getCurrentSession();
return (AppDetails) session.get(AppDetails.class, appId);
}
}
You run out of connections due to your application business logic. 由于您的应用程序业务逻辑,连接用完了。 Like you said:
就像你说的一样:
the problem is I have mostly short running transactions and very rarely long running transactions
问题是我的交易大部分都是短期交易,很少有长期交易
Instead of increasing the pool size for short running transactions declare a separate new DataSource
bean for long running transactions. 为长期运行的事务声明一个单独的新
DataSource
bean,而不是增加短期运行的事务的池大小。 This new DataSource
should be backed by new HikariCP pool. 此新的
DataSource
应由新的HikariCP池支持。 It can even have an min size of 0 since the cost of establishing a new database connection should be insignificant if you are running a long running transaction eg a monthly report. 它的最小大小甚至可以为0,因为如果您运行的是长时间运行的事务(例如月度报告),则建立新数据库连接的成本应该微不足道。
Separating OLTP and OLAP processing is the right approach eg see CQRS . 分离OLTP和OLAP处理是正确的方法,例如,参见CQRS 。 You can for example have a separate OLAP database refreshed daily to handle reporting while the main OLTP database is unaffected by time consuming reporting workloads.
例如,您可以每天刷新一个单独的OLAP数据库来处理报告,而主OLTP数据库不受费时的报告工作量的影响。
change the Postgres driver and change your setting for hikaricp 更改Postgres驱动程序并更改hikaricp的设置
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal
.HikariCPConnectionProvider
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.