PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] How to handle a connection properly with jooq?

 April 20, 2022     connection, jooq, kotlin, oracle, session     No comments   

Issue

I am getting a lot of inactive sessions in the database which not only take up all the resources but also cause the database to crash occasionally requiring me to restart it.

I am using jooq with kotlin, and this is how I establish a connection.

@Component
class EstDBConnection(private val cfg: DatabaseConfig, private val jooqExecuteListener: PromJooqExecuteListener) {

    init {
        cfg.migrateFlyway()
    }

    fun <T> acquire(f: (DSLContext) -> T): T {
        return DSL.using(DriverManager.getConnection(cfg.url, cfg.username, cfg.password), SQLDialect.ORACLE10G).use {
            jooqExecuteListener.attach(it)
            f(it)
        }
    }
}


Solution

You're never closing the connections that you're creating. Please use a connection pool (e.g. HikariCP) to manage your connections. Unless your writing a simple batch script, or some proof of concept, you should never resort to using DriverManager.getConnection directly



Answered By - Lukas Eder
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing