Skip to content

Instantly share code, notes, and snippets.

@kapion
Created December 24, 2018 09:36
Show Gist options
  • Select an option

  • Save kapion/3a85b8b11c89b338d774cfb4cd32012b to your computer and use it in GitHub Desktop.

Select an option

Save kapion/3a85b8b11c89b338d774cfb4cd32012b to your computer and use it in GitHub Desktop.
getDbNetInfoFromPostgresDriver
class PostgresqlDialect(private val dbInfo: DbDialect.DbInfo, val jdbcTemplate: JdbcTemplate) : DbDialect {
private val log = LoggerFactory.getLogger(this.javaClass)
private val connection = jdbcTemplate.dataSource.connection
...
private fun getDbNetInfoFromPostgresDriver() {
try {
//HikariProxyConnection wrapping org.postgresql.jdbc.PgConnection
val delegate: Any = FieldUtils.readField(connection, "delegate",true)
//спускаемся по иерархии драйвера постгрес
val protoConnection = FieldUtils.readField(delegate, "protoConnection",true)
val pgStream = FieldUtils.readField(protoConnection, "pgStream",true)
dbInfo.db = FieldUtils.readField(protoConnection, "database",true) as String
val socket = FieldUtils.readField(pgStream, "connection",true)
val impl = FieldUtils.readField(socket, "impl",true)
val addressMethod = ReflectionUtils.findMethod(impl.javaClass, "getInetAddress")
addressMethod?.let {
ReflectionUtils.makeAccessible(it)
dbInfo.host = it.invoke(impl).toString()
}
val portMethod = ReflectionUtils.findMethod(impl.javaClass, "getPort")
portMethod?.let {
ReflectionUtils.makeAccessible(it)
dbInfo.port = it.invoke(impl).toString()
}
dbInfo.schema = getCurrentScheme()
} catch (e: Exception) {
log.error("Error get the PostgreSQL driver data for dbInfo")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment