简体   繁体   中英

Why does this simple JDBC/JOOQ code creates 10 connections to my database?

Im working on a spring project ran on a local environment using SpringToolSuite. I'm using Putty to create a tunel to access an app server from which i can query my MySQL database (i have to use SSH). So i'm running this simple code:

public static void getConnection() {
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DSLContext create = DSL.using(connection, SQLDialect.MYSQL);

        Personne personne = Personne.PERSONNE.as("personne");
        Evenement evenement = Evenement.EVENEMENT.as("evenement");
        Genealogie genealogie = Genealogie.GENEALOGIE.as("genealogie");
        Lieu lieu = Lieu.LIEU.as("lieu");
        
        Result<Record3<Integer,Integer,String>> result = create
                .select(DSL.countDistinct(personne.ID).as("countRs"), 
                        evenement.IDGROUPE2.as("group2Rs"),
                        lieu.LIBELLE.as("libelleRs"))
                .from(evenement.innerJoin(personne)
                            .on(personne.ID.eq(evenement.IDPERS))
                        .innerJoin(genealogie)
                            .on(genealogie.ID.eq(personne.IDGEN))
                        .innerJoin(lieu)
                            .on(lieu.ID.eq(evenement.IDGROUPE2)))
                .where(personne._NOM.eq(" ")
                        .and((personne._PRENOM.eq(" ")
                            .or(personne._PRENOM.like(" -%"))))
                        .and(evenement.IDPERS.isNotNull())
                        .and(lieu.LIBELLE.isNotNull())
                        .and(genealogie.STATUS.ge(Byte.valueOf("1")))
                        .and(personne.CONTEMPORAIN.eq(Byte.valueOf("0"))))
                .groupBy(evenement.IDGROUPE2)
                .fetch();

        System.out.println("countRs group2Rs libellesRs");
        System.out.println("---------------------------");
        for (Record r : result) {
            System.out.println(r.get("countRs")+" "+r.get("group2Rs")+" "+r.get("libelleRs"));
        }
        
        try {
            create.close();
            connection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 

As you can see it's basicly just opening the connection with the database, making a query and closing the connection/query, nothing special.

But when i check the client connections to my database using MySQL Workbench i can see that my code opened 10 connection:

连接

And as you can see, only a single one of these connections is actualy executing the query. Is there something i don't know about how JOOQ executes queries? Or mabe it is because i'm using Putty to access my remote server and it somehow creates many connections?

Since you're using HikariCP in your project, you should not create new connections manually using DriverManager :

// Don't do this
connection = DriverManager.getConnection(url, user, password);
DSLContext create = DSL.using(connection, SQLDialect.MYSQL);

Instead, use HikariCP as a DataSource and pass that to jOOQ:

// Do this
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);

Now, you don't have to do any resource management anymore, because jOOQ / Hikari do this for you, behind the scenes (ie no close() calls)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM