我需要从表中读取数据并对每一行做一些工作并将该行标记为已更新。我不想再次阅读更新的行。这是我打算用 ExecutorService 做的事情。这个对吗 ?
谢谢。
public class ScheduledDBPoll
{
public static void main(String args[])
{
ExecutorService service = Executors.newFixedThreadPool(10);
/* Connect to database. */
while (true)
{
/* Issue a select statment for un-updated rows. */
/* Get the primary key. */
service.submit(new Task(primaryKey)); /* Pass the primary key*/
try
{
Thread.sleep(3000); /* Sleep for 3 seconds. */
}
catch (InterruptedException ex)
{
Logger.getLogger(ScheduledDBPoll.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* Close the connection here. */
}
}
final class Task implements Runnable
{
private int taskId;
public Task(int primayKey)
{
this.taskId = primayKey;
}
@Override
public void run()
{
try
{
/* Connect to database. */
/* Select the passed primary key row. */
/* Do some work, mark the row as updated. */
/* Close the connection here. */
}
catch (InterruptedException ex)
{
Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我正在使用 Firebird 数据库,Firebird 仅在连接级别是线程安全的。因此,我试图避免在针对同一连接运行的不同线程中进行两个查询。我还忘了提到上面的代码将作为 Windows 服务运行,所以它总是在寻找新插入的记录。