java - Transactions with Spring Cache Abstraction and JDBC DAO -
i'm testing out spring's @cacheable features ehcache can't find on whether works spring's @transactional annotations.
i placing @cacheable , @cacheevict on dao methods while @transactional on service methods.
suppose user dao looks this:
@cacheable(value="users", key="#id") user find(biginteger id); @cacheevict(value="users", key="#user.id") void update(user user); @cacheevict(value="users", key="#id") void delete(biginteger id); a problem may arise when, example, getuser() called while removefriend() in progress, because user stale friend count re-cached (or it?):
public user getuser(userid) { return userdao.find(userid); } @transactional public void removefriend(userid, friendid) { frienddao.remove(friendid); user.setfriendcount(--numfriends); userdao.update(user); // other stuff } how can ensure doesn't update cache before database transaction has completed? place @cacheevict on service methods in addition dao methods? or, add read/write locking service methods? , if locking, there libraries lock based on id since want lock on each user instead of locking globally, e.g. @guardedby("userlocks.getreadlock(#userid)")? there accepted way of handling caching , transactions?
many thanks!
i should've looked ehcache documentation bit more because answer here.
ehcache 2.4+ works spring's @transactional annotation. need configure transaction manager lookup.
Comments
Post a Comment