Use NHibernate to insert nearly 100,000 records into the database, as in the following example:
ISession session = session. BeginTransaction (); for int i=0; i<100000; i++ ) { new Customer (...); Session. Save (customer);} Tx.commit (); session. Close ();
This will throw OutOfMemoryException and terminate at about 50 000 records. This is because nhibernate caches all newly inserted customer instances in the Session-level cache.
When you persist a new object, you must constantly control the size of the first-level cache with flush () and then clear () session.
ISession session =sessionfactory.opensession (); ITransaction TX=session. BeginTransaction (); for(intI=0; i<100000; i++) {Customer Customer=NewCustomer (...); Session. Save (customer); ifI -==0) {//same as the ADO batch size//flush a batch of inserts and release memory:session. Flush (); Session. Clear (); }} tx.commit (); session. Close ();
NHibernate processing method for large batch Insert database NHibernate batch Processing