2

Why is it that with OpenSessionInViewFilter and Hibernate, we need to use the below code and manually flush objects during update!

sessionFactory.getCurrentSession().flush();

That even if we annotate @Transactional on the Service class for which the expected behaviour is that the spring transaction manager takes care of the above duty.

Weird and inconsistent or am I missing something?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Vishwas Shashidhar
  • 807
  • 2
  • 9
  • 23
  • 1
    That shouldn't be needed, so I would say something is wrong in your setup. – M. Deinum May 29 '14 at 18:36
  • Well, as far as I know, I don't see what's wrong with my setup really..If you could have a look at this thread where I've posted my entire code, maybe you can help me. Thanks http://stackoverflow.com/questions/23938580/hibernate-update-problems-opensessioninviewfilter – Vishwas Shashidhar May 30 '14 at 03:30
  • Would you try using `DataSourceTransactionManager` instead of `HibernateTransactionManager`? – Kalyan May 30 '14 at 08:49

2 Answers2

1

With OSIV the Hibernate Session remains open even beyond the service layer boundaries.

But it's the service layer who's in charge of the transaction logic so the default flush mode is MANUAL. In the service layer the flush mode switches back to AUTO while it get's back to MANUAL for the view part.

The view part will require the session open but since entities are still attached, you don;t want any changes to be propagated to the database. That's why you disable automatic flushing for the UI rendering phase.

As for manual calling flush, I doubt you need that. Both Spring and Seam have transaction management support and the flushing is taken care of by the OSIV implementer.

You need to read this too and decide for yourself whether it's worth the trouble. I only used it once and ever since I've consider OSIV a 'cargo cult programming' anti-pattern.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Yes, I've understood the whole concept behind OSIV and the flush mode being made AUTO only at the service layer (where we declare the @Transactional annotation). Like you mentioned, ideally Spring has to take care of this, and it is doing so while saving an object, but whereas while updating an object, for some reason, it simply isn't flushing...I've posted my entire code in the below thread, if you could go through it and help me, would be grateful: http://stackoverflow.com/questions/23938580/hibernate-update-problems-opensessioninviewfilter – Vishwas Shashidhar May 30 '14 at 03:34
  • And thanks for the links...they were quite interesting. – Vishwas Shashidhar May 30 '14 at 05:20
1

Solved after moving the transaction manager configuration to the application context xml from the dispatcher

More details in this thread: Hibernate Update Problems - OpenSessionInViewFilter

Community
  • 1
  • 1
Vishwas Shashidhar
  • 807
  • 2
  • 9
  • 23