-->
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.  [ 3 posts ] 
Author Message
 Post subject: Repeatedly load & saving the same persisted object
PostPosted: Tue Apr 12, 2005 12:37 pm 
Newbie

Joined: Tue Nov 16, 2004 11:22 am
Posts: 6
I am trying to write a simple incremental counter:

I have a very simple object that contains just an int for the database ID and a long for the counter. It has a getNext() method that increments the int and returns it's value.

I also have an EJB (Stateless session bean) that:
- loads this simple object from the database,
- calls the getNext() method,
- saves the object back to the database,
- and returns the counter value.

When I run a single instance of a client program to call the EJB repeatedly I get the predicted output:
1,2,3,4,5,6,7,8,9,10
and so on, with the last value always being stored in the database.

If I run two instances of the client program I expect each client to recieve different values from the EJB, but occasionally both clients receive the same value!

E.G. (Client1 and Client2 are running at the same time.)

Client1
1
3
5
6
9
12

Client 2
2
4
7
8
9 <---- Wrong!
11
13

Does anybody have any ideas why this is happening?
(Even if you can help narrow it to a problem in my EJB or Hibernate code would be great.)


Hibernate version: 2.17

Mapping documents:
<hibernate-mapping>
<class
name="com.test.Counter"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version">

<id name="id"
column="ID"
type="java.lang.Long">
<generator class="native"></generator>
</id>

<property
name="counter"
type="long"
update="true"
insert="true"
access="property"
column="counter"/>
</class>
</hibernate-mapping>


Name and version of the database you are using: MySQL 4.1.7

The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 2:37 pm 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
Quote:
optimistic-lock="version"

You are trying to use optimistic locking with versioning without having any versioning column. Either add a version column or use pessimistic locking.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 6:57 am 
Newbie

Joined: Tue Nov 16, 2004 11:22 am
Posts: 6
Many thanks, I added a version column and it all works fine now.


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