Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage DSLContext in jooq? (close connection)

Tags:

java

jooq

This is how I implement each jooq query that 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 overworking here? / Is it safe to leave the Context and Connection Open?

In case it is safe to leave it open I would rather work with 1 static field DSLContext per UtilClass (and only the commented section would be on my methods). I would be opening a connection for each UtilClass since I am encapsulating the methods per table (more or less).

like image 863
corlaez Avatar asked Jan 05 '15 05:01

corlaez


People also ask

Does jOOQ close connection?

The normal operation mode is to provide a Configuration with a JDBC Connection, whose lifecycle you will control yourself. This means that jOOQ will not actively close connections, rollback or commit transactions.

What is DSLContext in jOOQ?

DSLContext references a org. jooq. Configuration , an object that configures jOOQ's behaviour when executing queries (see SQL execution for more details). Unlike the static DSL, the DSLContext allow for creating SQL statements that are already "configured" and ready for execution.

What is a DSLContext?

public interface DSLContext extends Scope. A contextual DSL providing "attached" implementations to the org. jooq interfaces. Apart from the DSL , this contextual DSL is the main entry point for client code, to access jOOQ classes and functionality that are related to Query execution.

What is DSLContext in Java?

DSLContext is a Scope type, mostly for convenience access to its underlying Configuration properties, including the Scope. data() map, which it shares with the Configuration .


1 Answers

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

A JDBC Connection, however, is a resource, and as all resources, you should always close it explicitly. The correct way to close resources in Java 7+ is by using the try-with-resources statement:

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
 }

More information about the try-with-resources statement can be seen here. Please also notice that the jOOQ tutorial uses the try-with-resources statement when using standalone JDBC connections.

When is DSLContext a resource?

An exception to the above is when you let your DSLContext instance manage the Connection itself, e.g. by passing a connection URL as follows:

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

In this case, you will need to close() the DSLContext as shown above

like image 158
Lukas Eder Avatar answered Oct 05 '22 22:10

Lukas Eder