多次在Hibernate控制台上显示相同的mysql查询,这可能导致DataSource.getConnection()响应速度很慢

多次在Hibernate控制台上显示相同的mysql查询,这可能导致DataSource.getConnection()响应速度很慢

问题描述:

我最近遇到的激活问题setShowSql(true)在开发模式下测试时显示多次MySQL查询(大部分是3次)我只有一个用户session.And这是我的网站在高流量期间放缓的主要原因,并通过监测我的网站在新的文物监测系统,它显示我总是DataSource.getConnection()持有这么多时间。请结帐这个下面的截图..多次在Hibernate控制台上显示相同的mysql查询,这可能导致DataSource.getConnection()响应速度很慢

enter image description here

我仍然不知道什么是主因在这背后slowness.and很少有开发商建议我使用@PostConstruct是保证这个方法会被调用仅合同一次在bean生命周期中,但它根本不起作用。

例如这里的mysql查询重复几次。

Hibernate: select count(favorite0_.id) as col_0_0_ from favorite favorite0_ left outer join User user1_ on favorite0_.user_id=user1_.id where user1_.id is null 

Hibernate: select count(favorite0_.id) as col_0_0_ from favorite favorite0_ left outer join User user1_ on favorite0_.user_id=user1_.id where user1_.id is null 

Hibernate: select count(favorite0_.id) as col_0_0_ from favorite favorite0_ left outer join User user1_ on favorite0_.user_id=user1_.id where user1_.id is null 

这里是我的连接池的配置(Tomcat的7's的context.xml)

<Resource auth="Container" 
      driverClassName="com.mysql.jdbc.Driver" 
      maxActive="100" 
      maxIdle="10" 
      name="jdbc/fappi" 
      password="xxxxxxxx" 
      type="javax.sql.DataSource" 
      url="jdbc:mysql://localhost:3306/coexi useUnicode=true&amp;characterEncoding=utf-8" 
      username="root" 
      timeBetweenEvictionRunsMillis="600000" 
      removeAbandoned="true" 
      testWhileIdle="true" 
      removeAbandonedTimeout="100" 
      validationQuery="select 1" 
      testOnBorrow="true" /> 

我请求你们所有人请给我建议。

提前很多感谢.. 添

+0

@微弱,谢谢你的正确。 – princeexpedition

看起来像一个N+1 query issue to me。您应该使用an automatic SQL statement validator以确保数据访问层方法生成的SQL语句的数量是预期的数量。

+0

非常感谢您给我的建议。无论如何,我只想知道我是否在我的项目中维护了许多不同的日志,这是否也会导致我的网站缓慢? – princeexpedition

+0

过多的日志记录确实会影响应用程序的性能。这就是为什么[异步记录器是更好的选择](https://logging.apache.org/log4j/2.x/performance.html)。 –

+0

当我们有5个不同的高速CPU处理时,我的连接池配置是否可以处理大流量?我创建了MYSQL服务器(这意味着使用MySQL服务器的部分)我不知道这是不是好主意?请给我一些建议。 – princeexpedition