Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id AE969633ACB for ; Fri, 20 Mar 2009 15:02:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 42094-04 for ; Fri, 20 Mar 2009 15:02:47 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f115.google.com (mail-qy0-f115.google.com [209.85.221.115]) by mail.postgresql.org (Postfix) with ESMTP id 8AA866326B1 for ; Fri, 20 Mar 2009 15:02:45 -0300 (ADT) Received: by qyk13 with SMTP id 13so1374126qyk.15 for ; Fri, 20 Mar 2009 11:02:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type; bh=72xFo1xLaROGrFwBpY/hOts66FLd41byqb9V+RM2oKM=; b=qTT7XSpOZ6/cPF8YGLPZe3lYecK3qAKVx8Mnx4C5F/wNrE8UOJuCuTihWjxRvqp5lq hx37n+WovbtNVGkI08Wd16OAX69RiddWelw3WBkRECAS8I2Uf58DO0m3qZzPlY7P68He LwU/H2p3fgqZAzWKu6FfJC8lxr+pSbKSbTxl8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=sM9aenzYeEsehHBebbW5L9BuJeBhkrNmC+GiKB7Ap+1kEj2B+RKcIbrpijgtEGHDkf N2jdBPsEjQVvQfGt3g36E+Q0Ckb+i35QO3Qhd3GCFcPeZ9p4iS77GztHeEBNzsAnjPgF N2LP97EZhoIRGAjUXKzkPx3PmRBeVSMd0735k= MIME-Version: 1.0 Received: by 10.229.74.12 with SMTP id s12mr2279084qcj.27.1237572158318; Fri, 20 Mar 2009 11:02:38 -0700 (PDT) In-Reply-To: <49C3C5FB.20706@astrofoto.org> References: <49C3C5FB.20706@astrofoto.org> Date: Fri, 20 Mar 2009 14:02:37 -0400 X-Google-Sender-Auth: 5fd59e727f6fa980 Message-ID: <491f66a50903201102u22f2e81ah2383ff46a1cf6ff4@mail.gmail.com> Subject: Re: Hibernate, JBoss, PostgreSQL, timestamp read doesn't match during update From: Dave Cramer To: Roland Roberts Cc: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=00163646bfcce23691046590b80c X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.079 tagged_above=0 required=5 tests=AWL=0.078, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200903/98 X-Sequence-Number: 19428 --00163646bfcce23691046590b80c Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit You should not use a trigger to generate the version info for hibernate. You should use the application's time. Dave On Fri, Mar 20, 2009 at 12:36 PM, Roland Roberts wrote: > I have no idea where to point the blame on this one. I have a workaround, > but here's the scenario. > > I have a timestamp column which I want to use in JBoss/Hibernate as a > "version" column so that Hibernate can do opportunistic locking for updates. > The timestamp is actually generated by a trigger on the table. Here's an > edited down copy of the DDL for the table and trigger: > > CREATE TABLE security ( > id SERIAL, > primary_identifier VARCHAR(10) NOT NULL, > ... > active_on TIMESTAMP(3), > ); > > DROP FUNCTION security_biur_trg() CASCADE; > > CREATE FUNCTION security_biur_trg() RETURNS TRIGGER AS $$ > BEGIN > NEW.active_on := CURRENT_TIMESTAMP AT TIME ZONE 'UTC'; > RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > CREATE TRIGGER security_biur BEFORE INSERT OR UPDATE > ON security FOR EACH ROW > EXECUTE PROCEDURE security_biur_trg(); > > I have a custom UserVersionType for Hibernate to allow it to treat the > column as a version object. You can see the full post of my code at > http://www.hibernate.org/461.html. The code does work with one caveat > that I will come to shortly. > > The Hibernate mapping file for this table is > > "-//Hibernate/Hibernate Mapping DTD 3.0//EN" > "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> > > > > > > security_id_seq > > > > column="primary_identifier" > type="string"/> > > column="active_on" > type="HibernateUTC$TimestampType" > generated="always" > insert="false" > unsaved-value="null"/> > ... > > > > What happens when I actually try to update a row is described in more > detail here, http://forum.hibernate.org/viewtopic.php?p=2409286#2409286, > but the short answer is that Hibernate thinks the row has been updated by > another transaction. After trying all sorts of different mapping > definitions for the active_on column (including letting Hibernate generate > the values, which sort-of works), I finally hit upon a simple column > definition change that works around the problem: > > active_on timestamp(3) > > Yes, restrict the timestamp to millisecond precision. Somewhere the > sub-millisecond parts are getting lost. I have no idea if it is in the JDBC > layer or somewhere in Hibernate. Any clues on figuring this out? > > Oh, the SQL generated by Hibernate puts both the primary key and the > active_on column in the "where" clause. This is what it is supposed to do > and how it detects a row as having been updated by another transaction. In > this case, the precision mismatch fools it. > > roland > > -- > PGP Key ID: 66 BC 3B CD > Roland B. Roberts, PhD RL Enterprises > roland@rlenter.com 6818 Madeline Court > roland@astrofoto.org Brooklyn, NY 11220 > > > -- > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-jdbc > --00163646bfcce23691046590b80c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable You should not use a trigger to generate the version info for hibernate. Yo= u should use the application's time.

Dave


On Fri, Mar 20, 2009 at 12:36 PM, Roland Roberts <roland@astrofoto.org> wrote:
I have no idea wh= ere to point the blame on this one. =A0I have a workaround, but here's = the scenario.

I have a timestamp column which I want to use in JBoss/Hibernate as a "= ;version" column so that Hibernate can do opportunistic locking for up= dates. =A0The timestamp is actually generated by a trigger on the table. = =A0Here's an edited down copy of the DDL for the table and trigger:

=A0CREATE TABLE security (
=A0 id =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SERIAL,
=A0 primary_identifier =A0 =A0 =A0 =A0VARCHAR(10) NOT NULL,
=A0 ...
=A0 active_on =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TIMESTAMP(3),
=A0);

=A0DROP FUNCTION security_biur_trg() CASCADE;

=A0CREATE FUNCTION security_biur_trg() RETURNS TRIGGER AS $$
=A0 =A0 BEGIN
=A0 =A0 =A0 =A0 NEW.active_on :=3D CURRENT_TIMESTAMP AT TIME ZONE 'UTC= ';
=A0 =A0 =A0 =A0 RETURN NEW;
=A0 =A0 END;
=A0$$ LANGUAGE plpgsql;

=A0CREATE TRIGGER security_biur BEFORE INSERT OR UPDATE
=A0 =A0 ON security FOR EACH ROW
=A0 =A0 EXECUTE PROCEDURE security_biur_trg();

I have a custom UserVersionType for Hibernate to allow it to treat the colu= mn as a version object. =A0You can see the full post of my code at
http://www.hiberna= te.org/461.html. =A0The code does work with one caveat that I will come= to shortly.

The Hibernate mapping file for this table is
=A0<?xml version=3D"1.0"?>
=A0<!DOCTYPE hibernate-mapping PUBLIC
=A0 =A0 =A0 =A0 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
=A0 =A0 =A0 =A0 "http://hibernate.sourceforge.net/hiber= nate-mapping-3.0.dtd">

=A0<hibernate-mapping>
=A0 <class name=3D"Security">
=A0 =A0 <id name=3D"securityId" column=3D"id" type= =3D"long" unsaved-value=3D"null">
=A0 =A0 =A0 <generator class=3D"sequence">
=A0 =A0 =A0 =A0 <param name=3D"sequence">security_id_seq&l= t;/param>
=A0 =A0 =A0 </generator>
=A0 =A0 </id>
=A0 =A0 <natural-id>
=A0 =A0 =A0 <property name=3D"primaryIdentifier"
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 column=3D"primary_identifier" =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 type=3D"string"/>
=A0 =A0 </natural-id>
=A0 =A0 <version name=3D"activeOn"
=A0 =A0 =A0 =A0 =A0 =A0 =A0column=3D"active_on"
=A0 =A0 =A0 =A0 =A0 =A0 =A0type=3D"HibernateUTC$TimestampType" =A0 =A0 =A0 =A0 =A0 =A0 =A0generated=3D"always"
=A0 =A0 =A0 =A0 =A0 =A0 =A0insert=3D"false"
=A0 =A0 =A0 =A0 =A0 =A0 =A0unsaved-value=3D"null"/>
=A0 =A0 ...
=A0 </class>
=A0</hibernate-mapping>

What happens when I actually try to update a row is described in more detai= l here, http://forum.hibernate.org/viewtopic.php?p=3D240928= 6#2409286, but the short answer is that Hibernate thinks the row has be= en updated by another transaction. =A0After trying all sorts of different m= apping definitions for the active_on column (including letting Hibernate ge= nerate the values, which sort-of works), I finally hit upon a simple column= definition change that works around the problem:

=A0active_on timestamp(3)

Yes, restrict the timestamp to millisecond precision. =A0Somewhere the sub-= millisecond parts are getting lost. =A0I have no idea if it is in the JDBC = layer or somewhere in Hibernate. =A0Any clues on figuring this out?

Oh, the SQL generated by Hibernate puts both the primary key and the active= _on column in the "where" clause. =A0This is what it is supposed = to do and how it detects a row as having been updated by another transactio= n. =A0In this case, the precision mismatch fools it.

roland

--
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 RL Enterprises
roland@rlenter.com<= /a> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A06818 Madeline Co= urt
roland@astrofoto.= org =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Brooklyn, NY 11= 220


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

--00163646bfcce23691046590b80c--