# SQLCODE -842: Connection already exists

Source: https://wndsoftware.com/mainframe-errors/sqlcode-842

### 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

- Note the location name in the message and find the CONNECT that failed.
- Find the earlier CONNECT to that location in the same process. It may be in another program.
- Check the plan's SQLRULES bind option in the BIND output or your change tool.
- 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


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


### IBM documentation

[IBM documentation: -842](https://www.ibm.com/docs/en/db2-for-zos/13.0.0?topic=codes-842). Use the documentation for your installed release.

[Report a correction to WND Software](https://wndsoftware.com/contact#message).

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.

Last reviewed 2026-10-18 by the WND Software team. Written in our own words. For the full reference, check IBM’s Db2 for z/OS documentation for your release.


### Related codes

[SQLCODE -808 CONNECT not consistent with the first CONNECT Read →](https://wndsoftware.com/mainframe-errors/sqlcode-808)
[SQLCODE -900 Not connected to a server Read →](https://wndsoftware.com/mainframe-errors/sqlcode-900)
[SQLCODE -30081 Communication error detected Read →](https://wndsoftware.com/mainframe-errors/sqlcode-30081)
[SQLCODE -30082 Connection failed for a security reason Read →](https://wndsoftware.com/mainframe-errors/sqlcode-30082)
[SQLCODE -927 Language interface called without a connection Read →](https://wndsoftware.com/mainframe-errors/sqlcode-927)

### 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.

[Db2 SQLCODEs](https://wndsoftware.com/mainframe-errors/db2-sqlcodes) · [SQLCODE -840](https://wndsoftware.com/mainframe-errors/sqlcode-840) · [SQLCODE -900](https://wndsoftware.com/mainframe-errors/sqlcode-900)

[← All error codes](https://wndsoftware.com/mainframe-errors/)
