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)
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 directlyDSL
creates queries that are "unattached", and thus cannot be executed, only embedded in other queriesThere are two solutions:
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:
SELECT
in the INSERT
statementDSL.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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With