简体   繁体   中英

Unexpected queries are being executed after connecting to MySQL while using Hibernate

public User getUserOnlyByUserName(String username) {

    LOGGER.info(" into getUserByIdJOIN ServiceImpl in UserDao------>>>>>>");
    Session session = sessionFactory.openSession();

    Criteria criteria = session.createCriteria(User.class);
    criteria.add(Restrictions.eq(USERNAME, username));
    Object obj =  criteria.uniqueResult();

    if(obj==null){
        return null;
    }
        session.close();
LOGGER.info(" out of getUserByIdJOIN ServiceImpl in UserDao------>>>>>>");
        return (User) obj;

}

197 Connect root@localhost on (dbname)

The mysql log for the same is shown below

197 Connect root@localhost on itaf4 197 Query /* mysql-connector-java-5.1.42 ( Revision: 1f61b0b0270d9844b006572ba4e77f19c0f230d4 ) */***SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS tx_isolation, @@wait_timeout AS wait_timeout 197 Query SET NAMES latin1 197 Query SET character_set_results = NULL 197 Que ry SET autocommit=1 197 Query SET sql_mode='STRICT_TRANS_TABLES'

197 Query select this_.user_id as user_id1_3_0_, this_.accountNonExpired as accountN2_3_0_, this_.accountNonLocked as accountN3_3_0_, this_.attempts as attempts4_3_0_, this_.credentialsNonExpired as credenti5_3_0_, this_.enabled as enabled6_3_0_, this_.firstname as firstnam7_3_0_, this_.lastAttempt as lastAtte8_3_0_, this_.lastname as lastname9_3_0_, this_.password as passwor10_3_0_, this_.roleId as roleId12_3_0_, this_.username as usernam11_3_0_ from users this_ where this_.username='demo_admin' 197 Query SHOW WARNINGS 197 Quit**

Those SQL statements are not executed by Hibernate. The MySQL Driver execute them for you.

If you want to know what statements Hibernate generates, then you should configure an SQL query logger, like datasource-proxy . This way, you are going to know for sure what JDBC statements were generated explicitly by the application.

In your case, all those statements are executed when the database connection is initialized. You can debug the MySQL Connector/J driver and see for yourself.

If you see those logs many times, it means you don't reuse connections, which is bad for performance and system scalability. After configuring a connection pooling solution, make sure you use FlexyPool as well so that you know what happens underneath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM