Changeset 5037


Ignore:
Timestamp:
Jul 31, 2009, 9:20:53 AM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #1350: Update to Hibernate 3.3.2

Added a very ugly hack that effectively disables the query plan cache in Hibernate. This forces all queries to be rebuilt from scratch at all times and ensures that the proper number of parameter placeholders are added to the final SQL. The disabling of the cache may result in some performance degradation but this has not yet been investigated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r4889 r5037  
    6161import java.sql.DatabaseMetaData;
    6262import java.sql.SQLException;
     63import java.lang.reflect.Field;
    6364import java.net.URL;
    6465import java.net.URI;
     
    9495import org.hibernate.tool.hbm2ddl.SchemaExport;
    9596import org.hibernate.tool.hbm2ddl.SchemaUpdate;
     97import org.hibernate.util.SoftLimitMRUCache;
    9698import org.hibernate.metadata.ClassMetadata;
    9799import org.hibernate.dialect.Dialect;
     
    99101import org.hibernate.dialect.function.SQLFunctionTemplate;
    100102import org.hibernate.engine.Mapping;
     103import org.hibernate.engine.query.QueryPlanCache;
     104import org.hibernate.impl.SessionFactoryImpl;
    101105
    102106/**
     
    199203      cfg.configure();
    200204      sf = cfg.buildSessionFactory();
     205      /*
     206        =========================================================
     207        VERY UGLY hack to solve a bug in Hibernate. See
     208        http://opensource.atlassian.com/projects/hibernate/browse/HHH-4065
     209        The hack replaces an internal query cache in Hibernate
     210        with an implementation that simply ignores everything that
     211        Hibernate tries to put in it. This code should be removed
     212        as soon as Hibernate has released an update that fixes the
     213        issue.
     214        =========================================================
     215      */
     216      try
     217      {
     218        SessionFactoryImpl sfi = (SessionFactoryImpl)sf;
     219        QueryPlanCache qpc = sfi.getQueryPlanCache();
     220        Field pcf = QueryPlanCache.class.getDeclaredField("planCache");
     221        pcf.setAccessible(true);
     222        pcf.set(qpc,
     223            new SoftLimitMRUCache()
     224            {
     225              private static final long serialVersionUID = -7082205080525320488L;
     226              @Override
     227              public Object put(Object key, Object value)
     228              {
     229                return null;
     230              }
     231            }
     232        );
     233      }
     234      catch (Exception ex)
     235      {
     236        log.warn("Could not replace query plan cache in Hibernate", ex);
     237      }
     238      // =========================================================
     239
    201240    }
    202241    catch (HibernateException ex)
     
    17831822      news = HibernateUtil.loadData(session, NewsData.class, newsId);
    17841823      ok = news == null;
     1824      /*
     1825      ===============================================
     1826      Code used to test the bug described in #init2()
     1827      ===============================================
     1828      System.out.println("Selecting news 1:");
     1829      session = HibernateUtil.newSession();
     1830      String hql = "SELECT n FROM NewsData n";
     1831      Filter f = session.enableFilter("memberOf");
     1832      List<Integer> idList = new ArrayList<Integer>();
     1833      idList.add(1);
     1834      f.setParameterList("items", idList);
     1835      f.setParameter("owner", 0);
     1836      Query q = session.createQuery(hql);
     1837      System.out.println(q.list());
     1838      session.close();
     1839     
     1840      System.out.println("Selecting news 1+2:");
     1841      session = HibernateUtil.newSession();
     1842      f = session.enableFilter("memberOf");
     1843      idList.add(2);
     1844      f.setParameterList("items", idList);
     1845      f.setParameter("owner", 0);
     1846      System.out.println("creating query");
     1847      q = session.createQuery(hql);
     1848      System.out.println("query created");
     1849      System.out.println(q.list());
     1850      session.close();
     1851      */
     1852     
    17851853    }
    17861854    finally
Note: See TracChangeset for help on using the changeset viewer.