Skip to content

Instantly share code, notes, and snippets.

@kui
Created March 16, 2020 09:34
Show Gist options
  • Save kui/81ec2e4502debbda76711152a3cec4c9 to your computer and use it in GitHub Desktop.
Save kui/81ec2e4502debbda76711152a3cec4c9 to your computer and use it in GitHub Desktop.
Treat timestamp with spring-data-r2dbc in non-UTC timezone
val connectionFactory = ConnectionFactories.get("r2dbc:mysql://[email protected]:3306/test")
// Wrapper to initialize each connections
val connectionFactory2 = object : ConnectionFactory {
override fun getMetadata(): ConnectionFactoryMetadata = connectionFactory.metadata
override fun create(): Publisher<out Connection> =
connectionFactory
.create()
.toMono()
.flatMap { connection ->
connection
.createStatement("SET time_zone = 'Asia/Tokyo'")
.execute()
.toMono()
.map { connection }
}
}
val client = DatabaseClient.create(connectionFactory2)
client.insert()
.into("person")
.value("name", "John Doe")
.value("birthday", LocalDateTime.of(2019, 10, 20, 10, 22))
.await()
val person = client.select()
.from("person")
.`as`(Person::class.java)
.fetch()
.awaitFirst()
println(person)
// => Person(id=1, name=John Doe, birthday=2019-10-20T10:22)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment