Skip to content

Kotlin codegen: add an Exposed 1.x output mode (0.x output blocks the identifier-cache thread-safety fix) #390

Closed

Description

@dmealing

Problem

Every Kotlin generator in server/java/codegen-kotlin emits Exposed 0.x APIs (org.jetbrains.exposed.sql.*). Exposed 1.0 renamed those packages to org.jetbrains.exposed.v1.* and changed several signatures, so an adopter that uses the generated *Table objects cannot upgrade Exposed past 0.x.

That blocks a real fix. Exposed 0.x shares one IdentifierManagerApi per Database across all threads, and its identifier caches are plain LinkedHashMaps that getOrPut mutates on every query. Under concurrent queries they corrupt. The symptom is ClassCastException: class java.util.LinkedHashMap$Entry cannot be cast to class java.util.HashMap$TreeNode, or a thread that spins forever inside HashMap. A downstream Kotlin adopter hit the exception in its integration tests. A 16-thread stress test against exposed-core 0.55.0 reproduced it in 20 of 20 rounds, and 18 of those rounds hung. Upstream fixed it only in Exposed 1.3.0 (JetBrains/Exposed PR #2783, "fix thread-safety of IdentifierManagerApi identifier caches (#1704)"). The adopter currently carries a same-name shadow class to patch it, which is a stopgap.

What the generators emit today

Generator 0.x APIs in its output
KotlinExposedTableGenerator Table, Column, ColumnType, ReferenceOption, CustomFunction + UUIDColumnType (the gen_random_uuid() default), json.jsonb, javatime.*; the emitted MetaInstantWithTimeZoneColumnType / MetaUriColumnType / MetaInetColumnType support files use IDateColumnType, statements.api.PreparedStatementApi and vendors.currentDialect
KotlinTypeMapper uuid(col), javatime.date/datetime/time, json.jsonb, TextColumnType, UUIDColumnType
KotlinRepositoryGenerator selectAll, insert, update, deleteWhere, ResultRow, SqlExpressionBuilder, statements.UpdateBuilder/UpdateStatement, transactions.transaction
KotlinRelationsGenerator selectAll, Query, JoinType, and, or, SqlExpressionBuilder.eq
KotlinSpringControllerGenerator selectAll, insert, update, deleteWhere, Op, SortOrder, castTo, SqlExpressionBuilder, TextColumnType, transactions.transaction
KotlinStoredProcGenerator Transaction, transactions.transaction, the scalar *ColumnTypes, javatime.JavaInstantColumnType/JavaLocalDateColumnType, UUIDColumnType
KotlinSpringConfigGenerator, KotlinValidatorGenerator org.jetbrains.exposed.sql / Table

What 1.x output needs (from the official 1.0 migration guide)

  • Package moves. Core types go to org.jetbrains.exposed.v1.core.*. The JDBC-side functions (selectAll, insert, update, deleteWhere, Query, Database, transactions.transaction, SchemaUtils) go to org.jetbrains.exposed.v1.jdbc.*. Also v1.javatime.* and v1.json.*.
  • UUID columns. Table.uuid() now maps kotlin.uuid.Uuid. java.util.UUID columns must emit javaUUID(col) (org.jetbrains.exposed.v1.core.java.javaUUID), and UUIDColumnType moves to v1.core.java.UUIDColumnType. This is the one silent trap: emitting uuid() still compiles, but it changes the Kotlin type.
  • SqlExpressionBuilder is gone. eq, and and the other operators become top-level imports from v1.core, and deleteWhere / update(where = …) lambdas lose their receiver.
  • Custom column types. IColumnType.readObject(rs: ResultSet, …) becomes readObject(rs: RowApi, …). currentDialect moves, and the metadata-query methods move to currentDialectMetadata. This affects the emitted Meta*ColumnType support files.
  • Transactions. The receiver type becomes JdbcTransaction, and TransactionManager.defaultDatabase no longer defaults to the last registered database.
  • Consumers. Exposed 1.x needs Kotlin ≥ 2.2; 1.3.0 is built against stdlib 2.3.20. spring-transaction 1.3.0 is built against Spring 6.2 / Boot 3.5.

Proposed shape

  • A generator arg such as exposedApi = 0 | 1, defaulting to 0, so existing adopters' output stays byte-identical. Every Kotlin generator above honours it. The alternative is a clean major-version cut to 1.x only, which is the maintainer's call.
  • Extend CodegenCompileConformanceTest (and the integration-tests-kotlin reference module) to compile the generated output against Exposed 1.3.x as well as 0.x. A mode that is never compiled will rot.
  • Document the Kotlin and Spring floor that the 1.x mode implies.

Acceptance

  • For the same metadata, exposedApi = 1 output compiles against exposed-core/jdbc/java-time/json 1.3.x, and exposedApi = 0 output is unchanged.
  • UUID columns keep Column<java.util.UUID> under 1.x (javaUUID, never uuid).
  • The emitted custom column types round-trip through a real JDBC read under 1.x.

Activity

  1. added
    FR
    on Oct 2, 2026
  2. added this to the 1.1 milestone on Oct 2, 2026
  3. dmealing commented on Oct 3, 2026

    @dmealing
    MemberAuthor

    Shipped in 1.0.12 (Maven Central 8.0.12): exposedApi = 0 | 1 on every Kotlin generator; 1.x output compile-checked with Kotlin 2.2 against Exposed 1.3.1 and real Spring 6.2, custom column types round-tripped through Postgres. See docs/ports/kotlin.md.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions