Whether or not you are comfortable with the use of hibernate in the face of complex business processing, as described in exception-found shared references to a collection

Source: Internet
Author: User

Let's talk about the project requirements first: because a Production Evaluation (estimation) may need data from an old version, as they can imagine, for example, this factory helped KFC to produce a batch of cake boxes last time. This is McDonald's order and a batch of cake boxes need to be produced. The size and process are similar to those of machines, but you need to dulicate it, and then go in and make some modifications. Therefore, we need to create a batch Bob in the background, and copy some data from the old data, probably from 9 tables, first query, and then insert. If Hibernate is not used, the logic is simple, query, and loop insertion. But later I found that

"Found shared references to a collection "!!!

It has been plagued by about one day, two people. Later, we found that the session and transaction are the two things starting from the beginning. Because our data is associated, lazy = false is set. Obtain the data first at the service layer. The method is probably like this.

Public void saveestaimtionfromoldversion (integer oldversionestimationid ){

List list = estimationdao. getestiamtionbyid (oldversionestimationid );

For (iterator it = List. iterator; it. hasnext ();){

// Processonerecord

// Save new data to 9 tables ......

}

}

This method is written in the service layer, and our transaction control with spring is also at this layer.

Everything works normally when debugging is passed, but the above error is reported when the thread leaves this method. Later, we suspected that the session did not release the data queried from the database. The official bean was called an unmanaged bean because it was always at the service layer and the session would never be flushed () note that the flush () action is controlled by spring; the session does not perform the flush () action, so the reference of which objects in the action always exists, because the new reference is new. Later, I made a change, that is, when the data is recycled out, the memory reference = NULL, so that everything is normal. however, such code is full of such business-independent code, which is at the cost of using hibernate.

Later, I changed the program.

 

Public void saveestaimtionfromoldversion (list oldestimation) {// This is not an integer, but a list

For (iterator it = oldestimation. iterator; it. hasnext ();){

// Processonerecord

// Save new data to 9 tables ......

}

}

Put the queried data action before the batchjob...

Everything is normal...

 

Another solution is to associate the features ---- remove all many2one and one2133 without using the hibernate cascade. In this way, the data queried through hql or SQL is normal. I guess the session will be specially processed if it is queried through SQL, and there will be no reference in the session.

Welcome to the discussion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.