jOOQ事务中MySQL临时表不存在问题求助
问题:jOOQ创建的临时表在事务中无法被找到
代码实现
我编写了以下Kotlin代码,使用jOOQ在事务内创建临时表并执行后续操作:
fun <T> withTempTableTxn( ctx: DSLContext, tempTableSelectAs: org.jooq.Select<*>, txnBlock: (DSLContext, org.jooq.Name) -> T, ): T { val uuid = UUID.randomUUID().toString().replace("-", "_") val tempTableName = DSL.name("temp_table_$uuid") val createTempTable = DSL.createGlobalTemporaryTable(tempTableName).`as`(tempTableSelectAs) return ctx.transactionResult { txn -> txn.dsl().execute(createTempTable) val res = txnBlock(txn.dsl(), tempTableName) txn.dsl().execute(DSL.dropTemporaryTableIfExists(tempTableName)) res } } private fun updateMutation( mutationRequest: MutationRequest, op: UpdateMutationOperation, ctx: DSLContext, ): MutationOperationResult { // 临时表要填充的数据行 val tempTableRows = DSL .selectFrom(DSL.table(DSL.name(op.table.value))) .where(/* ... */) return withTempTableTxn(ctx, tempTableRows) { txn, tempTableName -> // ... } }
问题现象
运行时出现临时表找不到的错误:
注意:若将临时表替换为普通表,所有操作均可正常执行
打印创建临时表的SQL语句,确认语句本身是正确的:
Creating temp table: create temporary table `temp_table_e2fd79a5_4589_41df_9972_8ff9b216300a` as select * from `Chinook`.`Artist` where `Artist`.`Name` = 'foobar'
但后续执行更新操作时抛出如下错误:
13:48:02 ERROR traceId=, parentId=, spanId=, sampled= [io.ha.ap.ExceptionHandler] (executor-thread-0) Uncaught exception: org.jooq.exception.DataAccessException: SQL [update `temp_table_e2fd79a5_4589_41df_9972_8ff9b216300a` set `Name` = 'foobar new' where `temp_table_e2fd79a5_4589_41df_9972_8ff9b216300a`.`Name` = 'foobar']; Table 'Chinook.temp_table_e2fd79a5_4589_41df_9972_8ff9b216300a' doesn't exist
排查与解决方向
- 调整临时表创建方式:
createGlobalTemporaryTable对应数据库的全局临时表(会话级可见),部分数据库(如MySQL)的临时表是会话隔离而非事务隔离,换成createTemporaryTable尝试,确保临时表在当前事务/会话内可见。 - 检查表名引用方式:错误信息显示数据库在
Chinookschema下查找临时表,但临时表默认是会话私有,不属于特定schema。确认txnBlock内引用临时表时未错误指定schema,直接使用表名即可。 - 验证事务连接一致性:确保
transactionResult内的txn.dsl()始终使用同一个数据库连接,若jOOQ事务配置导致创建表和更新操作使用不同连接,会出现临时表不可见的情况。 - 适配数据库临时表特性:不同数据库对临时表的实现不同:MySQL临时表是会话隔离,PostgreSQL临时表是事务隔离。根据使用的数据库,确保操作在同一个会话/事务周期内完成。
内容的提问来源于stack exchange,提问作者Gavin Ray
相关产品推荐
相关产品推荐

