Re: Merge Detached Entity Problem
Hi Roger, I don't know anything about your project or environment, but I've been doing some testing with PostgreSQL today and I'm finding it seems to work much better with OpenJPA than MySQL. I've recently come out of the corporate world, and Oracle was our "standard" database platform. I've just started working with open source db platforms, and I'm finding MySQL has some limitations that might prove problematic when using OpenJPA and programming robust database-backed web applications. I know it's a supported platform, but I'd be willing to bet there are a lot more caveats (and remaining bugs) when using MySQL as opposed to Oracle or PostgreSQL. I'm sure some of the experts on this list can speak to this point, but MySQL apparently does not support deferred foreign key constraints. In my particular use case, I was trying to merge a detached object that contained a reference to another detached object. The cascade update seemed to succeed only if both detached objects contained changes. Adding the @Version annotation and columns to my tables fixed the problem, but then I ran into an intermittent 'Optimistic Lock' exception when attempting to remove the same type of detached object. It appeared to be a race condition, as sometimes the delete would succeed, and other times it would fail. I'm speculating that this occured because I had cascade delete enabled on my foreign key constraint and CascadeType.ALL enabled for my referenced object. Since I was using MySQL, the cascade delete on the foreign key constraint couldn't be deferred until transaction commit. It would sometimes execute prior to OpenJPA execution of the 2nd delete statement. I believe that's where the (legitimate) intermittent optimistic lock exception was coming from for this removal use case. I've just finished setting up my database, schema, and tables on PostgreSQL 8.2.4. I'm finding that with deferred foreign key constraints enabled, my cascading issues seem to have disappeared. As an added bonus I was able to revert to the initial code I created for my DAOs because JPA was working without any additional (and seemingly unnecessary) manipulation of my referenced objects. If alternate database platforms are an option, I'd recommend looking at PostgreSQL. Support for deferred constraints, plus better handling of BLOBs and full support for multi-version concurrency control (MVCC). Cheers, -jmh On 4/29/07, roger.keays <[EMAIL PROTECTED]> wrote: Jason Hanna wrote: > > org.apache.openjpa.persistence.RollbackException: Optimistic locking > errors were detected when flushing to the data store. The following > objects may have been concurrently modified in another transaction: > [com.coincident.green.beans.User-com.coincident.green.beans.User-1] > I've just fixed a similar issue in my app [1]. Maybe it is the same problem with foreign key constraints [2] [1] http://www.nabble.com/forum/ViewPost.jtp?post=10247539&framed=y [2] http://www.nabble.com/forum/ViewPost.jtp?post=7395124&framed=y -- View this message in context: http://www.nabble.com/Merge-Detached-Entity-Problem-tf3658825.html#a10247785 Sent from the open-jpa-dev mailing list archive at Nabble.com.
Re: Merge Detached Entity Problem
An update - I was not using the @Version annotation in my item definition. After updating my class with the annotation and adding additional database column, my original reported problem disappears. My first read of the documentation led me to believe the @Version annotation was a best practice, but not necessary. From the OpenJPA User's Guide, Section 1.4: ---start Note OpenJPA fully supports version fields, but does not require them for concurrency detection. OpenJPA can maintain surrogate version values or use state comparisons to detect concurrent modifications. See Section 7, " Additional JPA Mappings " in the Reference Guide. ---end I seem to have resolved my immediate issue, but perhaps someone can set me straight on the expected behavior when the @Version annotation is not present. Thanks, -jmh On 4/27/07, Jason Hanna <[EMAIL PROTECTED]> wrote: Hi All, I'm running into a problem developing a web application that utilizes JSF and JPA. Not sure if my problem is related to an OpenJPA bug, or a flaw in my approach. I'm hoping you can help. In my web application, a user selects an item to "edit" from a table of all available items. They are then directed to a page containing a form which allows them to modify the details of the item they selected (pretty standard web app stuff). Upon submission, the backing bean connected to this form invokes the update method of the item's data access object. The DAO in turn creates and EntityManager from the EntityManagerFactory, starts a transaction, merges the detached object, and commits the transaction. If any value is modified on the item edit form, the detached object is merged and successfully committed. That's good. Now for the problem: If the detached object is NOT modifed (edit form submitted w/o any changes) the merge will fail on the EntityManager transaction commit with the following error: <2|true|0.9.5-incubating> org.apache.openjpa.persistence.RollbackException: Optimistic locking errors were detected when flushing to the data store. The following objects may have been concurrently modified in another transaction: [com.coincident.green.beans.User-com.coincident.green.beans.User-1] I've read through the lifecycle management documentation and don't see why attempting to merge an unchanged detached entity would result in an exception. I could probably detect that no updates were made at my JSF managed bean and avoid the merge operation altogether, but what happens when my item references other managed entities and I want to utilize cascade merge? Hoping you all might have some thoughts. Here are a few details about my environment: Sun JDK 1.5.0_06 Apache Tomcat 5.5.20 MyFaces 1.1.3 OpenJPA 0.9.5 (Packaged w/BEA Workship Studio and Kodo) MySQL 5.0.27 (Community) Thanks! Please let me know if you require any more information. Regards, -jmh Jason M. Hanna President/Founder Coincident, Inc.
Merge Detached Entity Problem
Hi All, I'm running into a problem developing a web application that utilizes JSF and JPA. Not sure if my problem is related to an OpenJPA bug, or a flaw in my approach. I'm hoping you can help. In my web application, a user selects an item to "edit" from a table of all available items. They are then directed to a page containing a form which allows them to modify the details of the item they selected (pretty standard web app stuff). Upon submission, the backing bean connected to this form invokes the update method of the item's data access object. The DAO in turn creates and EntityManager from the EntityManagerFactory, starts a transaction, merges the detached object, and commits the transaction. If any value is modified on the item edit form, the detached object is merged and successfully committed. That's good. Now for the problem: If the detached object is NOT modifed (edit form submitted w/o any changes) the merge will fail on the EntityManager transaction commit with the following error: <2|true|0.9.5-incubating> org.apache.openjpa.persistence.RollbackException: Optimistic locking errors were detected when flushing to the data store. The following objects may have been concurrently modified in another transaction: [com.coincident.green.beans.User-com.coincident.green.beans.User-1] I've read through the lifecycle management documentation and don't see why attempting to merge an unchanged detached entity would result in an exception. I could probably detect that no updates were made at my JSF managed bean and avoid the merge operation altogether, but what happens when my item references other managed entities and I want to utilize cascade merge? Hoping you all might have some thoughts. Here are a few details about my environment: Sun JDK 1.5.0_06 Apache Tomcat 5.5.20 MyFaces 1.1.3 OpenJPA 0.9.5 (Packaged w/BEA Workship Studio and Kodo) MySQL 5.0.27 (Community) Thanks! Please let me know if you require any more information. Regards, -jmh Jason M. Hanna President/Founder Coincident, Inc.