Pregunta

I have enabled second level and query caches in my app Looks like when I invoke following code

String sql = "update SOME_TABLE set SOME_FIELD=somevalue";
SQLQuery query = getSession().createSQLQuery(sql);
query.executeUpdate();

hibernate updates UpdateTimestampsCache for ALL tables. Why he does this? I have about 1000 tables and many sql queries in my app. I dont need this updates because I dont update cached tables via sql. It causes huge netwrok traffic and slowneess of application.

Is there a way to tell hibernate to NOT do any updates when running sql queries?

¿Fue útil?

Solución 2

I found solution!

You can use addSynchronizedEntityClass() method

String sql = "update SOME_TABLE set SOME_FIELD=somevalue";
SQLQuery query = getSession().createSQLQuery(sql);
query.addSynchronizedEntityClass(SomeTable.class)
query.executeUpdate();

In this case it will reset just cache for SOME_TABLE

Otros consejos

You can try using the StatelessSession instead of the normal session. You can also use straight JDBC instead of createSQLQuery method of hibernate.

The other option is to use HQL update query instead of SQL. That might be able to figure out which entity you are updating and only invalidate the query results for that query. When you fire a SQL query hibernate doesn't know which table is being updated - so to be on the safer side it will mark all query cache results as invalid.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top