【发布时间】:2017-08-23 21:01:04
【问题描述】:
我有一个使用 dropwizard 框架创建的应用程序,我在其中注册了一个quartz-scheduler 作业,该作业计划在每个指定的持续时间后运行。该作业向 SQL Server DB 发起 SQL 查询并迭代 ResultSet 并将数据设置为 POJO 类,该类稍后被推送到队列中。
SQL 查询使用 UNION 连接多个表,这些表使用 where 子句中相关表的 last_modified_time 列获取在增量时间修改的记录的数据。 pom.xml 中包含的 DB jar 为 sqljdbc-4.4.0,quartz 版本为 2.2.1
查询如下所示:
SELECT
u.last_modified_date,
u.account_id,
u.user_id,
ud.is_active
FROM user u WITH (NOLOCK)
JOIN user_details ud with (NOLOCK) ON u.account_id = ud.account_id AND u.user_id = ud.user_id
WHERE u.last_modifed_date > ? AND ud.last_modifed_date <= ?
UNION
SELECT
u.last_modified_date,
u.account_id,
u.user_id,
ud.is_active
FROM user u WITH (NOLOCK)
JOIN user_details ud with (NOLOCK) ON u.account_id = ud.account_id AND u.user_id = ud.user_id
JOIN user_registration_details urd WITH (NOLOCK) ON urd.account_id = u.account_id AND urd.user_id = u.user_id AND urd.reg_id = ud.reg_id
WHERE urd.last_modifed_date > ? AND urd.last_modifed_date <= ?
这个查询被简单的连接语句和这样的结果集调用
final ManagedDataSource datasource configuration.getDatabase().build(environment.metrics(), "sql");
// configuration is the configuration class in a drop wizard application and configuration.getDatabase() returns
// the DataSourceFactory with all credentials like user, password and url set into it
try (Connection conn = dataSource.getConnection()) {
int resultSetType = SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
LOGGER.info("Starting execution: ");
try (PreparedStatement pstmt = conn.prepareStatement(getQuery(), resultSetType,resultSetConcurrency))
{
setQueryParameters(pstmt);
try (ResultSet rs = pstmt.executeQuery();)
{
//process results
}
}
} catch (SQLException | IOException ex) {
LOGGER.error(“Error occurred “ + ex);
}
LOGGER.info("Completed execution: ");
在简单的执行中,它打印日志“开始执行”,然后处理记录并打印“完成执行”。但有时在执行期间,它会打印日志“开始执行”和“完成执行”,但此查询实际上并未触发到 SQL DB。
由于我没有得到我在那个增量时间修改的记录,我让分析器检查查询是否真的被触发并且没有发现这个查询被触发到数据库。另外,我尝试添加 log4jdbc 库 http://code.google.com/p/log4jdbc/wiki/FAQ 以将查询打印到日志中,但没有为该查询打印任何日志。
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
@halfer - 虽然我在发布这篇文章时阅读了所有的注意事项,但我忘记了阅读本节。下次我发布问题时,我会处理这个问题。
-
我之前没有在 stackoverflow 上发布太多内容,所以有人可以建议我是否需要输入更多信息以获得一些建议或答案。
-
我已经有一段时间没有做很多 Java 了,但是
try-catch格式看起来不正确。您正在使用try (statement) { statement } catch { statement }。但是,我认为应该只是try { statement } catch { statement }- 我认为圆括号是错误的。 See this example - 你的版本还能编译吗? -
@halfer - 这是 try-with-resources 的语法,其中任何实现 java.lang.AutoCloseable 接口的对象都可以用这种格式初始化。使用这种方式,它会自动注意在创建的实例上调用 close() 方法,因此我们不需要在 finally 块中显式编写结束语句。
标签: java sql-server database quartz-scheduler dropwizard