Reputation: 321
I am debugging some Groovy code for a website and have hit an issue where I create an object A
in controller in one part of the flow and set a variable within it (read it back and its correct).
I then pick up what I had understood to the the same object in a different controller. But the variable is no longer set.
Either my assumption that the object A
in the first controller is the same object A
as picked up the the second controller is wrong or something has modified the value en-route.
So, what might be a very basic question (and I have have a horrible feeling that it points to some fundamental misunderstanding on my part of how Groovy/Java works - so please be gentle) :
How can I tell if the object A
in controller 1
is the same as the object A
in controller 2 (by the same I mean point to the same object, not that they are equivalent).
Upvotes: 0
Views: 193
Reputation: 27255
I then pick up what I had understood to the the same object in a different controller.
If you show an example of what you are doing in the first controller and what you are doing in the second controller that would help clarify what is going on. It isn't clear what you might mean by "pick up" in the sentence quoted above.
If you can orchestrate things such that you have the 2 references at the same time you can call o1.is(o2)
which will tell you if o1
points to the same object as o2
. Something you can use to help debug the situation is in the first controller your can call System.identityHashCode(o1)
and in the second controller you can call System.identityHashCode(o2)
and see if those return the same value.
There are times in a web app where the notion of being the same object can be ambiguous. For example, if you have 2 separate proxies but they are proxying the same instance, there are contexts where you would treat them as the same object. Another example is that if you are dealing with persistent entities you could have 2 separate instances in memory that actually correspond to the same record in the data store.
Anyway, the identityHashCode
approach mentioned above is a technique you could use to know if these objects are the same object or not. If that doesn't do it for you and you can show some code or provide some more details that might help.
Upvotes: 2
Reputation: 20707
If you want to make some variables available in several controllers, you can do it in one of the following ways:
session
flash scope
flow scope
- here you MUST make sure, that you are accessing the SAME flow.singleton service
to persist the value permanently or temporarilystatic field
somewhere (shall never be used though)Upvotes: 1