java - Spring @Transactional DAO calls return same object -
we using spring , ibatis , have discovered interesting in way service method @transactional handles multiple dao calls return same record. here example of method not work.
@transactional public void processindividualtrans(indvtrans trans) { individual individual = individualdao.selectbyprimarykey(trans.getpartyid()); individual.setfirstname(trans.getfirstname()); individual.setmiddlename(trans.getmiddlename()); individual.setlastname(trans.getlastname()); individual oldindvrecord = individualdao.selectbyprimarykey(trans.getpartyid()); individualhistorydao.insert(oldindvrecord); individualdao.updatebyprimarykey(individual); } the problem above method 2nd execution of line individualdao.selectbyprimarykey(trans.getpartyid()) returns exact object returned first call.
this means oldindvrecord , individual same object, , line individualhistorydao.insert(oldindvrecord); adds row history table contains changes (which not want).
in order work must this.
@transactional public void processindividualtrans(indvtrans trans) { individual individual = individualdao.selectbyprimarykey(trans.getpartyid()); individualhistorydao.insert(individual); individual.setfirstname(trans.getfirstname()); individual.setmiddlename(trans.getmiddlename()); individual.setlastname(trans.getlastname()); individualdao.updatebyprimarykey(individual); } we wanted write service called updateindividual use updates of table store row in individualhistory table before performing update.
@transactional public void updateindividual(individual individual) { individual oldindvrecord = individualdao.selectbyprimarykey(trans.getpartyid()); individualhistorydao.insert(oldindvrecord); individualdao.updatebyprimarykey(individual); } but not store row before object changed. can explicitly instantiate different objects before dao calls , second 1 becomes same object first.
i have looked through spring documentation , cannot determine why happening.
can explain this?
is there setting can allow 2nd dao call return database contents , not returned object?
you using hibernate orm , behavior if describes in hibernate documentation. in transaction chapter:
through session, transaction-scoped cache, hibernate provides repeatable reads lookup identifier , entity queries , not reporting queries return scalar values.
same goes ibatis
mybatis uses 2 caches: local cache , second level cache. each time new session created mybatis creates local cache , attaches session. query executed within session stored in local cache further executions of same query same input parameters not hit database. local cache cleared upon update, commit, rollback , close.
Comments
Post a Comment