Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Hibernate auto update on flush on read only synonyms

I have a table and two databases which have the same table, but one is a symlink of the other one and only read is permitted on this table.

I have mapped the table to Java using Hibernate and I use spring to set the Entity Manager's data source as one of the two databases based on some input criteria.

I call only read only operations (selects) when I am connected to the second database, but it seems Hibernate tries to flush something back to the database and it fails telling update is not allowed on this view.

How do I disable this update only for the second datasource and keep it normal for the first one?

Update: Looking at the stack trace, the flush seems to be started here:


          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
          ... 55 more

Is this related to hibernate.transaction.flush_before_completion property? Can I set it to false for the second data source?

like image 366
Arvind Avatar asked Jun 29 '09 05:06

Arvind


1 Answers

Most probably your entities become "dirty" the same moment they are loaded from the database, and Hibernate thinks that it needs to store the changes. This happens, if your accessors (get and set methods) are not returning the exact same value or reference that had been set by Hibernate.

In our code, this happened with lists, developers created new list instances because they didn't like the type they got in the setter.

If you don't want to change the code, change the mapping to field access.

You can also prevent Hibernate of storing changes by setting FlushMode to never on the session, but this only hides the real problem which will still occur in other situations an will lead to unnecessary updates.

like image 118
Stefan Steinegger Avatar answered Oct 13 '22 02:10

Stefan Steinegger