Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-83452

If MvccVersion is incremented, value returned from cache by service can be obsolete

    Details

    • Type: Bug
    • Status: Closed
    • Resolution: Won't Fix
    • Affects Version/s: 7.0.X, 7.1.X, Master
    • Fix Version/s: None
    • Labels:
      None

      Description

      Objects cached in EntityCache are just a representation of the Hibernate session object. That can cause, when calling a service that updates an object with a mvccVersion field and returns the cached modified object, that the returned object is obsolete.

      Steps to Reproduce:

      1. Execute the following script: 
        import java.io.PrintWriter;
        import com.liferay.portal.kernel.model.Address;
        import com.liferay.portal.kernel.service.AddressLocalServiceUtil;
        import com.liferay.portal.kernel.service.ServiceContext;
        import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
        import com.liferay.portal.kernel.model.Company;
        import com.liferay.portal.kernel.model.User;
        import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
        
        public class CreateAndUpdateAddress {
            PrintWriter out = null;
            CreateAndUpdateAddress(PrintWriter out) {
                this.out = out;
            }
            
            public Address createAndUpdate() {
            
                try {
        
        
                    long companyId = CompanyThreadLocal.getCompanyId();
                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
                    User user = company.getDefaultUser();
        
                    ServiceContext sc = new ServiceContext();
                   
                    sc.setUserId(user.getUserId()); 
                    sc.setScopeGroupId(company.getGroupId()); 
        
                    Address address = AddressLocalServiceUtil.addAddress(
                        sc.getUserId(), "any", 0, "7sirc",
                        "pm", "0", "city", "6060", 0,
                        1, 2, false, false,
                        sc);
        
                    address.setCity("other");
                    address.setZip("2000");
        
                    address = AddressLocalServiceUtil.updateAddress(address.getAddressId(), address.getStreet1(), address.getStreet2(), address.getStreet3(), address.getCity(), address.getZip(), address.getRegionId(), address.getCountryId(), 1, false, false);                       
        
                    AddressLocalServiceUtil.updateAddress(address);
        
                    return address;
                } catch(Exception e) {
                    out.println(e);
                    return null;
                }
            }
        }
        (new CreateAndUpdateAddress(out)).createAndUpdate();
        

       

      Expected Results:

      No exception should be printed

       

      Actual Results:

      An exception due to being the object stale is printed.

       

      The reason strives in the fact that the entity cached object is a representation of Hibernate session object. When exiting the service method the commit gets done the mvccVersion gets incremented. We change in MVCCSynchronizerPostUpdateEventListener the cached object, but not the one that is already returned. So if we try to modify it afterwards we aren't going to be able because is obsolete.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                shuyang.zhou Shuyang Zhou
                Reporter:
                mariano.alvaro Mariano Álvaro
                Participants of an Issue:
                Recent user:
                José María Muñoz
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  1 year, 18 weeks, 4 days ago

                  Packages

                  Version Package