-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: MySQL General Query Log shows new query every (Session) get
PostPosted: Wed Apr 04, 2012 3:57 pm 
Newbie

Joined: Wed Apr 04, 2012 3:35 pm
Posts: 4
Is this normal? It's the same in the output on the command-line from Jboss AS7, it shows one query for every call to get. First I figured it just showed the queries it would use and then just get the data from the session but looking in the general query log in the MySQL-folder it lists the same queries being done.

I've got one of those HibernateUtil-classes like this:

Code:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}


Then I use it for example like this:

Code:
public class App
{
    public static void test()
    {

        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
       
        Test test = (Test) session.get(Test.class, 1);

        session.getTransaction().commit();
    }
}


Where Test is a MySQL-table mapped as a class and 1 is just the item's ID in the table. I've tried printing in the buildSessionFactory-function and it's only being called once still there's a new query in the general query log every-time get is called.

Btw, it's the log you turn on with this in MySQL:

Code:
SET GLOBAL general_log = 'ON';


Top
 Profile  
 
 Post subject: Re: MySQL General Query Log shows new query every (Session) get
PostPosted: Thu Apr 05, 2012 2:43 pm 
Newbie

Joined: Wed Apr 04, 2012 3:35 pm
Posts: 4
No-one? It seem pretty basic but I couldn't find anything about it. All tutorials I went through just seem to either not care or just assume Hibernate does whatever is best for you. It almost seem the most likely it registers all queries even if it gets data from the session, but I really don't want to take that chance.


Top
 Profile  
 
 Post subject: Re: MySQL General Query Log shows new query every (Session) get
PostPosted: Tue Apr 10, 2012 1:17 pm 
Newbie

Joined: Wed Apr 04, 2012 3:35 pm
Posts: 4
I've read up on caching in Hibernate and have understood that it's only second-level caching that works between transactions. I've implemented EHCache and managed to make the query-cache work with setting setCachable(true) for my queries. However ironicly I'm faced with the same question again as the second-level cache isn't working with calls to get :)

Here's my hibernate.cfg.xml:

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</property>
        <property name="connection.url">jdbc:mysql://localhost/test</property>
        <property name="connection.username">jboss</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
       
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

      <property name="cache.use_query_cache">true</property>
      <property name="cache.use_second_level_cache">true</property>
      <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

       <class-cache class="test.Test" usage="read-only"/>
      
    </session-factory>
   
</hibernate-configuration>


ehcache.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache
   eternal="false"
   timeToIdleSeconds="300"
   timeToLiveSeconds="600"
/>
</ehcache>


Still doing something like:

Code:
package test;

public class TestManager
{

   static public Test getTestFromId(String testId)
   {
      Test test = null;
      if(testId != null)
      {
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();

         test = (Test) session.get(Test.class, testId);
   
         session.getTransaction().commit();
         session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();

         test = (Test) session.get(Test.class, testId);
   
         session.getTransaction().commit();
         session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();

         test = (Test) session.get(Test.class, testId);
   
         session.getTransaction().commit();
      }
      return test;
   }

}


Still generates 3 SELECT-queries in the General Query Log... Wouldn't that read-only for the table test mapped to Test cause it to cache everything returned from get?

I'm using Hibernate 4.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.