在Java中以循环方式连续查询数据库 [英] querying DB in a loop continuously in java

查看:2185
本文介绍了在Java中以循环方式连续查询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议连续循环查询数据库以获取添加到特定表中的任何新数据吗?

Is it advisable to query a database continuously in a loop, to get any new data which is added to specific table?

我下面有一段代码:

  while(true)
      try{
         // get connection
         // execute only "SELECT" query
       }
      catch(Exception e){}
      finally{// close connection

      }
         //Sleep 5 sec's
      }


推荐答案

这是一种简单的方法,可用于很多情况下。确保您使用的select语句不会对数据库造成尽可能小的负载。

It is a simple approach that works in many cases. Make sure that the select statement you use doesn't put as little load as possible on the database.

更好(但更难设置)的变体是使用某种机制使数据库主动通知有关更改。例如,某些数据库可以使用某种排队机制发送信息,而该排队机制又可以使用数据库触发器来触发。

The better (but more difficult to setup) variant would be either to use some mechanism to get actively informed by the database about changes. Some databases can for example can send information with some queuing mechanism, which again could be triggered using a database trigger.

这篇关于在Java中以循环方式连续查询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆