Avatar billede fredand Forsker
28. marts 2008 - 15:26 Der er 3 kommentarer og
1 løsning

How to solve this concurrency saveing problem?

Hello!

I got an issue on my desk to solve.

We got an system that saves records in a database with an EJB (stateless SessionBean) that uses Hibernate.

Each record describes a Part with a name. The table, "parts", looks like:
db_id, name, version, desc (where db_id is the primary_key)

Beside the unique db_id, the name and version "should" be unique, but there is no constraints in the db for this.
The architect of the system would like to minimize database constraints. The reason for this is not have business logic in the database.

Several clients can connect to the system. These clients can save Parts in the database.
The user enters the name and desc. If there is no Part with this name, then the Part will be saved with version = 1;

If the name exists then the Part will be saved with version = 2..and so on.

The problem is that two clients can save the same Part (same name) for the first time at the same time. The situation in the database will then be:

db_id, name, version, desc
1    , a  , 1      , "A Part"
2    , a  , 1      , "A Part"

The logic is

ejb.transaction type = "RequiresNew"
...
savePart("a", "A Part")

  b = checkIfExist("a")
  if(b)
    session.save("a", "A Part", getNextVersion("a"))
  else
    session.save("a", "A Part", 1)
   

The problem seems to be that several clients can call this method simultaneous and if they are executed with same params, for a not existing Part, at the same time the method checkIfExist will return false for all calls and we get the same data in several records.

I hope that I have illustrated the problem well enough.

All comments how to solve this is most welcome and appreciated!

Best regards
Fredrik

Btw
We use Weblogic 8.1.5 for ejb container and Oracle 10g as database.
Avatar billede arne_v Ekspert
28. marts 2008 - 15:41 #1
If you make the checkIfExist (SELECT) and save (INSERT) in a single transaction
and the database connections are set to transaction isolation level repeatable read,
then you should not have a problem.

(I guess you have the same concurrency problem with getNextVersion and save, men der
skal du nok op på transaction isolation level serializable)
Avatar billede arne_v Ekspert
28. marts 2008 - 15:42 #2
And the easiest solution would be a constraint in the form of a unique index.

And it is not business logic - it is data logic.
Avatar billede fredand Forsker
28. marts 2008 - 16:05 #3
Hello Arne!

Thanks for a really fast reply!!

I quickly talked with my co-workers about your idea of a unique index and they do agree with you that this must be a perfect solution.

Thanks alot!!!

I will try it out asap!!

Btw please give svar so I can reward you mate!

Best regards
Fredrik
Avatar billede arne_v Ekspert
28. marts 2008 - 17:11 #4
OK
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester