Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connectWithSpringSupport出现空指针异常 #376

Open
Gargantua7 opened this issue Mar 20, 2022 · 4 comments
Open

connectWithSpringSupport出现空指针异常 #376

Gargantua7 opened this issue Mar 20, 2022 · 4 comments

Comments

@Gargantua7
Copy link

Gargantua7 commented Mar 20, 2022

线上环境出现了NPE,测试环境却无法复现
查看日志:

java.lang.NullPointerException: translator.translate("Ktorm", null, ex) must not be null
	at org.ktorm.database.Database$Companion$connectWithSpringSupport$1.invoke(Database.kt:639) ~[ktorm-core-3.4.1.jar!/:na]
	at org.ktorm.database.Database$Companion$connectWithSpringSupport$1.invoke(Database.kt:520) ~[ktorm-core-3.4.1.jar!/:na]

对应库中源码:

exceptionTranslator = { ex -> translator.translate("Ktorm", null, ex) },

猜测是translate方法为NullableexceptionTranslator仅接收Throwable类型参数

spring-boot-starter-jdbc版本为2.6.2,对应spring-jdbc版本为5.3.14
ktorm版本为3.4.1

@Gargantua7
Copy link
Author

经过排查,我发现引起此问题的原因是我使用了自定SQL语句同时执行出错,更改为正确的SQL语句后不再出现此问题
但是我认为Ktorm应该对此NPE进行一些封装提示或者处理

@abop
Copy link

abop commented Mar 4, 2024

经过排查,我发现引起此问题的原因是我使用了自定SQL语句同时执行出错,更改为正确的SQL语句后不再出现此问题 但是我认为Ktorm应该对此NPE进行一些封装提示或者处理

这里应该分为两个问题:

  1. sql 错误,与框架无关
  2. exceptionTranslator 声明为 return non-null,但 connectWithSpringSupport 用到的 org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslatortranslator.translate("Ktorm", null, ex) 时会返回 null

第2点我认为是框架没有正确处理,exceptionTranslator应该声明为

public val exceptionTranslator: ((SQLException) -> Throwable?)? = null,

abop added a commit to abop/ktorm that referenced this issue Mar 4, 2024
kotlin-orm#376 
`exceptionTranslator` 声明为 `return non-null`,但 `connectWithSpringSupport `用到的 `org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator` 在 `translator.translate("Ktorm", null, ex) `时会返回 `null`
@vincentlauvlwj
Copy link
Member

@abop 有道理,之前没有注意到,后续考虑优化

@vincentlauvlwj vincentlauvlwj reopened this Mar 4, 2024
@abop
Copy link

abop commented Mar 4, 2024

@abop 有道理,之前没有注意到,后续考虑优化

@vincentlauvlwj #524 我看已经有 PR 了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants