Db2 SQLCODE

SQLCODE -842: Connection already exists

SQLCODE -842 (SQLSTATE 08002) means a CONNECT statement named a location that the application process already has a connection to, in a situation where Db2 won't reuse it. The most common triggers are the SQLRULES(STD) bind option and a CONNECT with USER and USING.

Other names for this code

Also written as SQL -842, SQLCODE=-842, SQLCODE = -842, db2 -842, sqlstate 08002, SQL0842N, a connection to location already exists.

What it means

With type 2 CONNECT, a process can hold connections to several locations at once. What happens when a program issues CONNECT to a location it's already connected to depends on the SQLRULES bind option of the plan.

  • SQLRULES(DB2), the default, treats that CONNECT like SET CONNECTION and makes the existing connection current.
  • SQLRULES(STD) follows the SQL standard and rejects it with -842.

Db2 for z/OS also returns -842 when a type 2 CONNECT with the USER and USING clause names a connection that already exists. Db2 won't switch the user on a live connection that way. The documentation also lists two older cases that involve private connections made through system-directed access, where a CONNECT and a private connection clash over the same location.

Db2 for LUW uses SQL0842N with the same SQLSTATE for the SQLRULES STD case.

Common causes

  • The plan was bound with SQLRULES(STD) and the program issues CONNECT to a location it already connected to earlier in the process.
  • Retry or reconnect logic issues CONNECT TO location USER ... USING ... while the old connection is still there.
  • Two programs in the same process each connect to the same location, and the plan uses SQLRULES(STD).
  • The location name in the CONNECT is wrong and happens to match another connection the process already holds.

How to find the problem

  1. Note the location name in the message and find the CONNECT that failed.
  2. Find the earlier CONNECT to that location in the same process. It may be in another program.
  3. Check the plan's SQLRULES bind option in the BIND output or your change tool.
  4. If the CONNECT has USER and USING, check whether the program released and committed the old connection before it.

How to fix it

  • Use SET CONNECTION location to switch to a connection that already exists.
  • Rebind the plan with SQLRULES(DB2) if standard rules aren't needed, so a repeat CONNECT just makes the connection current.
  • Before reconnecting with USER and USING, issue RELEASE location and then COMMIT in an earlier unit of work, so the old connection is gone.
  • Correct the location name if it was a typo.
  • After changing the program, rebind the plan or package and resubmit the job.

What it looks like

DSNT408I SQLCODE = -842, ERROR:  A CONNECTION TO DB2REMOT ALREADY EXISTS
DSNT418I SQLSTATE   = 08002 SQLSTATE RETURN CODE

IBM documentation

IBM documentation: -842. Use the documentation for your installed release.

Report a correction to WND Software.

Questions

Does RELEASE end the connection straight away?

No. RELEASE marks the connection, and Db2 ends it at the next commit. That's why the reconnect has to happen in a later unit of work.

What is the difference between CONNECT and SET CONNECTION?

CONNECT creates a connection. SET CONNECTION makes an existing one current without creating anything new.

Which SQLRULES value is the default?

SQLRULES(DB2). With it, a CONNECT to a location the process already holds just makes that connection current.