Created
December 24, 2018 09:36
-
-
Save kapion/3a85b8b11c89b338d774cfb4cd32012b to your computer and use it in GitHub Desktop.
getDbNetInfoFromPostgresDriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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