You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Michael Gentry <mg...@masslight.net> on 2010/11/05 13:57:08 UTC

To Dep PK bug?

I kept getting an optimistic locking exception where my PK got nulled
out, so I set up a simple test case:

User Table:
id bigint
name varchar(50)
username varchar(20)

Avatar Table:
avatarImage blob(100000)
id biginit

User is the master table, Avatar the dependent, To Dep PK checked
going from User to Avatar to map the User PK to the Avatar.

Code:


        DataContext dataContext = DataContext.createDataContext();

        User admin = dataContext.newObject(User.class);

        admin.setName("Administrator");
        admin.setUsername("admin");

        dataContext.commitChanges();

        admin.setName("The Administrator");

        dataContext.commitChanges();

The second commitChanges fails with the following SQL:


INFO: INSERT INTO Users (id, name, username) VALUES (?, ?, ?)
INFO: [bind: 1->id:200, 2->name:'Administrator', 3->username:'admin']
INFO: UPDATE Users SET name = ? WHERE id IS NULL AND name = ? AND username = ?
INFO: [bind: 1->name:'The Administrator', 2->id:NULL,
3->name:'Administrator', 4->username:'admin']
org.apache.cayenne.access.OptimisticLockException: [v.3.0 Apr 30 2010
11:29:33] Optimistic Lock Failure, SQL: [UPDATE Users SET name = ?
WHERE id IS NULL AND name = ? AND username = ?], WHERE clause
bindings: [id=NULL, username='admin', name='Administrator']

The ID gets set to null which is clearly wrong.

mrg

Re: To Dep PK bug?

Posted by Michael Gentry <mg...@masslight.net>.
https://issues.apache.org/jira/browse/CAY-1507

I attached an Eclipse project illustrating the bug, too.  Also, as I
noted in the bug description, I had to have two relationships off the
master User record (Avatar and Role) in order to trip the bug.  When I
scaled down to just have User/Avatar, the bug went away.

I'll try to look into it more, but another set of eyes would
definitely be welcome.

Thanks,

mrg


On Mon, Nov 8, 2010 at 8:44 AM, Michael Gentry <mg...@masslight.net> wrote:
> If I turn optimistic locking off, it works as expected:
>
> INFO: UPDATE Users SET name = ? WHERE id = ?
> INFO: [bind: 1->name:'The Administrator', 2->id:200]
>
> I did a little digging on Friday and it looks like the snapshot
> doesn't contain the ID after the first commit which is what is causing
> the "WHERE id IS NULL" to occur on the second commit (I think).
>
> mrg
>
>
> On Sun, Nov 7, 2010 at 7:21 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>> Very odd. Please file a Jira. Maybe we can reproduce it with our unit tests? So what is optimistically locked in this case?
>>
>> Andrus
>

Re: To Dep PK bug?

Posted by Michael Gentry <mg...@masslight.net>.
If I turn optimistic locking off, it works as expected:

INFO: UPDATE Users SET name = ? WHERE id = ?
INFO: [bind: 1->name:'The Administrator', 2->id:200]

I did a little digging on Friday and it looks like the snapshot
doesn't contain the ID after the first commit which is what is causing
the "WHERE id IS NULL" to occur on the second commit (I think).

mrg


On Sun, Nov 7, 2010 at 7:21 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> Very odd. Please file a Jira. Maybe we can reproduce it with our unit tests? So what is optimistically locked in this case?
>
> Andrus

Re: To Dep PK bug?

Posted by Andrus Adamchik <an...@objectstyle.org>.
Very odd. Please file a Jira. Maybe we can reproduce it with our unit tests? So what is optimistically locked in this case?

Andrus

On Nov 5, 2010, at 9:30 AM, Michael Gentry wrote:

> It even fails if I include the dependent:
> 
> 
>        DataContext dataContext = DataContext.createDataContext();
> 
>        User admin = dataContext.newObject(User.class);
> 
>        admin.setName("Administrator");
>        admin.setUsername("admin");
> 
>        Avatar avatar = dataContext.newObject(Avatar.class);
>        admin.setAvatar(avatar);
> 
>        dataContext.commitChanges();
> 
>        admin.setName("The Administrator");
> 
>        dataContext.commitChanges();
> 
> 
> Same issue.  Both records get inserted this time (ID = 200).  Then on
> the second commit the ID goes to null in the master (User) record.
> 
> mrg
> 


Re: To Dep PK bug?

Posted by Michael Gentry <mg...@masslight.net>.
It even fails if I include the dependent:


        DataContext dataContext = DataContext.createDataContext();

        User admin = dataContext.newObject(User.class);

        admin.setName("Administrator");
        admin.setUsername("admin");

        Avatar avatar = dataContext.newObject(Avatar.class);
        admin.setAvatar(avatar);

        dataContext.commitChanges();

        admin.setName("The Administrator");

        dataContext.commitChanges();


Same issue.  Both records get inserted this time (ID = 200).  Then on
the second commit the ID goes to null in the master (User) record.

mrg