初探hibernate和spring整合得事務控制原理,歡迎交流。

來源:互聯網
上載者:User
For contrasaction by Spring,commonly,we take the tier of service for transaction control,but when we integrate hibernate to spring, Spring use Session gothrough the whole process, that is to say when the thread going to the method of service tier-- if the prefix of method is defined in the strategy in xml ,the transaction will effect, whatever you did in this operation to database,it will in one transaction in common,during this process, if happen exception, it will rollback. this is as to useaual. But for Session of hibernate integerated with Spring,during the current transaction, if you perform batch job since the BO has complicated realationship like one-to-many,or many-to-one, some times  it will happen a error as "found shared references to a collection".If you think more for detail of transaction by Spring integreted Hibernate, you will get why happen.
if you process like this,
1.get data list from one database,update some,and then update it to DB,or get the data and then create new record which data from pre data list.
to avoid this , you can do in two ways:
a. make simple for data structure in bo and get data by writing sql or hql.if you get data in this way, when create or update database, you can do anyway if you like as no  need to to warry about anything in service tier and no need to make the thread leave this tier.
b.for above case,if you don't remove the complicated data structure and realationship between BO,when creating or updating,due to the session have  the reference of BO, so if you want to create or update as usual,hibernate will happen error as above, for this, we can think a another way, which  is getting data before calling method of serivce tier. and then parse the data to serivce tier as once your thread leave service tier, sesion will flush automaiclly. hahahahaha....
one commont:the much you use spring integerated hibernate, the more intresting things you will see. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.