Spring hibernate template when to use and why?

前端 未结 5 1829
日久生厌
日久生厌 2020-11-29 17:36

Greetings, Currently developing small web service application where response from web service (using CXF + Spring) processed and saved to database. To work with database I a

5条回答
  •  死守一世寂寞
    2020-11-29 18:20

    The OpenSessionInViewFilter pattern is effective. This opens a Hibernate session & binds it to your thread, during the processing of every request. OpenSessionInView also extends the Session and loadability to View rendering & the View layer, which decreases coupling & complexity (by enabling that to 'just work').

    My philosophies don't really agree with aspect-based/ declarative transaction management. I like to make major state-change/ lifecycle events 'explicit', since they should be absolutely definite -- not weakly dependent on multiple hidden & indirect layers, which may or may not work.

    It provides a point to debug at.

    TX commit is only one line of code; but it's the major one you want to breakpoint on. No longer syntactically, than a 'transactional' declaration; but a hell of a lot more definite.

    Frankly I find "user commands" or "requests", which are the proper place to initiate a transaction & control transactionality from, should be well-structured, well-identified & fairly explicit within the application.

    (I did have trouble getting the aspect class-loading stuff to work, trying it when it first came out. My assessment is that compared to well-written OO code, aspect has only limited marginal value.)

    Tip: I generally make a helper class, to make it really convenient to get the Session & to commit the Transaction.

    HbHelper or somesuch.

提交回复
热议问题