Hibernate向数据库发送多余的查询
作者:互联网
我有一个奇怪的问题,即Hibernate运行的查询数量超出了我的要求,并且看不到需求.
这是我的控制器:
@Autowired UserService users;
@RequestMapping("/test")
@ResponseBody
public String test() {
User user = users.findUser(1L);
return "Found user: "+user.getEmail();
}
这是UserService:
@Component
public class UserService {
@javax.persistence.PersistenceUnit private EntityManagerFactory emf;
private JpaTemplate getJpaTemplate() {
return new JpaTemplate(emf);
}
public User findUser(long id) {
long start = System.currentTimeMillis();
JpaTemplate jpaTemplate = getJpaTemplate();
User user = jpaTemplate.find(User.class, id);
System.out.println(System.currentTimeMillis() - start);
return user;
}
}
对findUser()的调用大约需要140毫秒…令人莫名其妙.数据库对于其他查询(包括某些处理程序中的查询)的运行情况很好(我怀疑它不是第一次运行查询).
JProfiler建议每次调用它时,都会向数据库发送四个查询(不一定按此顺序):
1) [5ms] select user... (the actual query) 2) [7ms] SHOW COLLATION 3) [14ms] /* mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} ) */SELECT @@session.auto_increment_increment 4) [70ms] /* mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} ) */SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' OR Variable_name = 'init_connect'
很明显,实际查询根本不需要任何时间,大部分时间都花在了第四个查询中.我该怎么办?它不会在休眠日志输出中显示,只有第一个实际查询会显示.顺便说一句,所有时间都花在调用getJpaTemplate()之后-实际上是在jpa.find()方法中.
有任何想法吗?
更新:我已经确定它是休眠的,它多次与数据库进行某种初始连接,因为其他人发布了相同的查询集(http://ondra.zizka.cz/stranky/programovani/java/hibernate_netbeans_howto_tutorial.texy).休眠为什么会反复建立其初始连接,而不是使用连接池-如何检查?
解决方法:
我已经解决了我有一个非池数据源:
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
来自Javadoc,网址为:http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/datasource/DriverManagerDataSource.html
Simple implementation of the standard JDBC DataSource interface, configuring the plain old JDBC DriverManager via bean properties, and returning a new Connection from every getConnection call.
NOTE: This class is not an actual connection pool; it does not actually pool Connections. It just serves as simple replacement for a full-blown connection pool, implementing the same standard interface, but creating new Connections on every call.
因此,我现在将其替换为:
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://server.domain/database"/>
<property name="user" value="theUsername"/>
<property name="password" value="thePassword"/>
</bean>
我也不得不抛出c3p0-0.9.1.2.jar,因为它使用了该连接池.
标签:hibernate,jprofiler,spring,java,mysql 来源: https://codeday.me/bug/20191024/1918232.html