Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

Yun Jie Zhou
Hello Andreas,
 
I was intent to try/catch the exception with the following code, but the control flow didn't reach the catch block.
Haven't figured out the reason.
 
public SvcDump updateSvcDump(final SvcDump svcDump)
  {
    SvcDump updated = null;
    try
    {
      updated = svcDumpDao.update(svcDump);
    }
    catch (final Exception e)
    {
      LOG.info("Cause: " + e.getCause());
      if (e.getCause() instanceof javax.persistence.OptimisticLockException)
      {
        // retry persist
        // first reload, and then copy
        final SvcDump reloaded = svcDumpDao.findBy(...)
        reloaded.setXXX(svcDump.getXXX());
        return svcDumpDao.update(reloaded);
      }
    }
    return updated;
  }
Thanks & Best Regards
 
 
----- Original message -----
From: Andreas Joseph Krogh <[hidden email]>
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to deal with javax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 7:12 PM
 
På fredag 20. oktober 2017 kl. 05:54:38, skrev Yun Jie Zhou <[hidden email]>:
Hello experts,
 
We got some OptimisticLockException in updating operations. Because it is Unchecked Exception, we cannot use try/catch with update operations. I searched the archived mail list, but didn't figure out a solution. Any best practice to add retry logic for concurrent access?
 
Best practice is try/catch. What's preventing you from catching OptimisticLockException?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
 


_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

Andrei Ilitchev

May be OptimisticLockException is wrapped by javax.persistence.PersistenceException?

On 2017-10-20 10:32 AM, Yun Jie Zhou wrote:
Hello Andreas,
 
I was intent to try/catch the exception with the following code, but the control flow didn't reach the catch block.
Haven't figured out the reason.
 
public SvcDump updateSvcDump(final SvcDump svcDump)
  {
    SvcDump updated = null;
    try
    {
      updated = svcDumpDao.update(svcDump);
    }
    catch (final Exception e)
    {
      LOG.info("Cause: " + e.getCause());
      if (e.getCause() instanceof javax.persistence.OptimisticLockException)
      {
        // retry persist
        // first reload, and then copy
        final SvcDump reloaded = svcDumpDao.findBy(...)
        reloaded.setXXX(svcDump.getXXX());
        return svcDumpDao.update(reloaded);
      }
    }
    return updated;
  }
Thanks & Best Regards
 
 
----- Original message -----
From: Andreas Joseph Krogh [hidden email]
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to deal with javax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 7:12 PM
 
På fredag 20. oktober 2017 kl. 05:54:38, skrev Yun Jie Zhou <[hidden email]>:
Hello experts,
 
We got some OptimisticLockException in updating operations. Because it is Unchecked Exception, we cannot use try/catch with update operations. I searched the archived mail list, but didn't figure out a solution. Any best practice to add retry logic for concurrent access?
 
Best practice is try/catch. What's preventing you from catching OptimisticLockException?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
 



_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_eclipselink-2Dusers&d=DwICAg&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=GQtGouAnNB1NfXPRNVlsqjITIeGGPPcRPagQWKnQ6PM&m=jAy9gByyYIXhBjOLPuEXDzueyOhwQDJ8OiIN27TdwR4&s=YlMfEWIE1we190yMvmtc0IBD2a4PlX0ZswTB_i7Id-g&e= 


_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

christopher delahunt
Merge and persist do not issue SQL statements immediately, this only occurs when the context is synchronized with the database which occurs on Flush or commit. If you want to be able to catch the exception when it is a container managed transaction, you will have to call em.flush in a try/catch block.

Best Regards,
Chris

On Oct 20, 2017, at 10:54 AM, Andrei Ilitchev <[hidden email]> wrote:

May be OptimisticLockException is wrapped by javax.persistence.PersistenceException?

On 2017-10-20 10:32 AM, Yun Jie Zhou wrote:
Hello Andreas,
 
I was intent to try/catch the exception with the following code, but the control flow didn't reach the catch block.
Haven't figured out the reason.
 
public SvcDump updateSvcDump(final SvcDump svcDump)
  {
    SvcDump updated = null;
    try
    {
      updated = svcDumpDao.update(svcDump);
    }
    catch (final Exception e)
    {
      LOG.info("Cause: " + e.getCause());
      if (e.getCause() instanceof javax.persistence.OptimisticLockException)
      {
        // retry persist
        // first reload, and then copy
        final SvcDump reloaded = svcDumpDao.findBy(...)
        reloaded.setXXX(svcDump.getXXX());
        return svcDumpDao.update(reloaded);
      }
    }
    return updated;
  }
Thanks & Best Regards
 
 
----- Original message -----
From: Andreas Joseph Krogh [hidden email]
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to deal with javax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 7:12 PM
 
På fredag 20. oktober 2017 kl. 05:54:38, skrev Yun Jie Zhou <[hidden email]>:
Hello experts,
 
We got some OptimisticLockException in updating operations. Because it is Unchecked Exception, we cannot use try/catch with update operations. I searched the archived mail list, but didn't figure out a solution. Any best practice to add retry logic for concurrent access?
 
Best practice is try/catch. What's preventing you from catching OptimisticLockException?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
 



_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_eclipselink-2Dusers&d=DwICAg&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=GQtGouAnNB1NfXPRNVlsqjITIeGGPPcRPagQWKnQ6PM&m=jAy9gByyYIXhBjOLPuEXDzueyOhwQDJ8OiIN27TdwR4&s=YlMfEWIE1we190yMvmtc0IBD2a4PlX0ZswTB_i7Id-g&e= 

_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_eclipselink-2Dusers&d=DwICAg&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=xVpSyIIO_uKj1KAOMnm4_-T4KVw7uqAp5EeFOtyE4gI&m=UfU55JUoOcp_QnxukS7X_y1Hcpc0kMCHqtvjdzjoMVE&s=4LAVwdMrEqEKp4QKBD_BFl4B0woCIzVDj4Ia2AwXr3M&e=


_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

Yun Jie Zhou
In reply to this post by Yun Jie Zhou
But why cannot be caught by generic Exception? Any potential reasons? thanks.
 
Thanks & Best Regards

Austin ( 周运杰/Yun Jie Zhou)
 
 
----- Original message -----
From: Andrei Ilitchev <[hidden email]>
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to dealwithjavax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 10:55 PM
 

May be OptimisticLockException is wrapped by javax.persistence.PersistenceException?

On 2017-10-20 10:32 AM, Yun Jie Zhou wrote:
Hello Andreas,
 
I was intent to try/catch the exception with the following code, but the control flow didn't reach the catch block.
Haven't figured out the reason.
 
public SvcDump updateSvcDump(final SvcDump svcDump)
  {
    SvcDump updated = null;
    try
    {
      updated = svcDumpDao.update(svcDump);
    }
    catch (final Exception e)
    {
      LOG.info("Cause: " + e.getCause());
      if (e.getCause() instanceof javax.persistence.OptimisticLockException)
      {
        // retry persist
        // first reload, and then copy
        final SvcDump reloaded = svcDumpDao.findBy(...)
        reloaded.setXXX(svcDump.getXXX());
        return svcDumpDao.update(reloaded);
      }
    }
    return updated;
  }
Thanks & Best Regards
 
 
----- Original message -----
From: Andreas Joseph Krogh [hidden email]
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to deal with javax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 7:12 PM
 
På fredag 20. oktober 2017 kl. 05:54:38, skrev Yun Jie Zhou <[hidden email]>:
Hello experts,
 
We got some OptimisticLockException in updating operations. Because it is Unchecked Exception, we cannot use try/catch with update operations. I searched the archived mail list, but didn't figure out a solution. Any best practice to add retry logic for concurrent access?
 
Best practice is try/catch. What's preventing you from catching OptimisticLockException?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
 
 
 
 
 
 


_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

Andreas Joseph Krogh-2
På fredag 20. oktober 2017 kl. 17:05:47, skrev Yun Jie Zhou <[hidden email]>:
But why cannot be caught by generic Exception? Any potential reasons? thanks.
 
You've not given us any info about what kind of exception is being thrown.
Send us the output of log.debug(e.getMessage, e) 
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 

_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Any best practice to dealwithjavax.persistence.OptimisticLockException?

Yun Jie Zhou
In reply to this post by Andrei Ilitchev
Thanks all for your help. I think this answer is quite correct. I was able to catch the OptimisticLockException exception after I add a em.flush() to the update operation.
 
Basically we do a batch update in an EJB timer(default Container Managed Transaction),  if we don't add em.flush(), the exception will be thrown at the transaction boundary(EJB timer method complete), so I wasn't able to catch.
 
Thanks to the list.
 
Thanks & Best Regards

Austin ( 周运杰/Yun Jie Zhou)
 
 
----- Original message -----
From: Christopher Delahunt <[hidden email]>
Sent by: [hidden email]
To: EclipseLink User Discussions <[hidden email]>
Cc:
Subject: Re: [eclipselink-users] Any best practice to dealwithjavax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 11:04 PM
 
Merge and persist do not issue SQL statements immediately, this only occurs when the context is synchronized with the database which occurs on Flush or commit. If you want to be able to catch the exception when it is a container managed transaction, you will have to call em.flush in a try/catch block.
 
Best Regards,
Chris
 
On Oct 20, 2017, at 10:54 AM, Andrei Ilitchev <[hidden email]> wrote:
 

May be OptimisticLockException is wrapped by javax.persistence.PersistenceException?

On 2017-10-20 10:32 AM, Yun Jie Zhou wrote:
Hello Andreas,
 
I was intent to try/catch the exception with the following code, but the control flow didn't reach the catch block.
Haven't figured out the reason.
 
public SvcDump updateSvcDump(final SvcDump svcDump)
  {
    SvcDump updated = null;
    try
    {
      updated = svcDumpDao.update(svcDump);
    }
    catch (final Exception e)
    {
      LOG.info("Cause: " + e.getCause());
      if (e.getCause() instanceof javax.persistence.OptimisticLockException)
      {
        // retry persist
        // first reload, and then copy
        final SvcDump reloaded = svcDumpDao.findBy(...)
        reloaded.setXXX(svcDump.getXXX());
        return svcDumpDao.update(reloaded);
      }
    }
    return updated;
  }
Thanks & Best Regards
 
 
----- Original message -----
From: Andreas Joseph Krogh [hidden email]
Sent by: [hidden email]
To: [hidden email]
Cc:
Subject: Re: [eclipselink-users] Any best practice to deal with javax.persistence.OptimisticLockException?
Date: Fri, Oct 20, 2017 7:12 PM
 
På fredag 20. oktober 2017 kl. 05:54:38, skrev Yun Jie Zhou <[hidden email]>:
Hello experts,
 
We got some OptimisticLockException in updating operations. Because it is Unchecked Exception, we cannot use try/catch with update operations. I searched the archived mail list, but didn't figure out a solution. Any best practice to add retry logic for concurrent access?
 
Best practice is try/catch. What's preventing you from catching OptimisticLockException?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
 
 
 
 
_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_eclipselink-2Dusers&d=DwICAg&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=xVpSyIIO_uKj1KAOMnm4_-T4KVw7uqAp5EeFOtyE4gI&m=UfU55JUoOcp_QnxukS7X_y1Hcpc0kMCHqtvjdzjoMVE&s=4LAVwdMrEqEKp4QKBD_BFl4B0woCIzVDj4Ia2AwXr3M&e=
 
 


_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users