我有一个简单的 Postgres 测试表,其中包含 id、时间戳和时区。下面的测试和输出应该是不言自明的,但总而言之,我插入了一行,其时间戳的偏移量为 -6。它被正确地插入到数据库中,然后以相同的时间从数据库中加载出来,但是偏移量错误,特别是Z而不是-6。
我尝试将数据库设置为 UTC,当我在命令行中手动选择时,它会正确显示 UTC 时间。将数据库设置为 mountain,它显示预期时间,偏移量为-6。
通过在 jOOQ 中执行的语句强制数据库到不同的时区似乎没有任何作用。
context.execute( "set timezone TO 'GMT';" ); // this has no effect
Run Code Online (Sandbox Code Playgroud)
强制我的系统时间为 UTC 可以有效解决该问题,但由于多种原因是不可接受的。
TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) ); // this is a band aid that works, but is not sustainable
Run Code Online (Sandbox Code Playgroud)
这是单元测试:
@Test
public void confirmDateRoundTripFromDb() throws SQLException, DatatypeConfigurationException
{
ZonedDateTime testDate = ZonedDateTime.of( 2019, 05, 30, 12, 54, 32, 203, TimeUtilities.CENTRAL_ZONEID );
final OffsetDateTime testDateAsOffset = testDate.toOffsetDateTime( );
try( PGConnection dbConnection = DatabaseUtility.getPostgresConnection( _unitTestConfig.getSection("Postgres").getProperties(), _testDbName ) …Run Code Online (Sandbox Code Playgroud) 我使用 Spring Boot 2.4.0 和 Spring Boot Data JPA 连接到 PostgreSQL 并使用基于 JPA 的存储库执行典型的读写操作。由于其他服务也使用该数据库,因此我使用 LISTEN/NOTIFY 功能 ( https://www.postgresql.org/docs/9.1/sql-listen.html ) 来获取有关 PostgeSQL 的更改的通知。为此,我使用驱动程序com.impossibl.postgres.jdbc.PGDriver而不是默认驱动程序,并使用以下代码使 Spring 监听数据库的更改:
@Service
class PostgresChangeListener(
val dataSource: HikariDataSource,
@Qualifier("dbToPGReceiverQueue") val postgresQueue: RBlockingQueue<String>
) {
init {
listenToNotifyMessage()
}
final fun listenToNotifyMessage() {
val notificationListener = object:PGNotificationListener {
override fun notification(processId: Int, channelName: String, payload: String) {
log.info("Received change from PostgresQL: $processId, $channelName, $payload")
postgresQueue.add(payload)
}
override fun closed() {
log.debug("Connection to Postgres lost! Try to reconnect...") …Run Code Online (Sandbox Code Playgroud)