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 343203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:52:32+00:00 2026-05-12T10:52:32+00:00

today I have coded a test case for my application, to see how transactions

  • 0

today I have coded a test case for my application, to see how transactions behave. And I have found that nothing works the way I thought it does.

I have a Spring-based application using Hibernate as JPA provider, backed by MySQL.
I have DAO objects, extending Spring’s JpaDaoSupport. These are covered by Spring’s transaction management.

The test case I’ve created works like this:
1) An entity is created, with some counter set to 0.
2) Then two threads are created, which both call a DAO method incrementCounter() in a loop.

I thought that when DAO methods are covered with transaction, then only one thread will be inside it (i.e., Spring will take care of synchronization). But this has proven to be false assumption.

After (temporary) solving this by adding synchronized to the DAO method, I found out that Hibernate does not store the changes made by the DAO method, and the other thread, when find()ing the entity, has old data. Only explicit call of this.getJpaTemplate().flush(); helped.

I also thought that entity manager would give me the same entity instance from the cache of the persistence context, but this is also false. I have checked hashCode() and equals(), and they are fine – based on entity’s bussines key.

Any comments welcome, as it seems I miss some basic concepts of how JPA / Spring works with transactions.

  1. Should DAO methods be synchronized?
  2. Should I call flush() at the end of every DAO method?
  3. Is spring responsible for making the calls to DAO methods behave transactionally? (i.e. not letting two threads work with the same entity at the same time)
  4. If not, how do I achieve this?

Note that in my test case, I use a single DAO object, but that should be OK as Spring’s beans are singletons – right?

Thanks for any help.

public class EntityDaoImpl extends JpaDaoSupport implements EntityDao {

public synchronized void incrementCounter( String znacka ) 
{

  String threadName = Thread.currentThread().getName();
  log.info(threadName + " entering do incrementCounter().");

  Entity ent = this.getJpaTemplate().find( Entity.class, znacka );
  log.info("Found an entity "+ent.getZnacka()+"/"+ent.hashCode()+" - " + ObjectUtils.identityToString( ent ) );
  log.info(threadName + ": Actual count: "+ent.getCount() );

  ent.setCount( ent.getCount() + 5 );

  int sleepTime = threadName.endsWith("A") ? 700 : 50;
  try { Thread.sleep( sleepTime ); }
  catch( InterruptedException ex ) {  }

  ent.setCount( ent.getCount() + 5 );
  this.getJpaTemplate().flush();

  log.info(threadName + " leaving incrementCounter().");
}
}

Without synchronized and flush(), this was giving me output like

Thread A: Actual count: 220
...
Thread B: Actual count: 220
...
Thread A: Actual count: 240
...
Thread B: Actual count: 250
...
Thread A: Actual count: 250

…etc, meaning that one thread has overwriten the changes from the other.

  • 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-12T10:52:32+00:00Added an answer on May 12, 2026 at 10:52 am
    1. Should DAO methods be synchronized? Mine usually are not, because they don’t have any state. No need.
    2. Should I call flush() at the end of every DAO method? No, I don’t do that.
    3. Is Spring responsible for making the calls to DAO methods behave transactionally? (i.e. not letting two threads work with the same entity at the same time). I think you’re confusing transactions, isolation, and synchronization.
    4. If not, how do I achieve this? I think you have to worry about synchronization and isolation.

    Your example is not what I would think of as a DAO. I think your test really isn’t the proper idiom. If I had an object Foo, I’d have a FooDao interface that would declare the CRUD methods for that object:

    public interface FooDao
    {
        List<Foo> find();
        Foo find(Serializable id);
        void saveOrUpdate(Foo foo);
        void delete(Foo foo);
    }
    

    It’s possible to write a generic DAO, as you’d guess from the example.

    Spring usually has a service layer that uses domain and persistence objects to implement use cases. That’s where transactions need to be declared, because the unit of work is associated with a use case and not a DAO. A DAO has no way of knowing whether or not it’s part of a larger transaction. That’s why transactions are usually declared on the service.

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

Sidebar

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer During the execution of a query SQL Server can suspend… May 12, 2026 at 4:45 pm
  • Editorial Team
    Editorial Team added an answer If you are checking using the windows task manager, although… May 12, 2026 at 4:45 pm
  • Editorial Team
    Editorial Team added an answer To perform custom steps as part of your deployment/installation you… May 12, 2026 at 4:45 pm

Related Questions

I have a bug where special characters (danish 'ø' in this case) are shown
Currently in our enterprise we have a situation that i think it's not very
Ok, I wrote this question up earlier today but I decided to delete it
I don't really know much about the internals of compiler and JIT optimizations, but

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.