How to manage DSLContext in jooq? (closed connection)

This is how I implement every jooq request I want.

UtilClass{

 //one per table more or less
 static void methodA(){

  //my method
  Connection con = MySQLConnection.getConexion(); //open
  DSLContext create = DSL.using(con, SQLDialect.MYSQL); //open

  /* my logic and jooq querys */ //The code !!!!!!!

  try {

    if ( con != null )
       con.close(); //close
  } catch (SQLException e) {

  } //close

  con=null; //close
  create=null; //close
 }
}

Am I recycling here? / Is it safe to leave context and connection open?

In case it is safe to leave it open, I would rather work with 1 static DSLContext field per UtilClass(and only my comment would be included in my methods). I would open a connection for each UtilClass, since I encapsulate methods in a table (more or less).

+4
source share
1 answer

DSLContextusually not a resource, so you can leave it "open", i.e. You can let the garbage collector collect it for you.

JDBC Connection, , , , , . Java 7+ try-with-resources:

static void methodA() {
    try (Connection con = MySQLConnection.getConexion()) {
        DSLContext ctx = DSL.using(con, SQLDialect.MYSQL); //open

        /* my logic and jooq queries */

        // "ctx" goes out of scope here, and can be garbage-collected
    }   // "con" will be closed here by the try-with-resources statement
 }

try-with-resources . , jOOQ try-with-resources JDBC.

DSLContext ?

, DSLContext Connection, . URL- :

try (DSLContext ctx = DSL.using("jdbc:url:something", "username", "password")) {
}

close() DSLContext,

+9

Source: https://habr.com/ru/post/1569947/


All Articles