Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No connection configured

how i can configure connection for fix this bug? I use MySQL and jdbc.

public static void saveCallLink(String oktell_login, String link)
        throws SQLException {
    Connection conn = DBConnection.getDataSource().getConnection();
    DSL.using(conn)
            .insertInto(CALLS)
            .columns(CALLS.USER_ID, CALLS.CALL_LINK)
            .values(
                    DSL.select(USERS.ID)
                            .from(USERS)
                            .where(USERS.OKTELL_LOGIN.equal(oktell_login))
                            .fetchOne().value1()
                    , link
            ).execute();
    conn.close();
}

Sorry, log was added.

org.jooq.exception.DetachedException: Cannot execute query. No Connection configured org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:316) org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:365) org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:352) org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:517) org.jooq.impl.SelectImpl.fetchOne(SelectImpl.java:2868) ru.avito.model.CallModel.saveCallLink(CallModel.java:33) ru.avito.web.OktellListener.saveCallRecord(OktellListener.java:31) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source)

like image 693
Дмитрий Окунев Avatar asked Jul 12 '16 13:07

Дмитрий Окунев


1 Answers

Your mistake is here:

DSL.using(conn)
   .insertInto(CALLS)
   .columns(CALLS.USER_ID, CALLS.CALL_LINK)
   .values(
       DSL.select(USERS.ID)
          .from(USERS)
          .where(USERS.OKTELL_LOGIN.equal(oktell_login))
          .fetchOne().value1() // This query cannot be executed
       , link
   ).execute();

The query you ran in the middle of your INSERT statement cannot be executed because it has no Configuration (context) attached to it. As a general rule of thumb, always remember:

  • DSLContext creates queries that are "attached" to the DSLContext.configuration(), and thus can be executed directly
  • DSL creates queries that are "unattached", and thus cannot be executed, only embedded in other queries

There are two solutions:

1. Attach the nested select

DSL.using(conn)
   .insertInto(CALLS)
   .columns(CALLS.USER_ID, CALLS.CALL_LINK)
   .values(
       DSL.using(conn) // Use DSLContext, not DSL here
          .select(USERS.ID)
          .from(USERS)
          .where(USERS.OKTELL_LOGIN.equal(oktell_login))
          .fetchOne().value1()
       , link
   ).execute();

Note though, that this will run two separate queries from the client, which is probably not what you really want. You want:

2. Nest your SELECT in the INSERT statement

DSL.using(conn)
   .insertInto(CALLS)
   .columns(CALLS.USER_ID, CALLS.CALL_LINK)
   .values(
       DSL.field(
           DSL.select(USERS.ID)
              .from(USERS)
              .where(USERS.OKTELL_LOGIN.equal(oktell_login))
       )
       , link
   ).execute();

This is now the same as the following single SQL query:

INSERT INTO calls (user_id, call_link)
VALUES ((SELECT id FROM users WHERE oktell_login = :oktell_login), :link)
like image 171
Lukas Eder Avatar answered Oct 04 '22 01:10

Lukas Eder