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]