BigDecimal WOes
BigDecimal WOes
- Subject: BigDecimal WOes
- From: Neil MacLennan <email@hidden>
- Date: Sun, 23 Nov 2008 02:44:29 +0000
I'm having trouble with BigDecimal, WO and my database. You know how
some people have trouble with dates amd time zones? Well I've never
quite mastered the finer idiosyncrasies of floating point numbers in
Java.
I have an EO attribute, 'amount' -- a currency value you'll not be
surprised to hear. It's decimal field in my database (MySQL) and a
BigDecimal in my model.
I'm aware of "issues" with BigDecimal when you use the
BigDecimal(double) contructor and so I've been using the better
BigDecimal(String) constructor.
When I first insert my EO (containing "amount") and saveChanges -- it
works fine. The value in the DB is as expected, say, 16.16,
representing 16 pounds and 16 pence.
When I next come to update that row I get a JDBC error and an
EOGeneralAdaptorException failure.
I turned on logging in MySQL (Wonder SQL logging wasn't too helpful)
to see what was happening: the UPDATE transaction is being rolled back
because the row could not be sucessfully updated. Here's why:
[MySQL Log]
2 Prepare [4] UPDATE OrderTicket SET paymentStatus = ? WHERE
(ticketID = ? AND amount = ? AND orderID = ? AND paymentStatus = ? AND
paymentTimestamp is NULL AND shopID = ?)
2 Execute [4] UPDATE OrderTicket SET paymentStatus = 'Authorising
with Provider...' WHERE (ticketID = 5824 AND amount = 16.159999847412
AND orderID = 127 AND paymentStatus = 'Taking Card Details...' AND
paymentTimestamp is NULL AND shopID = 3)
2 Query rollback
[END]
For some reason, WO is using this imprecise floating value
(16.159999847412) in the DB lock to UPDATE against. But I'm stumped
where it's getting this from. Is it reading it back in again from the
DB at some point and because the database has the "amount" field as a
decimal it's using the BigDecimal(double) contructor? Should I only
use string (varchar/char) db fields for use with BigDecimal?
I've "fixed" the issue by not having WO lock on the amount attribute
in my model, but that doesn't fix the underlying problem: it just
avoids it. What's the real answer?
Thx
.neilmac
WO5.3/Wonder 5.3/OSX 10.5.5
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden