-->
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: Isolation misunderstood
PostPosted: Tue Aug 17, 2004 1:54 pm 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
Hi!!!

I need some help from Hibernate gurus. I'm not be able to understand completely the use of isolation levels in Hibernate. I try to expose my problem in a few lines.

I have 2 threads A and B, each of one with a different Session object. Both access to the same persisten data P, and execute the next operations:

A -> get session
A -> begin tx
B -> get session
B -> begin tx
B -> update(P)
B -> commit tx
B -> close session
A -> update(P)
A -> commit tx
A -> close session

I hope that in the 'commit' step, I acquired an exception because the B thread commit first the changes, but it could update the same data. With tradtional JDBC I'd obtained an exception.

How I can obtain the same result using Hibernate? Must I use version or timestamp columns or is enough setting the isolation level property in Hibernate configuration file?

For the using of Lock class, is it necessary add version/timestamp column?

I review for hours this forum, but I could resolve my dues. Below you can see my code.

Thanks in advance,

P.D. I sent this topic, by mistake, in another forum. I'm sorry






Hibernate version:2.1.6

Mapping documents:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="com.bbvasuiza.titulares.persistence.vo.TitularVO"
table="DC_TITULARES"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="ID_TITULAR"
type="java.lang.Integer"
>
<generator class="identity">
</generator>
</id>

<property
name="annulationDate"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="DATE_ANNUL"
/>

<property
name="birthdate"
type="java.sql.Date"
update="true"
insert="true"
access="property"
column="DATE_BIRTH"
/>

<property
name="businessPhone"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="PH_BUSINESS"
/>

<property
name="cdNatures"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CDNATURES"
/>

<property
name="code"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CODE_TITUL"
/>

<property
name="codePoder"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CODE_PODER"
/>

<property
name="comments"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="COMMENTS"
/>

<property
name="docId"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="DOCID"
/>

<property
name="docIdEnd"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="DOCIDEND"
/>

<property
name="docIdStart"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="DOCIDSTART"
/>

<property
name="docNo"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="DOCNO"
/>

<property
name="domicile"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="DOMICILE"
/>

<property
name="domicileCountry"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="DOM_COUNTR"
/>

<property
name="ebk"
type="java.lang.Boolean"
update="true"
insert="true"
access="property"
column="FLAG_EBK"
/>

<property
name="economyActivity"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ECOACTIV"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="EMAIL"
/>

<property
name="entryDate"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="DATE_ENTRY"
/>

<property
name="fax"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="FAX"
/>

<property
name="firstNationality"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="NATIO"
/>

<property
name="homePhone"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="PH_HOME"
/>

<property
name="idCLL"
type="java.lang.Integer"
update="true"
insert="true"
access="property"
column="IDCLL"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="LASTNAME"
/>

<property
name="mobilePhone"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="PH_MOBILE"
/>

<property
name="modificationDate"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="DATE_MOD"
/>

<property
name="origin"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ORIGIN"
/>

<property
name="personalId"
type="java.lang.Integer"
update="true"
insert="true"
access="property"
column="IDPERSL"
/>

<property
name="preName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="PRENAME"
/>

<property
name="residence"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="RESIDENCE"
/>

<property
name="residenceCountry"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="RES_COUNTR"
/>

<property
name="secondNationality"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="NATIO2"
/>

<property
name="userMod"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="USER_MOD"
/>

<property
name="web"
type="java.lang.Boolean"
update="true"
insert="true"
access="property"
column="WEB"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-TitularVO.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>



Name and version of the database you are using:SQL Server 2000 sp3

Code:
Thread A

        task = new TitularUpdateSessionTask(titular);
        worker = new Thread(task, task.getClass().getName());
       
       
        try {           
            Transaction tx = HibernateUtil.beginTransaction();           
            worker.start();           
            Thread.currentThread().sleep(1000);
            titular.setComments("Updated by TitularHibernateDAOTest");
            dao.update(titular);
            HibernateUtil.commitTransaction(tx);
        } catch (TitularDAOSysException expected) {
            log.error("Error updating", expected);
            assertTrue(true);
        } catch (HibernateException nonExpected) {
            log.error("Error updating", nonExpected);
            fail();
        } catch (Exception e) {
            log.error("Error updating", e);
            fail();
        }       



Code:
Thread B

    public TitularUpdateSessionTask(TitularVO titular) {
        this.titular = titular;
        //this.s = s;
    }
   
    public void run() {
        try {
            s = HibernateUtil.currentSession();
            dao = new TitularHibernateDAO();
            dao.setSession(s);
            titular.setComments("Updated by TitularUpdateSessionTask");
            tx = HibernateUtil.beginTransaction();
            dao.update(titular);
            HibernateUtil.commitTransaction(tx);
            HibernateUtil.closeSession();
        } catch (Exception nonExpected) {
            log.error("Error updating", nonExpected);
        }
    }
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi,
Quote:
With tradtional JDBC I'd obtained an exception
--> how?

you only need version to check if someone has updated the object before you

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 5:56 pm 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
anthony wrote:
hi,
Quote:
With tradtional JDBC I'd obtained an exception
--> how?

you only need version to check if someone has updated the object before you


Ok, do you mean should I use optimistic locking I have to use a version/timestamp column? And, on the other hand, should I use pessimistic locking I have to set locks explicitly using the Lock class o lock() method?

Another question, when I don't use pessimistic locks nor version/timestamp column, what kind of isolation applies? I understood that the default of the database. If so, I'm confused. SQL Server establishes READ COMMITTED as default, but my A thread can finish its tx although the B thread modify the record. Does not relie Hibernate over the tx manager of the database?

Thanks for your help,

Roque


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 6:00 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can get more info in the reference guide
http://www.hibernate.org/hib_docs/refer ... tions.html

in my opinion, you should always avoid pessimistic when you can...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 6:30 pm 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
I red this chapter several times, but I don't understand it well.
My case is the described in "Many sessions with automatic versioning". I'm creating a web application (without application server). When it is said "However, the same persistent instances are reused for each interaction with the database". It means that if you want to update the object with id "id", have you to load it before you can update it?
, or can you have a new created object (for example in a GUI) with the same id of the object you want to update and the execute session.update(guiObject)?

And please, could you respond my dues of the last message? I need your help.

Thanks,

Roque


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 9:09 am 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
I cotinue with my problem...

Can anybody show me a few lines of code in which is obtained a pessimistic lock for updating an object and after it is updated?
The main requirement is that the object come from a GUI application. This GUI creates the object and sends it to Hibernate. I need to update it in a pessimistic way (client requirements)


I need that code for my curent project, please.

Best regards,

Roque


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 9:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
get hibernate in action chapter 5
if you don't, i'll see if i can find time to help you tonight

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 9:44 am 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
Thanks Anthony.
I asked to my boss for this book, but now he's on holidays. He must to sign the order, so I must to wait


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 11:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i'm back, if you really want to obtain a pessimistic lock, then you need to:
1- begin trans
2- obtain object : Cat cat = (Cat) session.get(Cat.class,id,LockMole.UPGRADE)
...
4- Commit

you can also use session.lock(cat,LockMole.UPGRADE)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 2:50 am 
Newbie

Joined: Tue Aug 17, 2004 7:38 am
Posts: 12
Location: Z
Hello,

At the end, I understood my problem, indeed my two problems.

1.- I'm using SQL Server and therefore the
Code:
(Cat) session.get(Cat.class,id,LockMole.UPGRADE)
doesn't work because SQL Server doesn't support the "select for update" sentence.

2.- Hibernate doesn't flush the "update" operations inmediately at least you configure it "ad hoc" or you invoke explicitly session.flush(). Due to this, the database never blocked my two concurrents tx.

Anyway, thanks a lot for your help.

Best regards,

Roque


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 3:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
HIbernate3 supports pessimistic locking in SQL server, using "with (updlock, rowlock)"


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.