-->
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.  [ 4 posts ] 
Author Message
 Post subject: Trouble with Prepared Statement Caching & Generated SQL
PostPosted: Fri Feb 20, 2009 9:07 pm 
Newbie

Joined: Fri Feb 20, 2009 8:53 pm
Posts: 2
Can anyone shed light on why we see different generated SQL statements when kicking off the same Hibernate functionality? See below the same 3 queries but it appears different numbers have been tacked on the end - and as a result, even though these are prepared statements they don't save any performance because every (same) statement ends up being unique.

Is there some setting to control this behavior?


Hibernate version: 3.2
The generated SQL (show_sql=true):

SELECT ECMUSERBRO0_.EMP_CORP_ID AS EMP1_102_, ECMUSERBRO0_.ECM_SCRN_NM AS ECM2_102_, ECMUSERBRO0_.BEG_DT AS BEG3_102_, ECMUSERBRO0_.BRWS_WND_WTH_VLU AS BRWS4_102_, ECMUSERBRO0_.BRWS_WND_HT_VLU AS BRWS5_102_, ECMUSERBRO0_.BRWS_X_CORD_VLU AS BRWS6_102_, ECMUSERBRO0_.BRWS_Y_CORD_VLU AS BRWS7_102_ FROM ECM_USER2.ECM_USR_BRWS_DTL ECMUSERBRO0_ WHERE ECMUSERBRO0_.ECM_SCRN_NM = ? AND (ECMUSERBRO0_.EMP_CORP_ID IN (?, ?))

SELECT ECMUSERBRO0_.EMP_CORP_ID AS EMP1_104_, ECMUSERBRO0_.ECM_SCRN_NM AS ECM2_104_, ECMUSERBRO0_.BEG_DT AS BEG3_104_, ECMUSERBRO0_.BRWS_WND_WTH_VLU AS BRWS4_104_, ECMUSERBRO0_.BRWS_WND_HT_VLU AS BRWS5_104_, ECMUSERBRO0_.BRWS_X_CORD_VLU AS BRWS6_104_, ECMUSERBRO0_.BRWS_Y_CORD_VLU AS BRWS7_104_ FROM ECM_USER2.ECM_USR_BRWS_DTL ECMUSERBRO0_ WHERE ECMUSERBRO0_.ECM_SCRN_NM = ? AND (ECMUSERBRO0_.EMP_CORP_ID IN (?, ?))

SELECT ECMUSERBRO0_.EMP_CORP_ID AS EMP1_8_, ECMUSERBRO0_.ECM_SCRN_NM AS ECM2_8_, ECMUSERBRO0_.BEG_DT AS BEG3_8_, ECMUSERBRO0_.BRWS_WND_WTH_VLU AS BRWS4_8_, ECMUSERBRO0_.BRWS_WND_HT_VLU AS BRWS5_8_, ECMUSERBRO0_.BRWS_X_CORD_VLU AS BRWS6_8_, ECMUSERBRO0_.BRWS_Y_CORD_VLU AS BRWS7_8_ FROM ECM_USER2.ECM_USR_BRWS_DTL ECMUSERBRO0_ WHERE ECMUSERBRO0_.ECM_SCRN_NM = ? AND (ECMUSERBRO0_.EMP_CORP_ID IN (?, ?))


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 5:21 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Can you post your mapping and code?
What exactly do you mean by "same Hibernate functionality"?


Top
 Profile  
 
 Post subject: Mapping + Code
PostPosted: Mon Feb 23, 2009 1:06 pm 
Newbie

Joined: Fri Feb 20, 2009 8:53 pm
Posts: 2
Mapping:

Code:
<hibernate-mapping schema="ECM_USER2">

   <class   name="com.mycompany.ecm.query.dto.ECMUserBrowserDetailDTO"
          table="ECM_USR_BRWS_DTL"
            dynamic-update="true"
            dynamic-insert="true">
      <composite-id>
         <key-property name="userId" type="string" length="7" column="EMP_CORP_ID" />
         <key-property name="screenName" type="string" length="25" column="ECM_SCRN_NM" />         
      </composite-id>
      <property name="beginDate" type="java.util.Date" column="BEG_DT" />
      <property name="browserWidth" type="java.lang.Integer" column="BRWS_WND_WTH_VLU"  not-null="true"/>
      <property name="browserHeight" type="java.lang.Integer" column="BRWS_WND_HT_VLU"  not-null="true"/>
      <property name="browserXCoordinate" type="java.lang.Integer" column="BRWS_X_CORD_VLU"  not-null="true"/>
      <property name="browserYCoordinate" type="java.lang.Integer" column="BRWS_Y_CORD_VLU" not-null="true"/>
    </class>
</hibernate-mapping>


Code (first findUserBrowserDetail relates to the sql posted in original post). Each of the 3 sql statements are all from calls to the same findUserBrowserDetail method, but the AS 'generated column' gets tacked on, thus preventing prepared statements from being cached.

Code:
package com.mycompany.ecm.query.dao;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.mycompany.ecm.query.dto.ECMUserBrowserDetailDTO;
import com.mycompany.ecm.query.util.ServiceConstants;

/**
*
* Implementation class to perform CRUD operation with ECM Query
* Database table - ECM User Security using hiberante(O-R mapping table)
*
*/
public class ECMUserBrowserDetailDAOImpl implements ECMUserBrowserDetailDAO,ServiceConstants {

   /**
    * Represents Hibernate session factory
    */
   private SessionFactory sessionFactory;
   
   static Logger logger = Logger.getLogger(ECMUserBrowserDetailDAOImpl.class);

   /**
    * default constructor
    */
   public ECMUserBrowserDetailDAOImpl(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
   }

   /**
    * Obtains the current session.
    *
    * @return the transactional session
    */
   protected Session getCurrentSession() {
      return sessionFactory.openSession();
   }
   
   /**
    * Insert an record in ECM Query database table ECM User Browser Detail.
    *
    * @param browserDetail
    * @return ECMUserBrowserDetailDTO
    */
   public ECMUserBrowserDetailDTO addUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
      getCurrentSession().save(browserDetail);
      return browserDetail;
   }

   /**
    * Update the given record in ECM Query database table ECM User Browser Detail.
    *
    * @param browserDetail
    */
   public void updateUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
      getCurrentSession().update(browserDetail);
   }
   
   /**
    * Update the given record in ECM Query database table ECM User Browser Detail
    * If the record not exist in the Db inserts a new record.
    *
    * @param browserDetail
    */
   public void saveOrUpdateUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
      
       Session session = null;

         try{
            session = getCurrentSession();
            session.saveOrUpdate(browserDetail);            
         }         
         catch(Exception e){
             logger.error("Exception occured while saving user preferences "+e);
         }
         finally{
            if(session != null){
                session.flush();
               session.close();
            }
         }
      
   }

   /**
    * Delete the given record in ECM Query database table User Browser Detail.
    *
    * @param userSecurity
    */
   public void deleteUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
      getCurrentSession().delete(browserDetail);
   }

   /**
    * Get and return the list of browser settings  for the given screen(s).
    *
    * @param screenName
    * @param userId
    * @return List
    */
   public List findUserBrowserDetails(String userId,String screenName)
   {
       List browserDetailsList = null;
       Session session = null;
      try{
         String[] preferences = {userId,USER_ID_DEFAULT};
         session = getCurrentSession();
           Query query =
              session.createQuery("from ECMUserBrowserDetailDTO " +
                    "browserDetail where " +
                    "browserDetail.screenName = ? and browserDetail.userId in (:preferences)");
          query.setString(0, screenName);
         query.setParameterList("preferences", preferences);
         browserDetailsList = query.list();
      }
      catch(Exception e){
          logger.error("Exception occured while getting user preferences "+e);
      }
      finally{
         if(session != null){
            session.flush();
            session.close();
         }
      }
      return browserDetailsList;
   }

   /**
    *
    * Get and return the browser detail  for the given input.
    *
    * @param userSecurity
    * @return List
    */
   public List findUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
      
      Criteria crit = null;
      try{
         crit = getCurrentSession().createCriteria(ECMUserBrowserDetailDTO.class);
      }
      catch(Exception e){
          logger.error("Exception occured while finding user preferences "+e);
      }
       if(browserDetail!=null){
           if(browserDetail.getScreenName()!=null){
              crit.add(Restrictions.eq("screenName",browserDetail.getScreenName()));              
           }
           if(browserDetail.getBeginDate()!=null){
              crit.add(Restrictions.eq("beginDate",browserDetail.getBeginDate()));              
           }
           if(browserDetail.getUserId()!=null){
              crit.add(Restrictions.eq("userId",browserDetail.getUserId()));              
           }
       }

      return crit.list();
   }

   /**
    * Get and return the list of security level exist in the
    * ECM Query database table ECM User Browser Detail.
    *
    * @return List
    */
   public List findAllUserBrowserDetail() {
      return getCurrentSession()
            .createQuery(
                  "from ECMUserBrowserDetailDTO browserDetail")
            .list();
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 6:50 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Maybe use of criteria and dynamic construction of the statement prevents caching the prepared statement... you could try use fixed queries.

The other thought is how sessions/transactions are handled in your app, maybe your use of SessionFactory/Session/Transaction is a problem for the prepared statements... like in findUserBrowserDetails where you do your session inside the function opposed to findUserBrowserDetail...

and why are you calling sessionFactory.openSession inside your getCurrentSession method?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.