简体   繁体   English

Java调度与SQL调度

[英]Java scheduling Vs SQL scheduling

Here is my requirement: 这是我的要求:

a date is inserted in to a db table with each record. 将日期插入到包含每条记录的db表中。 Two weeks before that particulate date, a separate record should be entered to a different table. 在该微粒日期前两周,应在另一个表格中输入单独的记录。

My initial solution was to put up a SQL schedule job, but my client insisted on it being handled through java. 我最初的解决方案是提出一个SQL计划工作,但我的客户坚持要通过java处理它。

  • What is the best approach for this? 对此最好的方法是什么?
  • What are the pros and cons of using SQL schedule job and Java scheduling for this task? 使用SQL调度作业和Java调度来完成此任务有哪些优缺点?

Ask yourself the question: to what domain does this piece of work belong? 问自己一个问题:这件作品属于哪个领域? If it's required for data integrity, then it's obviously the DBMS' problem and would probably best be handled there. 如果它是数据完整性所必需的,那么它显然是DBMS的问题,可能最好在那里处理。 If it's part of the business domain rather than the data, or might require information or processing that's not available or natural to the DBMS, it's probably best made external. 如果它是业务领域的一部分而不是数据,或者可能需要DBMS不可用或不自然的信息或处理,那么它最好是外部的。

I'd say, use the best tool for the job. 我会说,使用最好的工具来完成工作。 Having stuff handled by the database using whatever features it offers is often nice. 使用它提供的任何功能来处理由数据库处理的内容通常很好。 For example, a log table that keeps "snapshots" of status updates of records in another table is something I typically like to have a trigger for, taking that responsibility out of my app's hands. 例如,一个日志表保存了另一个表中记录状态更新的“快照”,我通常喜欢触发它,从我的应用程序的手中承担这个责任。

But that's something that's available in practically any DBMS. 但这几乎可以在任何DBMS中使用。 There's the possibility that other databases won't offer the job scheduling capacities you require. 其他数据库可能无法提供您所需的作业调度功能。 If it's conceivable that some day you'll be switching to a different DBMS, you'll then be forced to do it in Java anyway. 如果可以想象有一天你会转换到不同的DBMS,那么你将被迫用Java做这件事。 That's the advantage of the Java approach: you've got the functionality independently of the database. 这就是Java方法的优势:您拥有独立于数据库的功能。 If you're using pure JDBC with standard SQL queries, you've got a fully portable solution. 如果您将纯JDBC与标准SQL查询一起使用,那么您就拥有了一个完全可移植的解决方案。

Both approaches seem valid. 两种方法似乎都有效。 Consider what induces the least work and worries. 考虑什么导致最少的工作和担忧。 If it's done in Java you'll need to make sure that process is running or scheduled. 如果用Java完成,则需要确保进程正在运行或已安排。 That's some external dependency. 这是一些外部依赖。 If it's in the database, you'll be sure the job is done as long as the DB is up. 如果它在数据库中,只要数据库已启动,您就可以确定作业已完成。

Well, first off, if you want to do it in Java, you can use the Timer for a simple basic repetitive job, or Quartz for more advanced stuff. 好吧,首先,如果你想用Java做,你可以使用Timer进行简单的基本重复性工作,或者使用Quartz进行更高级的工作。

Personally I also think that it would be better to have the same entity (application) deal with all related database actions. 我个人也认为让相同的实体(应用程序)处理所有相关的数据库操作会更好。 In other words, if your Java app is reading/writing to/from the db, it should be consistent and also deal with scheduled reading/writings. 换句话说,如果您的Java应用程序正在读取/写入数据库,它应该是一致的,并且还要处理预定的读/写。 And as a plus, this way you can synchronize your actions easier, like, if you want to make sure that a scheduled job is running, has started, has finished, you can do that a lot easier if all is done in Java as compared with having a different process (like the SQL Scheduler) doing it. 另外,通过这种方式,您可以更轻松地同步您的操作,例如,如果您想确保计划的作业正在运行,已经启动,已经完成,那么如果所有操作都在Java中完成,则可以更轻松地执行此操作有一个不同的进程(如SQL Scheduler)这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM