-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Concurent problems
PostPosted: Tue Feb 13, 2007 4:34 am 
Newbie

Joined: Tue Feb 13, 2007 4:20 am
Posts: 3
Location: Ukraine
Hi all.
I need to implement a counter logic. Database should contain a table COUNTER which whill consits of such columns:
id: number
name: varchar
count: number

Also I have a class Counter.java which is mapped to previous table.
Each counter has its name and an index. Concurent thread try to get counter by name, if counter doesn't exist thread creates it otherwise - just increments counter's index.

I have read many books and articles about concurent programming with hibernate but I still has no ideas how to prevent concurent attempts of row creation.

I understend than the solvation is connected with usage of pesimistic locking, but hibernate doesn't allow to lock a table. It only allows to lock a row connected with existent instance.

An another approach I have considered is to use serializable isolation level of transaction. Accordingly to documentaion such level of transaction will not allow concurent creation of rows. But such approach is not effective. My test still fails with primary key constaint violation.

I have no other ideas. Please help me to understand what should be done to prevent concurent row creation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 1:17 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi silj,

Use version column inDB and map it with

<version /> in HBM

or you could use timestamp

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 1:51 am 
Newbie

Joined: Tue Feb 13, 2007 4:20 am
Posts: 3
Location: Ukraine
dharmendra.pandey wrote:
Hi silj,

Use version column inDB and map it with

<version /> in HBM

or you could use timestamp


Hi. Thanks for the previoous reply. I cannot use nether version number nor timestamp because:
1) I'm using a legacy database. I have no possibilities to change it.
2) My application is not the only who uses this database. There are many other legacy applications which are usning it too.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 5:13 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi silj,
then i don't think you are able to do that from Hibernate.Try to design Lock manager.Or you could ask from Hibernate Team.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 5:29 am 
Newbie

Joined: Tue Feb 13, 2007 4:20 am
Posts: 3
Location: Ukraine
I have created a temporary solution. I'm using such method at the begining of a transaction:
Code:
public class LockTableUtil
       private LockTableUtil(){}
public static void lockExclusive(Connection connection, String tableName)
         throws SQLException{
      PreparedStatement statement = connection
            .prepareStatement("LOCK TABLE " + tableName +" IN EXCLUSIVE MODE");
      try
      {
         statement.execute(); 
      } finally
      {
         try
         {
            statement.close(); 
         }  catch (Exception e) {
            // Swallow
         }
      }
   }

This works. I'm planning move this code to interceptor.
I hope that it's temporary solution and the best one should be found.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.