-->
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.  [ 4 posts ] 
Author Message
 Post subject: java hibernate, Postgresql, NO_WAIT
PostPosted: Tue Mar 09, 2010 10:19 am 
Newbie

Joined: Tue Mar 09, 2010 10:09 am
Posts: 2
I want to do a select for update NOWAIT with postgres. Postgresql is actually capable of this.

For NHibernate this is supported with NHibernate.Dialect.PostgreSQL81Dialect (https://www.hibernate.org/hib_docs/nhib ... ation.html). But for java-hibernate there is no PostgreSQL81Dialect (at least I cannot find anything about it).


session.lock( cleanupSaved, LockMode.UPGRADE_NOWAIT);

leads to this sql:

select ID from CLEANUP _TREE where ID =? for update

without NOWAIT.

I also fail to pass the 'for update nowait' together with hibernate statement

DAO.getUnique("from SessionTreeCleanup where id = ? for update nowait", cleanuptree.getId());
--> unexpected token: for near line 1, column 61 [from entities.CleanupTree where id = ? for update nowait]

What are my options when I need the nowait?

Thanks for your help


Top
 Profile  
 
 Post subject: Re: java hibernate, Postgresql, NO_WAIT
PostPosted: Tue Mar 09, 2010 2:34 pm 
Beginner
Beginner

Joined: Thu Jun 21, 2007 1:47 pm
Posts: 46
Yes, actually we're seeing the same symptom. Even though we pass UPGRADE_NOWAIT to session.get() it waits, which is kinda bad.

Should it throw an exception if UPGRADE_NOWAIT is not supported?

And, is there any way to patch in support for UPGRADE_NOWAIT for Postgres?

Thanks!


Top
 Profile  
 
 Post subject: Re: java hibernate, Postgresql, NO_WAIT
PostPosted: Tue Mar 09, 2010 3:36 pm 
Newbie

Joined: Tue Mar 09, 2010 3:28 pm
Posts: 2
The problem is due to the fact that current version of Hibernate does not have nowait in its dialect, if you go have a look at the source code you will see that the methods that is supposed to append "for update nowait" string will instead delegate to the method that appends the regular string.

You can create your own dialect to solve the problem. See the following code

Code:
package books.db;

import org.hibernate.dialect.PostgreSQLDialect;

public class MyPostgreSQLDialect extends PostgreSQLDialect {
   @Override
   public String getForUpdateNowaitString() {
      return " for update nowait";
   }
   
   @Override
   public String getForUpdateNowaitString(String aliases) {
      return " for update of " + aliases + " nowait";
   }
   
   @Override
   public String getForUpdateString(String aliases) {
      return " for update of " + aliases;
   }

}


Don't forget to change the dialect class used in your persistence.xml settings.


Top
 Profile  
 
 Post subject: Re: java hibernate, Postgresql, NO_WAIT
PostPosted: Wed Mar 10, 2010 5:55 am 
Newbie

Joined: Tue Mar 09, 2010 10:09 am
Posts: 2
I've solved the problem now by using native sql:

First I try if I can lock the row:
Code:
session.createSQLQuery(
                "select id from CLEANUP_TREE where id = :id for update nowait")
                .setParameter( "id",id).list();


if I cannot lock it throws a GenericJDBCException that has nested another exception, whose message contains following text:

Code:
  catch( GenericJDBCException e)
        {
            if( e.getCause().getMessage().contains("could not obtain lock on row in relation \"cleanup_tree\""))
            {
                return false;
            }
            else
            {
                throw e;
            }
        }


If I don't get the error, I just go on an get the thing via hibernate by id.

Especially the exception-thing is not beautful but it works at least for now.

But I will try the dialect-thing, perhaps I can use it on other places as well ...

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.