r2dbc-postgresql icon indicating copy to clipboard operation
r2dbc-postgresql copied to clipboard

The code that initializes the mapping of enums of the DB-JVM have problem

Open geliotropv opened this issue 3 years ago • 1 comments
trafficstars

Main discription of problem: There is a service that starts before the connection to the database appears, after the connection appears, this situation occurs: EnumCodec is created after the connection is received and the code that initializes the DB-JVM enum mapping has already completed. Because we have a pool and this mapping seems to be called only once. The first launch of the service cannot be successful.

I am using below given dependencies

org.springframework.data:spring-data-r2dbc
name.nkonev.r2dbc-migrate:r2dbc-migrate-spring-boot-starter
org.postgresql:r2dbc-postgresql
io.r2dbc:r2dbc-pool

The service itself start in k8s with postgres pod that start after service This service have several of enums that I connect via R2dbcCustomConversions + EnumCodec.builder() from r2dbc posgresql through bean: bean { EnumCodec.builder() .withEnum("status", Status::class.java) .build() } bean { ConnectionFactoryOptionsBuilderCustomizer { builder -> builder.option(EXTENSIONS, provider<Extension>().toList()) } } bean { R2dbcCustomConversions.of( PostgresDialect.INSTANCE, provider<BusinessConverter<*, *>>().toList() ) } In the base itself I have a created TYPE status AS ENUM And after reading the records from the database, I see errors related to the fact that there is no codec that could read the enum from postgresql When starting the service now I see the following error 2022-07-21 13:35:01.962 WARN 1 --- [actor-tcp-nio-2] i.r.p.c.EnumCodec : Could not lookup enum types for: [status] And after service start read row from db show next exception Caused by: java.lang.IllegalArgumentException: Cannot decode value of type test.package.Status$Status with OID 16418 at io.r2dbc.postgresql.codec.DefaultCodecs.decode(DefaultCodecs.java:222) ~[r2dbc-postgresql-0.9.1.RELEASE.jar:0.9.1.RELEASE] I'm looking for a way to restart mapping these enums after getting a database connection.

geliotropv avatar Jul 21 '22 16:07 geliotropv

Your database is being initialized after creating an R2DBC connection? Because the connection is already open, the EnumCodec isn't registered because at the time the connection was created, the database didn't have the enum type registered.

One approach can be to create a separate connection factory just for initialization purposes to avoid this problem.

We avoid the runtime loading of types because we cannot run queries while streaming the result of an ongoing query.

mp911de avatar Jul 22 '22 07:07 mp911de

Closing due to lack of actionability.

mp911de avatar Nov 08 '22 14:11 mp911de