简体   繁体   English

使用Spring避免数据库轮询的最佳方法

[英]Best approach for avoiding database polling using Spring

I have web service for reading and updating data and using spring, spring JDBC for DB access. 我有用于读取和更新数据并使用spring,spring JDBC进行数据库访问的Web服务。 My controller can be accessed by many channels like desktop, mobile etc. If data is updated using desktop, then same should reflect in mobile immediately. 我的控制器可以通过许多渠道访问,例如台式机,移动设备等。如果使用台式机更新数据,则应该立即在移动设备中反映出来。 Current approach is calling service continuously to get updated data. 当前的方法是不断调用服务以获取更新的数据。 I feel that it is worst approach and causing DB performance issue as well. 我认为这是最糟糕的方法,并且也会导致数据库性能问题。

Is there a possible way such that GET service is called only when there is DB update by other channel instead of continuous polling ? 有没有一种可能的方式,使得仅当通过其他通道进行数据库更新而不是连续轮询时才调用GET服务? What is best approach for this and how to implement it ? 最佳方法是什么?如何实施?

Continuously calling the service seems like a really bad idea. 连续调用该服务似乎是个坏主意。 I think you need a database trigger that fires when rows are inserted/updated/deleted. 我认为您需要一个数据库触发器,当插入/更新/删除行时将触发该触发器。 It could POST something to a Web Service or put something on a Message Queue. 它可以将某些内容发布到Web服务或将某些内容放入消息队列中。

Good luck. 祝好运。

I can think of an architectural answer to the problem. 我可以想到一个对该问题的体系结构答案。 Use a messaging solution between the spring controller and the database. 在spring控制器和数据库之间使用消息传递解决方案。 Infact you will need two queues 实际上,您将需要两个队列

  • EventSink queue - Publish all data change requests originating from any of the channels to this queue.The subscriber will be the service managing the database update aka dbservice . EventSink队列-将来自任何通道的所有数据更改请求发布到此队列。订户将是管理数据库更新(即dbservice)的服务。

  • EventBroadcast queue - Publish the changed data post db update to this queue. EventBroadcast队列-将数据库更新后的更改数据发布到此队列。 Ideally the dbservice should handle this publish within the same transaction as db update. 理想情况下,dbservice应该在与db update相同的事务中处理此发布。 All channels can subscribe to this queue to receive the update. 所有通道都可以订阅此队列以接收更新。

    The merits to consider this approach would involve 考虑这种方法的优点将涉及

  • Pros - this approach involves no database services so both performance and de-coupling from database changes. 优点-这种方法不涉及任何数据库服务,因此性能和与数据库更改的脱钩都没有。

  • Cons - Increased complexity 缺点-复杂性增加

Continuously polling is not as bad as you might imagine. 连续轮询并不像您想象的那样糟糕。 Pushing messages to clients without them making a request requires web-sockets or of the like to achieve this. 在没有请求的情况下将消息推送给客户端需要Web套接字等来实现。 If it is not a large repose from the server, and is not too often, as in many millions and millions of requests then I would leave it for now. 如果它不是服务器中的重要部件,并且使用频率不是很高(如数以百万计的请求中那样),那么我将暂时保留它。

If however this is a large amount of bandwidth we are talking about it then you wouldn't want to be polling. 但是,如果这是大量带宽,我们正在谈论,那么您就不想进行轮询。 You would probably want to look at a subscriber type pattern whereby clients would subscribe to be notified when a specific event occurs. 您可能希望查看订户类型模式,在发生特定事件时,客户端将通过该模式订阅客户端的通知。 When this event occurs the server would then send a message to the clients. 发生此事件时,服务器将向客户端发送一条消息。

Detecting this event shouldn't require polling a database. 检测到此事件不需要轮询数据库。 The modification to the database should trigger the event. 对数据库的修改应触发事件。 You might do this with point-cuts in Spring if you are into that sort of thing. 如果您喜欢这种事情,可以在Spring中使用切入点来实现。

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

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