-->
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.  [ 11 posts ] 
Author Message
 Post subject: Disable Session Level Cache
PostPosted: Sun Feb 29, 2004 9:53 am 
Regular
Regular

Joined: Sun Jan 18, 2004 9:43 am
Posts: 50
Hi all

Version: Hib 2.1

I have asked how to disable Session Level Cache before in the forum and I have told that it cannot be disabled. I have performed a test:

Code:
        for (int i = 0; i < 10; i++) {
            Cardno c = (Cardno) sess.load(Cardno.class, cardNo);
            System.out.println("Name: " + c.getCarName());

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
        }


Then, I have changing the value of the record during the execution of the program and I found that the value get returned from Hib didn't get updated. Since, I have multiple programs accessing the database, how can I ensure some exclusive access to the data? I need this for key generation.


Thanks
[/code]

_________________
Edmond Hung
Credit Card DNA Security System (Holdings) Ltd.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 10:03 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
There is no meaning to dissable it (It has meaning if transactions are dissabled too). Just use sequence or some UID generation algorythm there is nothing dependant on session cache, transactions isolate your applications in the same way too.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 8:59 am 
Regular
Regular

Joined: Sun Jan 18, 2004 9:43 am
Posts: 50
baliukas wrote:
There is no meaning to dissable it (It has meaning if transactions are dissabled too). Just use sequence or some UID generation algorythm there is nothing dependant on session cache, transactions isolate your applications in the same way too.


hm... I am working on a existing application that I want to migrate it to Hib. I need to use the key table to manually create the sequence numbers.

If I can't disable second level cache, is there anyway to lock the record at the time it is being retrieved?

Thanks!

_________________
Edmond Hung
Credit Card DNA Security System (Holdings) Ltd.


Top
 Profile  
 
 Post subject: ???
PostPosted: Mon Mar 01, 2004 10:01 am 
Regular
Regular

Joined: Sun Jan 18, 2004 9:43 am
Posts: 50
Hi all

I have tested if the transaction is working properly when there are more than one program access the DB. The following simple program generate 10000 keys and I run another program that also generate 10000 keys.

Code:
        for (int i = 0; i < 10000; i++) {
            t = sess.beginTransaction();
            KeyTable k = (KeyTable) sess.load(KeyTable.class, new Integer(1));
            k.setNextKey(k.getNextKey() + 1);
            t.commit();
        }


The second program uses direct SQL to generate the key -

"update keytable set nextkey = nextkey + 1 where keyId = 1"

However, when the program finishes the nextkey value only increased by 18554 and there was about 1500 keys missing.


I have run Two second programs at the same time, then no key is missing. So, it is should not be the problem in the DB.


Does anyone know how to do this? Please Help!

_________________
Edmond Hung
Credit Card DNA Security System (Holdings) Ltd.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use session.load(object, type, LockMode.UPGRADE) for explicity pessimistic locking.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 9:14 pm 
Regular
Regular

Joined: Sun Jan 18, 2004 9:43 am
Posts: 50
michael wrote:
You can use session.load(object, type, LockMode.UPGRADE) for explicity pessimistic locking.


I updated the code as below:

Code:
        for (int i = 0; i < 1000; i++) {
            t = sess.beginTransaction();
            KeyTable k = (KeyTable) sess.load(KeyTable.class, new Integer(1), LockMode.UPGRADE);
            k.setNextKey(k.getNextKey() + 1);
            t.commit();
        }



However, when I run the test again - run this program and run another program that executes "Update KeyTable set NextKey = NextKey + 1 where KeyId = 1" for 10000 times, I found that the key only increased by 10853 and there were still keys losted.

Thanks for your advice!

_________________
Edmond Hung
Credit Card DNA Security System (Holdings) Ltd.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 2:24 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
This key generation algorythm depens on concurency control algorythm.

"update keytable set nextkey = nextkey + 1 where keyId = 1"

This SQL statement is executed this way:

Transaction puts read lock selects record(key = 1), puts write lock and increases nextkey. Two transaction can read/read or read/write value concurenty, but not to write/write. As I understand DB must reject one of transactions if both transactions read the same value before one write.

T1( r ) ->T2( r )->T1(w)(rw lock, wait T2 )->T2(r)(rw lock, wait T1)

It looks lik deadlock, is not it ?


Top
 Profile  
 
 Post subject: refresh after lock
PostPosted: Thu Mar 11, 2004 2:35 am 
Newbie

Joined: Thu Mar 11, 2004 1:33 am
Posts: 5
Location: Taipei, Taiwan
Well, this will work:

aEmp = (Employee)session.load(Employee.class, aPK);
session.lock(aEmp, LockMode.UPGRADE);
session.refresh(aEmp);
Transaction tx = session.beginTransaction();
aEmp.setXxx(xxx);
...
tx.commit();

To refresh() after lock() will let the 2nd concurrent thread re-read database after the blocking of lock is released. I tested it in the ThreadLocal pattern where there are separate Hibernate sessions maintaining different copies of PO relative to the same db record.
Also, Oracle9 dialect is used to ensure DB native lock scheme.

I think the main issue is that both session.lock() and session.load() with a nontrivial LockMode lock the record AFTER the data is retrieved. There is no method in Hibernate Session to lock BEFORE retrieve.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 2:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Do you realize that load() and get() can specify a LockMode


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 4:07 am 
Newbie

Joined: Thu Mar 11, 2004 1:33 am
Posts: 5
Location: Taipei, Taiwan
gavin wrote:
Do you realize that load() and get() can specify a LockMode


YES!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 4:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
LockMode just inserts the appropriate locking hints to the query, it is still your job to lock at the right points, just like you'd have to do with plain JDBC.


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