如何检查在数据库中持久化的JAVA代码是否已成功完成或突然终止?

huangapple 未分类评论51阅读模式
英文:

How to check if JAVA code (that persists in database) completed properly or terminated abruptly?

问题

以下是您要翻译的内容:

我有一个将一些数据持久化到 SQL Server 的服务。我正在持久化的两个字段分别是

is_valid,这只是一个安全检查,以防将来需要使当前服务运行无效(以及它在数据库中持久化的任何数据)。

另一个字段是 end_execution_time,它是当前服务结束的时间。

当我启动服务时,我有 is_valid=0end_execution_time=NULL。当运行完成(成功)时,我有 is_valid=1end_execution_time=GETDATETIME()。现在,这是我的关注点

我有三种可能发生的情况 ->

  1. 服务运行失败。代码知道运行失败了。所以我们在数据库中更新 is_valid = 0end_execution_time 保持为 NULL。
  2. 由于某种原因,运行失败,并且代码也停止工作(有点像终端中的 Ctrl-C)。is_valid 最初的值为 0。它仍然保持为零。end_execution_time 保持为 NULL。
  3. 有些运行正在进行中。现在 is_valid 是 0。end_execution_time 为 NULL。

将来,我如何检查当前是否有某个服务正在进行中,或者是否已突然终止?

无法区分第2种情况和第3种情况。请帮我解决一下。我正在使用 Spring-Mybatis 以及 Java/Kotlin。

英文:

I have a service that persists some data in SQL Server. 2 of the fields that i am persisting are named

is_valid that is just a safety check, in case we need to invalidate the current service run (and whatever data it persisted in DB) in future.

Another field is end_execution_time, which is the time when the current service ended.

When i start the service, i have is_valid=0 and end_execution_time=NULL. When the run completes (succesfully), i have is_valid=1 and end_execution_time=GETDATETIME(). Now, here is my concern

I have 3 scenarios that might happen ->

  1. Service Run fails. Code knows that run failed. So we update in DB that is_valid = 0. end_execution_time remains NULL.
  2. For some reason, Run fails, and code also stops working (sort of like a Ctrl-C in terminal) . is_valid originally has a value of 0. it still remains zero. end_execution_time remains NULL.
  3. some run is active. right now is_valid is 0. end_execution_time is NULL.

IN FUTURE, HOW DO I CHECK WHETHER CURRENTLY SOME SERVICE IS IN-PROGRESS, OR HAD TERMINATED ABRUPTLY?

There is no way to distinguish between the 2nd and 3rd case. Please help me out. I'm using Spring-Mybatis and Java/Kotlin

答案1

得分: 0

如果您可以修改服务代码,以下是一些建议,可以帮助区分第2种情况和第3种情况。

  1. 检查长时间运行的服务调用并将它们置为过期。假设在某个时间间隔(1分钟?)内的调用是无效的。使用开始时间和日期差异来实现。

  2. 使用try/catch/finally中的FINALLY部分,在服务调用失败时更新数据库。

  3. 将服务调用实现为异步服务调用,可以参考这里的描述:http://wiki.eclipse.org/Jetty/Feature/Continuations#Jetty_6_Continuations 我知道您没有在使用Jetty,但听起来您需要对更新数据库的服务调用设置超时。

我没有关于如何在不修改代码的情况下解决您的问题的想法。

祝您好运!

英文:

If you can modify the service code, here are some things you can do to help with distinguishing between the 2nd and 3rd case.

  1. Check for long running service calls and expire them. Assume that calls over some interval (1 minute?) are dead. Implement this using a start time and a datediff.

  2. Use the FINALLY part of try/catch/finally to update the database on service call failures.

  3. Implement the service call as an async service call as described here: http://wiki.eclipse.org/Jetty/Feature/Continuations#Jetty_6_Continuations I know you aren't using Jetty, but it sounds like you need a timeout on the service call that updates the database.

I don't have an idea about how to address your problem without modifying code.

Good luck!

答案2

得分: 0

程序化地,您可以捕捉到进程是否突然结束或未结束。然而,在这里需要考虑的是您正在处理三种状态。依我之见,您需要中间状态的is_valid和end_time,或者数据库中的额外列,用以标识当前状态。

例如:

状态1:

     成功

     is_valid = 1

     end_time = 时间

状态2:不成功,但进程未突然结束

     is_valid = 0

     end_time = 空

状态3:不成功,并且进程突然结束

     is_valid = -1 // 仅为示例

     end_time = -1 // 仅为示例

英文:

Programmatically you can catch that does process ended abruptly or not. However concern here is that you are dealing with three states. In my opinion you either need intermediate state of is_valid and end_time or extra column in database which will identify current state

For example :

State 1 :

            success 

            is_valid = 1

            end_time = time

State 2 : unsuccessful but process did not ended abruptly

          is_valid = 0 

          end_time = null 

State 3 : unsuccessful but process and ended abruptly

          is_valid = -1 // just an example

          end_time = -1  // just an example

huangapple
  • 本文由 发表于 2020年4月7日 21:43:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61081441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定