1
votes

I am working on an assignment to make the code transactional. I am having this problem about read-only transaction for while and none of a single suggestion I found on the internet didn't work. (Spring and hibernate integrated project)

This is my read-only transactional method

@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=true 
,rollbackFor=Exception.class)
    public void 
editInternationalExportConsigment(InternationalExportConsignmentFormWrapper 
exportConssi (){}

Inside this method, there is a translator process happening. Where the process fetch (select ) data from DB and set to an Object

  Contact contact =inquiry.loadCustomerContactById(consignmentVO.getCustomerContactId().intValue()); 

    if (contact != null && contact.getCity() != null) {
          consignment.setOrgin(contact.getCity().getCountry());
          consignment.setUniqueOriginCountry((contact.getCity().getCountry()!=null)?contact.getCity().getCountry().getId():null);
          consignment.setOrginCity(contact.getCity());
    }

There are no any update or insert query run in the middle, Only select. But end of the this code snippet execution it commit the data to DB (whatever the value set to setter method will persist into DB )

Can someone please tell me what wrong is happening here. your feedback will be much appricated.

1

1 Answers

0
votes

After tedious research, I have found the answer. In our project there are two session factories are running. And also it uses spring OpenSessionInViewFilter to avoid 'lazy initializing' issue. OpenSessionInViewFilter has set flushMode to Auto. Since the OpenSessionInViewFilter keeping binding a hibernate session to the thread along in the entire process, it will override the transactional hibernate session object which gets creates once I start the new transaction.Therefore even if I kept the flushmode for transactional scope as 'COMMIT' it get override with the AUTO property by the properties declared OpenSessionInViewFilter.

When the flushMode is AUTO hibernate will flush dirty objects to DB. Read this for understand hibernate data flushin

As a solution, I manually change the flush mode to 'COMMIT' inside my transactional method.

Thank you for everyone replied and commented. :)