Return-Path: X-Original-To: apmail-openjpa-users-archive@minotaur.apache.org Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A9969C04C for ; Tue, 10 Jul 2012 17:20:53 +0000 (UTC) Received: (qmail 2591 invoked by uid 500); 10 Jul 2012 17:20:53 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 2557 invoked by uid 500); 10 Jul 2012 17:20:53 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 2549 invoked by uid 99); 10 Jul 2012 17:20:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Jul 2012 17:20:53 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of mailto.woden@gmail.com designates 74.125.82.52 as permitted sender) Received: from [74.125.82.52] (HELO mail-wg0-f52.google.com) (74.125.82.52) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Jul 2012 17:20:47 +0000 Received: by wgbgn7 with SMTP id gn7so205723wgb.9 for ; Tue, 10 Jul 2012 10:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=JfbIIu9ZeTBMy4AaI6c03UKEX74eE/xrmDsWPWUzr80=; b=wNK+09bK/f5AEWRO4X04LII1WAPPh1hEj85hqiTIYPtMivwaujEnUnl8q5oGuNBXJh BSTMmw9ZvQqf9fOzMyt26niEnjhx6ZxGwChj74jczHRuMyv51/pp805SB24iE3LH5Y/c Ch1W3s81enxi1F15lRCCZhdUP769awsU8gKCmSc+yHTo2Lz6Bl6/ZzHGBd5UPNoULbWP yl4fhUBtEkjhX+lTI6nVj1Y+/mdO+GybG22JEMgEBeLHi/nBS9bEq4sJBj2zzsZJh2y1 vYVgZOGNYRVBbpMggQd0rWcpQmFPJP2Fspn4M26uBl8R1jr+0e7VKdHXndPSF1zeaqBZ Pb0w== MIME-Version: 1.0 Received: by 10.216.136.227 with SMTP id w77mr8706736wei.111.1341940825251; Tue, 10 Jul 2012 10:20:25 -0700 (PDT) Received: by 10.216.237.136 with HTTP; Tue, 10 Jul 2012 10:20:24 -0700 (PDT) Received: by 10.216.237.136 with HTTP; Tue, 10 Jul 2012 10:20:24 -0700 (PDT) In-Reply-To: References: Date: Tue, 10 Jul 2012 19:20:24 +0200 Message-ID: Subject: Re: Thread writing. From: Daniel Persson To: users@openjpa.apache.org Content-Type: multipart/alternative; boundary=0016e6da7ae132e18904c47cf26b X-Virus-Checked: Checked by ClamAV on apache.org --0016e6da7ae132e18904c47cf26b Content-Type: text/plain; charset=UTF-8 Yes, thats what I'm experience. I want the last update though. Thinking about implementing it inside transactions to isolate the writing. On Jul 10, 2012 6:31 PM, "Rick Curtis" wrote: > Is this your scenario? > > T1: Find id=1, returns null > T2: Find id=1, returns null > > T1: merge(id=1) > T2: merge(id=1) > > T1: tran commit > T2: tran commit, EntityExistsException > > If so, it sounds like you need to add some exception handling? > > > On Tue, Jul 10, 2012 at 11:04 AM, Daniel Persson >wrote: > > > Correct but I'm merging entities. And if the find returns null I'll > create > > an new entity. > > On Jul 10, 2012 5:11 PM, "Rick Curtis" wrote: > > > > > > I have a problem with one of our entities that we write in a callable > > > thread that could be called multiple times simultaneously. > > > If I'm reading this correctly, you're trying to persist an Entity with > > the > > > same ID multiple times simultaneously? If that is the case, this > > exception > > > is expected. > > > > > > On Tue, Jul 10, 2012 at 10:00 AM, Daniel Persson < > mailto.woden@gmail.com > > > >wrote: > > > > > > > Hi OpenJPA users. > > > > > > > > I have a problem with one of our entities that we write in a callable > > > > thread that could be called multiple times simultaneously. > > > > > > > > We have an ThreadPoolExecutor that runs multiple versions of an > object > > > > implements Callable. > > > > In some of these objects an entity is fetched and merged. > > > > > > > > We seperate each thread using ThreadLocal so each thread has there > own > > > > entityManager and it uses it during the thread execution. > > > > private static transient ThreadLocal EntityManager>> > > > > entityManagers = new ThreadLocal>(); > > > > > > > > No entities is sent between threads. > > > > > > > > The error we get frequently: > > > > > > > > Caused by: error> > > > > org.apache.openjpa.persistence.EntityExistsException: Duplicate entry > > > > '472307' for key 1 {prepstmnt 37611 INSERT INTO table(col1, col2, > > > > col3, col4) VALUES (?, ?, ?, ?)} [code=1062, state=23000] > > > > FailedObject: path.to.table@3725f1 > > > > at > > > > > org.apache.openjpa.jdbc.sql.DBDictionary.narrow(DBDictionary.java:4926) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:4901) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:136) > > > > at > > > > > > org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:78) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:143) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:99) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:87) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flushPrimaryRow(OperationOrderUpdateManager.java:203) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flush(OperationOrderUpdateManager.java:89) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:105) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:78) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:738) > > > > at > > > > > > > > > > org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:131) > > > > at > > > > > > > > > > org.apache.openjpa.datacache.DataCacheStoreManager.flush(DataCacheStoreManager.java:661) > > > > at > > > > > > > > > > org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:131) > > > > ... 19 more > > > > Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: > > > > Duplicate entry '472307' for key 1 {prepstmnt 37611 INSERT INTO > > > > table(col1, col2, col3, col4) VALUES (?, ?, ?, ?)} [code=1062, > > > > state=23000] > > > > at > > > > > > > > > > org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:219) > > > > at > > > > > > > > > > org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:195) > > > > at > > > > > > > > > > org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$1000(LoggingConnectionDecorator.java:59) > > > > at > > > > > > > > > > org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:1134) > > > > at > > > > > > > > > > org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:272) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1768) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.executeUpdate(PreparedStatementManagerImpl.java:267) > > > > at > > > > > > > > > > org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:118) > > > > ... 29 more > > > > > > > > Our persistance configuration using an non-jta-data-source and tomcat > > > > datasource pooling. > > > > > > > > > > > value="com.mysql.jdbc.Driver" /> > > > > > > > value="autoReconnect=false,connectTimeout=5,socketTimeout=5" /> > > > > > > > value="mysql(SupportsSubselect=true,UseClobs=true)"/> > > > > > > > > > > > > > > > > > > > > > > > value="fetch-groups(DetachedStateField=true)"/> > > > > > > > > > > > > > > > > Any suggestions how to proceed? > > > > My guess is that one thread writes an entity before the another > thread > > > > and the EntityManager entity state isn't updated before the next > > > > merge. > > > > > > > > Best regards > > > > > > > > Daniel > > > > > > > > > > > > > > > > -- > > > *Rick Curtis* > > > > > > > > > -- > *Rick Curtis* > --0016e6da7ae132e18904c47cf26b--