java - Hibernate: updating an object in a different Session? -
is bad practice in hibernate update object in different session created in? think answer yes, because hibernate session (by default) cache session objects, , release them when session closed or object evicted. creating object in 1 session updating in session (while object still 'alive' in first session) seems bad practice me. can explain why, repercussions? example, consider code (which shortened clarity):
private void updaterequest(request req){ //request came hibernate session mydao mydb = null; mydb = new mydao(); transaction trans = mydb.getsession().begintransaction(); mydb.getsession().update(object); trans.commit(); }
this called "session per operation anti-pattern", here quote hibernate documentation better explains issue:
do not use session-per-operation antipattern: do not open , close session every simple database call in single thread. the same true database transactions. database calls in application made using planned sequence; grouped atomic units of work. means auto-commit after every single sql statement useless in application mode intended ad-hoc sql console work. hibernate disables, or expects application server disable, auto-commit mode immediately. database transactions never optional. communication database has occur inside transaction. auto-commit behavior reading data should avoided, many small transactions unlikely perform better 1 defined unit of work. latter more maintainable , extensible.
Comments
Post a Comment