Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:15:54+00:00 2026-05-17T01:15:54+00:00

I am using Spring+Hibernate for an operation which requires creating and updating literally hundreds

  • 0

I am using Spring+Hibernate for an operation which requires creating and updating literally hundreds of thousands of items. Something like this:

{
   ...
   Foo foo = fooDAO.get(...);
   for (int i=0; i<500000; i++) {
      Bar bar = barDAO.load(i);
      if (bar.needsModification() && foo.foo()) {
         bar.setWhatever("new whatever");
         barDAO.update(bar);
         // commit here
         Baz baz = new Baz();
         bazDAO.create(baz);
         // if (i % 100 == 0), clear
      }
   }
}

To protect myself against losing changes in the middle, I commit the changes immediately after barDAO.update(bar):

HibernateTransactionManager transactionManager = ...; // injected by Spring
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
transactionManager.commit(transactionStatus);

At this point I have to say that entire process is running in a transaction wrapped into org.springframework.orm.hibernate3.support.ExtendedOpenSessionInViewFilter (yes, this is a webapp).

This all works fine with one exception: after few thousand of updates/commits, entire process gets really slow, most likely due to memory being bloated by ever-increasing amount of objects kept by Spring/Hibernate.

In Hibernate-only environment this would be easily solvable by calling org.hibernate.Session#clear().

Now, the questions:

  • When is it a good time to clear()? Does it have big performance cost?
  • Why aren’t objects like bar or baz released/GCd automatically? What’s the point of keeping them in the session after the commit (in the next loop of iteration they’re not reachable anyway)? I haven’t done memory dump to prove this but my good feeling is that they’re still there until completely exited. If the answer to this is “Hibernate cache”, then why isn’t the cache flushed upon the available memory going low?
  • is it safe/recommended to call org.hibernate.Session#clear() directly (having in mind entire Spring context, things like lazy loading, etc.)? Are there any usable Spring wrappers/counterparts for achieving the same?
  • If answer to the above question is true, what will happen with object foo, assuming clear() is called inside the loop? What if foo.foo() is a lazy-load method?

Thank you for the answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:15:55+00:00Added an answer on May 17, 2026 at 1:15 am

    When is it a good time to clear()? Does it have big performance cost?

    At regular intervals, ideally the same as the JDBC batch size, after having flushed the changes. The documentation describes common idioms in the chapter about Batch processing:

    13.1. Batch inserts

    When making new objects persistent
    flush() and then clear() the session
    regularly in order to control the size
    of the first-level cache.

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
       
    for ( int i=0; i<100000; i++ ) {
        Customer customer = new Customer(.....);
        session.save(customer);
        if ( i % 20 == 0 ) { //20, same as the JDBC batch size
            //flush a batch of inserts and release memory:
            session.flush();
            session.clear();
        }
    }
       
    tx.commit();
    session.close();
    

    And this shouldn’t have a performance cost, au contraire:

    • it allows to keep the number of objects to track for dirtiness low (so flushing should be fast),
    • it should allow to reclaim memory.

    Why aren’t objects like bar or baz released/GCd automatically? What’s the point of keeping them in the session after the commit (in the next loop of iteration they’re not reachable anyway)?

    You need to clear() the session explicitly if you don’t want to keep entities tracked, that’s all, that’s how it works (one might want to commit a transaction without "losing" the entities).

    But from what I can see, bar and baz instances should become candidate to GC after the clear. It would be interesting to analyze a memory dump to see what is happening exactly.

    is it safe/recommended to call org.hibernate.Session#clear() directly

    As long as you flush() the pending changes to not lose them (unless this is what you want), I don’t see any problem with that (your current code will lose a create every 100 loops, but maybe it’s just some pseudo code).

    If answer to the above question is true, what will happen with object foo, assuming clear() is called inside the loop? What if foo.foo() is a lazy-load method?

    Calling clear() evicts all loaded instances from the Session, making them detached entities. If a subsequent invocation requires an entity to be "attached", it will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using Spring/Hibernate on a Websphere Application Server for AIX. On my Windows machine,
I'm writing a web application using Spring/Hibernate that displays a report to a user,
Hey I am developing an desktop application using Spring and Hibernate, and I have
I'm trying to setup Spring using Hibernate and JPA, but when trying to persist
We're using Spring 2.5.4/Hibernate 3.2/Websphere Application Server 6.1.0.17. We've deployed the application on an
I'm using Hibernate with Spring in my application. I have been consistently using detached
How do I get Spring to load Hibernate's properties from hibernate.cfg.xml ? We're using
I'm using hibernate validator framework with Spring. A class implementing the Spring Validator validates
We are using Hibernate 3.1 with Spring MVC 2.0. Our problem occurs when data
I am trying to develop a .NET Web Project using NHibernate and Spring.NET, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.