Strange cached object result behavior?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange cached object result behavior?

Caden Howell
Hi,

I'm seeing some strange behavior in some of my tests.  Say we have a User class with a numeric userId field in it and various other user id information.

final int TEST_USER_ID = 1;


// userDao uses MyBatis behind the scenes.
final User testUser = userDao.getUser(TEST_USER_ID);


// In the debugger I can see that User is fully populated with name and email, and User.getUserId() == TEST_USER_ID


// Set the user ID to null (we use this for some business logic.)  We do not explicitly save this back to the database.
testUser
.setUserId(null);


// Try to load the same user into a new object
User testUser2 = userDao.getUser(TEST_USER_ID);


// In the debugger at this point testUser2.getUserId() is null.

Any idea why I might be seeing this behavior?  Seems bizarre to me, almost like it is writing the changes I make to the testUser object to the database without me asking it to.

Thanks,

Caden

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Strange cached object result behavior?

Jakub Malý
I suppose the second call does not go to the database at all, instead it returns the cached object. 
If I understand the default caching logic correctly, it works like this 

When you open a session, a cache is created. 
Every time you do a "select", the result is stored in the cache
When you do the same select with the same parameters, the result is obtained from the cache, not actually from the database.

You can turn this off by setting flushCache="true" for your particular select statement or turn it off globally by putting in  localCacheScope="STATEMENT" your config file. 

I agree the default setting is confusing.
Jakub. 




On Friday, January 9, 2015 at 7:39:15 PM UTC+1, Caden Howell wrote:
Hi,

I'm seeing some strange behavior in some of my tests.  Say we have a User class with a numeric userId field in it and various other user id information.

final int TEST_USER_ID = 1;


// userDao uses MyBatis behind the scenes.
final User testUser = userDao.getUser(TEST_USER_ID);


// In the debugger I can see that User is fully populated with name and email, and User.getUserId() == TEST_USER_ID


// Set the user ID to null (we use this for some business logic.)  We do not explicitly save this back to the database.
testUser
.setUserId(null);


// Try to load the same user into a new object
User testUser2 = userDao.getUser(TEST_USER_ID);


// In the debugger at this point testUser2.getUserId() is null.

Any idea why I might be seeing this behavior?  Seems bizarre to me, almost like it is writing the changes I make to the testUser object to the database without me asking it to.

Thanks,

Caden

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Strange cached object result behavior?

Ray Sprinkle
In reply to this post by Caden Howell
This is normal and correct.  There is only 1 copy of the object returned
from the cache.  If you are going to modify it you need to make a copy
first.

There may be a setting that changes this behavior.

On 1/9/2015 1:39 PM, Caden Howell wrote:

> Hi,
>
> I'm seeing some strange behavior in some of my tests.  Say we have a
> User class with a numeric userId field in it and various other user id
> information.
>
> |
> finalintTEST_USER_ID =1;
>
>
> // userDao uses MyBatis behind the scenes.
> finalUsertestUser =userDao.getUser(TEST_USER_ID);
>
>
> // In the debugger I can see that User is fully populated with name and
> email, and User.getUserId() == TEST_USER_ID
>
>
> // Set the user ID to null (we use this for some business logic.)  We do
> not explicitly save this back to the database.
> testUser.setUserId(null);
>
>
> // Try to load the same user into a new object
> UsertestUser2 =userDao.getUser(TEST_USER_ID);
>
>
> // In the debugger at this point testUser2.getUserId() is null.
> |
>
> Any idea why I might be seeing this behavior?  Seems bizarre to me,
> almost like it is writing the changes I make to the testUser object to
> the database without me asking it to.
>
> Thanks,
>
> Caden
>
> --
> You received this message because you are subscribed to the Google
> Groups "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email]
> <mailto:[hidden email]>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.