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

  • SEARCH
  • Home
  • 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 4251170
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:37:33+00:00 2026-05-21T04:37:33+00:00

I’m really upset with Hibernate! I have a database table (mysql) that holds parent-child

  • 0

I’m really upset with Hibernate!

I have a database table (mysql) that holds parent-child relationships that allow me to build a tree of categories. I have multiple threads which can try to get and if not there create the category path (with several parent-child rows implied) at roughly the same time.

The problem is that I’m using TRANSACTION_READ_COMMITTED only, and so race conditions can occur where a thread can create a parent-child for a category subpath because it didn’t find it, and then (lo!) another thread did this at the same time. To try to solve this problem, I put a unique constraint on the parent/child ids and a unique constraint on the full category path. Then, I hoped that in my session, I would catch the hibernate ConstraintViolationException, and knowing that another thread wrote the new relationship for me, I query for the row that the other thread wrote in the catch clause. And try to continue in that thread doing all the things it needs to do with the session.

This is the ONLY way I can think of to solve the problem of multiple threads doing the work of get/creating the same long category path (with several sub parent-child relationship rows) at the same time, and ensuring that the unique constraint is upheld.

But hibernate invalidates the session on ConstraintViolationException and ultimately throws an assertion exception (“null id in com.stagirite.bean.Category entry (don’t flush the Session after an exception occurs)”) so my solution is unviable.

How am I going to solve this throughout my application for “get/create” models that don’t create duplicate rows, without using a pessimistic lock?

Andy

  • 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-21T04:37:33+00:00Added an answer on May 21, 2026 at 4:37 am

    Since I am not sure of how you have modelled your data (Self referential table or marterialized path) and the corresponding hibernate mapping, I am going to attempt answering the part where two threads are trying to change the same thing. Have you tried using the hibernate versioning feature. In short with versioning if one thread has already updated a category, the version number is incremented. The other thread which may have started the update but did not finish it will see the version change and rollback. It would help if you could post your data model.

    Hope that helps.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.