H2数据库中的当前时刻 [英] Current moment in H2 database

查看:453
本文介绍了H2数据库中的当前时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取H2数据库中的当前当前实际时钟时间,当前时刻?

How to get the actual current clock time, the current moment, in H2 database?

CURRENT_TIMESTAMP 函数给出了当前数据库事务开始的时刻.有没有办法获取当前时刻,当前语句执行的时间?这可能与CURRENT_TIMESTAMP相同或晚于CURRENT_TIMESTAMP.

The CURRENT_TIMESTAMP function gives the moment when the current database transaction began. Is there a way to get the current moment, the time when the current statement is executing? This may be the same or later than CURRENT_TIMESTAMP.

为了进行比较,请在

For comparison, in Postgres, some functions such as current_timestamp return the transaction start time while some functions such as clock_timestamp return the actual current clock time.

推荐答案

(函数调用的)当前时间

您可以创建 ALIAS 对于System.currentTimeMillis():

CREATE ALIAS CURRENT_TIME_MILLIS FOR "java.lang.System.currentTimeMillis";

这不会生成语句执行开始的时间戳,但实际上是 current 时间戳,无论何时H2实际调用该函数-即不确定的时刻,以及不同行的值可能不同在同一条语句中.

This wouldn't generate the timestamp of statement execution start, but really the current timestamp, whenever H2 actually calls the function - i.e. a non-deterministic moment, and perhaps a different value for different rows within the same statement.

但是,也许这足以满足您的要求.

But perhaps, that's good enough for your requirements.

如果上述不确定性解决方案对您而言不够精确,则另一种选择是实现JDBC包装器,该包装器将拦截所有语句,并将当前时间设置为某些本地线程(H2不支持Connection.getClientInfo()):

If the above non-deterministic solution is not sufficiently precise for you, another option would be to implement a JDBC wrapper that intercepts all statements, sets the current time to some thread local (H2 doesn't support Connection.getClientInfo()):

threadlocal.set(new Timestamp(System.currentTimeMillis()).toString());

...,然后像这样从ALIAS中读取该客户端信息:

... and then reads that client info from an ALIAS like this:

public static Timestamp statementTime() throws SQLException {
    return Timestamp.valueOf(threadlocal.get());
}

然后:

CREATE ALIAS STATEMENT_TIME FOR "com.example.statementTime";

这篇关于H2数据库中的当前时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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