Sometimes the connection state is unclear. The following can all make it fuzzy wether or not you have a valid connection:
- A db2 error or warning related to your connection
- A system error related to network connectivity
- Changing VPNs or adding a VPN connection
- Leaving a connection up overnight or over longer periods
This tip is strictly related to the DB2 command line. Various database interfaces in scripting languages or tools may have another way of doing this.
To check a connection state at the DB2 command line, you can issue:
>db2 get connection state Database Connection State Connection state = Connectable and Connected Connection mode = SHARE Local database alias = SAMPLE Database name = SAMPLE Hostname = Service name =
There is no specific authorization needed to execute this command, unlike many DB2 commands.
The possible states include:
- Connectable and connected – meaning you are connected to the database listed
- Connectable and unconnected – you are not connected, but may connect
- Unconnectable and connected
- Implicitly connectable
In addition to the mode of SHARE
seen above, the connection mode could also be EXCLUSIVE
. The Hostname and the Service Name are only populated if the connection is a remote one over TCP/IP.
The Knowledge Center page on the GET CONNECTION STATE
command is: http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0001953.html?cp=SSEPGG_10.5.0/3-5-2-4-41&lang=en
Excellent! I don’t know how I had never stumbled across that before. I ususally just type db2 connect, but it’s not good at detecting if you’ve been forced, such as if the db has been quiesced.
Thanks for the tip 🙂
Is there any property that maintains the DB2 connection status that I can refer to rather than checking the connection status using if x = ‘Open”.
I am thinking something like this
If IBM.Data.DB2.DB2Connection::State.Open
Hi Manish – not sure if I understood your question correctly. If your application is using the IBM Data Server Provider for .NET (as your post suggests), then the State property of the DB2Connection class instance is exactly what you would want to investigate.
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.swg.im.dbclient.adonet.ref.doc/doc/DB2ConnectionClassStateProperty.html.
this command cannot be used to check instance status.
it says connectable when database manager is down
so I’m not sure how it’s useful.