Announcement Announcement Module
Collapse
No announcement yet.
Hibernate readOnly transactions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate readOnly transactions

    I'm using amixture of Hibernate and JCA data sources in a project, and I'm looking at implementing some query functions to the Hibernate Data Source as read-only transactions. It looks to me like the HibernateTransactionManager will optimise things for me nicely setting the flush mode to FLUSH_NEVER, but unless I'm missing something this behaviour isn't available when using the JtaTransactionManager.

    I've looked at extending HibernateDaoSupport to provide a second HibernateTemplate which is set to FLUSH_NEVER for read only dao methods, but I was wondering if anyone else has tried to handle this at all, and if I'm on approaching the problem the best way?

    Cheers,

    Andrew.

  • #2
    I've never noticed that FLUSH_NEVER has such a effect. But beside that, just another option that pops in my mind is using a diffrent session/sessionFactory configuring it to use a simple HibernateTransactionManager. So all your read only hibernate queries are 'outside' of the scope of the current application transaction.

    Another option would be to check the LOCK option. I am sure there is a never lock option, which is basically the same as an optimistic read only transaction. But I am not that sure, I've never run into such a problem, frankly.


    Regards,

    Martin (Kersten)

    Comment


    • #3
      Originally posted by Martin Kersten
      I've never noticed that FLUSH_NEVER has such a effect.
      OK, perhaps I'm misunderstanding something then. My understanding was that a Hibernate read-only transaction would effectively not flush (no need as it doesn't change the state of the cache at all) and potentially gain
      a perfomance benefit as a result.

      Originally posted by Martin Kersten
      But beside that, just another option that pops in my mind is using a diffrent session/sessionFactory configuring it to use a simple HibernateTransactionManager. So all your read only hibernate queries are 'outside' of the scope of the current application transaction.
      Unfortunately I don't think that will be an option for us, as we will almost certainly have transactions which will need to span both types of data source. Unless I actively code transaction handling, but we wanted to avoid that and use a declaritive approach wherever possible.

      Originally posted by Martin Kersten
      Another option would be to check the LOCK option. I am sure there is a never lock option, which is basically the same as an optimistic read only transaction.
      Thanks Martin, I'll look into that as well.

      Cheers,

      Andrew (Daws).

      Comment


      • #4
        from Hibernate code:
        Code:
          if ( session.getFlushMode()!=FlushMode.NEVER) session.flush(); 
          try { 
            session.connection().commit(); 
            committed = true; 
            ...
        from Hibernate javadoc
        Code:
        public static final FlushMode NEVER
        The Session is never flushed unless flush() is explicitly called by the application. This mode is very efficient for read only transactions. 
        ...
        Flushing is the process of synchronising the underlying persistent store with persistable state held in memory.
        FlushMode.NEVER allows to implement "readonly transactions". Unless sesion.flush is called, no synchronization will be executed resulting in a potential gain of perfomance.

        Comment

        Working...
        X