-->
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.  [ 2 posts ] 
Author Message
 Post subject: save without commit results in stale data
PostPosted: Thu Sep 09, 2004 6:56 am 
Newbie

Joined: Thu Sep 02, 2004 8:16 am
Posts: 4
I am adding transient objects to a session without actually committing or rolling back a transaction.
When closing the session, opening a new session and executing a find on this newly created session it returns me the non committed objects. Trying session.clear, evict or sessionFactory.evict does not resolve this behaviour. Seems like the saved but non-committed objects are somehow cached as they pop-up in a newly created session.

Is this expected behaviour?

Any enlightment on this would be appreciated.

Cheers,
Ricky


Hibernate version:
2.1.4

Mapping documents:
<?xml version="1.0" ?>
<!DOCTYPE hibernate-configuration (View Source for full doctype...)>
- <hibernate-configuration>
- <session-factory>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///fin_frame</property>
<property name="connection.username">root</property>
<property name="connection.password" />
<property name="Environment.HBM2DDL_AUTO">create</property>
<mapping resource="SystemEndpoint.hbm.xml" />
</session-factory>
</hibernate-configuration>

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

<hibernate-mapping>

<class name="com.ricky.persistence.SystemEndpoint"
table="SYSTEM_ENDPOINT" optimistic-lock="version"
dynamic-update="true">

<id name="uid" column="uid" type="long" unsaved-value="0">
<meta attribute="field-description">Unique synthetic\surrogate object id: the object's database identity which is used to distinguish two persistent objects.</meta>
<generator class="native" />
</id>

<version column="version" name="version" unsaved-value="undefined">
<meta attribute="field-description">Used for optimistic locking purposes.</meta>
<meta attribute="scope-get">private</meta>
<meta attribute="scope-set">private</meta>
</version>


<property name="code" type="string" unique="true" not-null="true">
<meta attribute="field-description">The code of the SystemEndpoint.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="name" type="string" unique="true" not-null="true">
<meta attribute="field-description">The name of the SystemEndpoint.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="description" type="string" not-null="true">
<meta attribute="field-description">The description of the SystemEndpoint.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="engineUUID" type="string" unique="true" not-null="true">
<meta attribute="field-description">A UUID generated by means of the UUIDFactory. Appears in the export XML. Might be used in the future by the loaders and engines to uniquely identify an instance.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>

</class>

<query name="com.ricky.service.SystemEndpointMgr.findByCode"><![CDATA[from com.ricky.persistence.SystemEndpoint where code = ?]]>
</query>
<query name="com.ricky.service.SystemEndpointMgr.findByName"><![CDATA[from com.ricky.persistence.SystemEndpoint where name = ?]]>
</query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

public void testSaveWithoutCommit() throws HibernateException, InterruptedException {

Session session;
SystemEndpoint systemEndPoint;
Transaction tx;
List systemEndPointList;
SessionFactory sessionFactory;

sessionFactory = new Configuration().configure().buildSessionFactory();

System.out.println("testWithoutCommit - Start");

session = sessionFactory.openSession();

System.out.println("Thread : " + Thread.currentThread().toString());
System.out.println("Session : " + session.toString());

for (int i = 0; i < 10; i++) {
systemEndPoint = new SystemEndpoint("ricky-code" + i, "ricky-desc" + i, "ricky-eng" + i, "ricky-engine-id" + i, null);
session.save(systemEndPoint);
session.evict(systemEndPoint);
}

// tx = session.beginTransaction();
// tx.commit();
// tx.rollback();

session.clear();
session.disconnect();
session.close();

sessionFactory.evict(SystemEndpoint.class);

System.out.println("testSelect - Start");

session = sessionFactory.openSession();

System.out.println("Thread : " + Thread.currentThread().toString());
System.out.println("Session : " + session.toString());

systemEndPointList = session.find("from com.ricky.persistence.SystemEndpoint");

for (Iterator iter = systemEndPointList.iterator(); iter.hasNext();) {
System.out.println(" --> " + iter.next());
}

session.close();

System.out.println("testSelect - Stop");

System.out.println("testWithoutCommit - Stop");
}

Full stack trace of any exception that occurs:


Name and version of the database you are using:
MySQL 4.20.d

Debug level Hibernate log excerpt:

output of above executed code:

log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
testWithoutCommit - Start
Thread : Thread[main,5,main]
Session : net.sf.hibernate.impl.SessionImpl@50a649
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
Hibernate: insert into SYSTEM_ENDPOINT (version, code, name, description, engineUUID) values (?, ?, ?, ?, ?)
testSelect - Start
Thread : Thread[main,5,main]
Session : net.sf.hibernate.impl.SessionImpl@12aea3e
Hibernate: select systemendp0_.uid as uid, systemendp0_.version as version, systemendp0_.code as code, systemendp0_.name as name, systemendp0_.description as descript5_, systemendp0_.engineUUID as engineUUID from SYSTEM_ENDPOINT systemendp0_
--> com.ricky.persistence.SystemEndpoint@112e7f7[uid=1,code=bbc,name=Basic Business Component,description=Description for: Basic Business Component System,engineUUID=000082b18f650f7a945a13971681094655708326.1094655709077.66]
--> com.ricky.persistence.SystemEndpoint@3b1f38[uid=2,code=coc,name=Commomn Operational Component System,description=Description for: Common Operational Component System,engineUUID=000082b18f650f7a945a13971681094655708326.1094655709097.76]
--> com.ricky.persistence.SystemEndpoint@17ec9f7[uid=93,code=ricky-code0,name=ricky-desc0,description=ricky-eng0,engineUUID=ricky-engine-id0]
--> com.ricky.persistence.SystemEndpoint@fd918a[uid=94,code=ricky-code1,name=ricky-desc1,description=ricky-eng1,engineUUID=ricky-engine-id1]
--> com.ricky.persistence.SystemEndpoint@15e2075[uid=95,code=ricky-code2,name=ricky-desc2,description=ricky-eng2,engineUUID=ricky-engine-id2]
--> com.ricky.persistence.SystemEndpoint@787c16[uid=96,code=ricky-code3,name=ricky-desc3,description=ricky-eng3,engineUUID=ricky-engine-id3]
--> com.ricky.persistence.SystemEndpoint@1a9d1b[uid=97,code=ricky-code4,name=ricky-desc4,description=ricky-eng4,engineUUID=ricky-engine-id4]
--> com.ricky.persistence.SystemEndpoint@140984b[uid=98,code=ricky-code5,name=ricky-desc5,description=ricky-eng5,engineUUID=ricky-engine-id5]
--> com.ricky.persistence.SystemEndpoint@1ce669e[uid=99,code=ricky-code6,name=ricky-desc6,description=ricky-eng6,engineUUID=ricky-engine-id6]
--> com.ricky.persistence.SystemEndpoint@1b33a0e[uid=100,code=ricky-code7,name=ricky-desc7,description=ricky-eng7,engineUUID=ricky-engine-id7]
--> com.ricky.persistence.SystemEndpoint@1a8773c[uid=101,code=ricky-code8,name=ricky-desc8,description=ricky-eng8,engineUUID=ricky-engine-id8]
--> com.ricky.persistence.SystemEndpoint@12c3327[uid=102,code=ricky-code9,name=ricky-desc9,description=ricky-eng9,engineUUID=ricky-engine-id9]
testSelect - Stop
testWithoutCommit - Stop


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 8:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:

I am adding transient objects to a session without actually committing or rolling back a transaction.



don't do that.... what for? the problem is of course here.

you have to understand how session, jdbc transaction and connection are linked...

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


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