简体   繁体   中英

Caching queries in Jooq

I try to use jooq in a project, but for performance reasons I want to cache queries. At the user manual in performance consideration section , query caching is mentioned but there are no examples about how it can be achieved. So i try to cache a query (for example SimpleSelectConditionStep) as following:

SimpleSelectConditionStep query = getFromPool();
if(query != null) {
    factory.attach(query);
    query.bind(1,"John");
} else {
   // create query
   factory.select(PERSON).where(PERSON.NAME.equal("Michael"));
}
// fetch and use query
result = query.fetchOne();
.....
putToPool(query);

My software is a multi threaded web application but it is guaranteed than two different threads can not use same query at the same time (pool not return same query before it put to the pool again).

I know than factory is not a thread safe object but I wonder that using query like this in the code can cause problems?

If preparing the statement takes a long time, you might be able to use jOOQ's statement reusing feature:

http://www.jooq.org/doc/latest/manual/sql-execution/reusing-statements

This will internally keep an open statement within the query, if the same query is executed very often. Other than that, I doubt that for a simple query as in your example, you should need a cache for the jOOQ object.

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