简体   繁体   中英

reduce prepared statement count in hibernate

i am struggling with hibernate prepared statement count. I am using the following JPA criteria query:

int count = 30
EntityManager manager = ...
CriteriaBuilder builder = manager.getCriteriaBuilder();

CriteriaQuery<String> select = builder.createQuery(String.class);
Root<AdministrationParameter> root = select.from(AdministrationParameter.class);
select.select(root.get(AdministrationParameter_.value));

ParameterExpression<String> peF1 = builder.parameter(AdministrationParameter_.context.getBindableJavaType(), "f1");
ParameterExpression<String> peF2 = builder.parameter(AdministrationParameter_.parameter.getBindableJavaType(), "f2");

Predicate p1 = builder.equal(root.get(AdministrationParameter_.context), peF1);
Predicate p2 = builder.equal(root.get(AdministrationParameter_.parameter), peF2);

select.where(p1, p2);

List<String> results = Collections.emptyList();
TypedQuery<String> query = manager.createQuery(select);

for (int i = 0; i < count; i++) {
    query.setParameter(peF1, administrationParameterTypeInterface.getContext());
    query.setParameter(peF2, administrationParameterTypeInterface.getParameter());
    query.getResultList();
}

The count variable is to execute the query n times, eg to run a db trace in background (the query is executed against a db2 database).

Assume count = 30 The db2 trace says, there are "30 prepares" and "30 describes", the "statement found count = 30".

Hibernate give me the same values:

EntityManagerFactory factory = ...;
SessionFactory sessionFactory = factory.unwrap(SessionFactory.class);
statistics = sessionFactory.getStatistics();
Statistics statistics = statistics.setStatisticsEnabled(true);

...running the query above...

System.out.println("prepared statement count: " + statistics.getPrepareStatementCount());//is 30
System.out.println("query cache hit count: " + statistics.getQueryCacheHitCount());//0
System.out.println("query cache miss count: " + statistics.getQueryCacheMissCount());//0
System.out.println("query execution count: " + statistics.getQueryExecutionCount());//30

According to the javadoc https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/stat/Statistics.html the statistics.getPrepareStatementCount() is "The number of prepared statements that were acquired".

Shouldn't it be 1?

Which Hibernate version are you using? This might be a bug that has already been fixed in newer versions. If updating doesn't help please create an issue in the issue tracker( https://hibernate.atlassian.net ) with a test case( https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java ) that reproduces the issue.

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