spring-projects / spring-projects/spring-session

Update an object stored

Open
#648 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
1.9k
Forks
1.2k
Avg merge
4h 27m
Merged PRs (30d)
55

Description

Hello,

I am trying to use spring-session-data-redis:1.2.2 on an existing project built with Spring/Spring MVC.

At the moment we have java objects stored in the session, and such objects are retrieved a modified in different parts of the code. Just as a simplified example, consider the simple object:

public class TestObject implements Serializable{
    private static final long serialVersionUID = -1283966188531894027L;  
    private Integer val;
    public TestObject(Integer val){
        this.val = val;
    }

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer val) {
        this.val = val;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((val == null) ? 0 : val.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TestObject other = (TestObject) obj;
        if (val == null) {
            if (other.val != null)
                return false;
        } else if (!val.equals(other.val))
            return false;
        return true;
    }   
}

At a certain point in the flow we retrieve the object an modify the internal value doing something like:

TestObject t = (TestObject)WebUtils.getSessionAttribute(request, "testAttribute");
t.setVal(99);

And after in the flow we retrieve it and read the value doing something like:

TestObject t = (TestObject)WebUtils.getSessionAttribute(request, "testAttribute");
int value = t.getVal();  //here we expect to see 99

Before trying to use spring-session, here value was 99.
But now when I retrieve the value, I don't see 99, but I see the original value that the TestObject had when he was initially stored in the session.

Is there something I can configure in order to be able to replicate the behaviour I had before using spring-session?

Few notes:

  • redisFlushMode is configured to IMMEDIATE
  • I appreciate that I could force a rewrite of the TestObject after the value is modified doing something like:
TestObject t = (TestObject)WebUtils.getSessionAttribute(request, "testAttribute");
t.setVal(99);
WebUtils.setSessionAttribute(request, "testAttribute", t);

but I was curious to know if there was something I could do without modifying the existing code.

Thanks in advance for your help.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing how spring-session-data-redis 1.2.2 persists session attributes and how redisFlushMode IMMEDIATE handles in-place mutations. Reproduce the example with a mutable Serializable object, then determine whether the existing behavior can be configured without resetting the attribute. Done means the behavior or limitation is confirmed and documented with a supported workaround if needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.