Announcement Announcement Module
No announcement yet.
How to apply transactions best practice for read operations? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to apply transactions best practice for read operations?

    As it is mentioned in blogs/books (e.g. Java Transactions Design Strategies by Mark Richards), read operations must have the Propagation.SUPPORTS attribute.

    In a simple Spring 3.1 MVC project with Hibernate 4.1 the scenario is:
    • Declarative transaction management using @Transactional
    • sessionFactory of org.springframework.orm.hibernate4.LocalSessionFac toryBean
    • Transaction manager of org.springframework.orm.hibernate4.HibernateTransa ctionManager
    • Service class with @Transactional(propagation=Propagation.REQUIRED)
    • Function of that Service class that only retrieves a resultset (performs read operation) with @Transactional(propagation=Propagation.SUPPORTS)
    • Function of read operation retieves the resultset using sessionFactory.getCurrentSession().get()

    Of course, when a Controller executes the function of read operation, the exception "No Session found for current thread" is raised because a transaction is not started and a session is not obtained.

    Based on the above configuration (while it is best e.g. non-invasive, less code etc) the Propagation.SUPPORTS attribute cannot be used unless a transaction is started before with Propagation.REQUIRED or Propagation.REQUIRES_NEW.

    How do we use use Propagation.SUPPORTS for read operations without having to start a transaction e.g. with Propagation.REQUIRED before?

    Thank you in advance.