1

Is it possible to use sqlplus to programmatically check a database connection (in particular, the SYS account)?

I'm thinking something along this line

  1. Run sql plus (using Process.Start) with some parameter, giving the sys username and password
  2. Check the exit code (or even the stdout), whether it is successful or not. The best way I can come up so far is to check the stdout string whether the substring "ERROR" exist, but I don't think this is very clean

Is this something possible? What parameter do you suggest I use? The reason I want to do this is because System.Data.OracleClient doesn't support connection as SYSDBA

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

3 Answers3

2

This might work

whenever sqlerror exit failure
whenever oserror exit failure
conn user/pwd@db-name
select 'success' from dual;
exit

The above script will have the following behaviour:

  • sqlplus process will return non-zero code if there was an error
  • Otherwise sqlplus will return zero

Note that I haven't tested the above.

Amendment

The script should be run as follows on unix:

sqlplus -nolog @script-file.sql

And as follows on windows:

sqlplus /nolog @script-file.sql
Andrew Skirrow
  • 3,402
  • 18
  • 41
1

You don't need to get a session to check that the connection is running.

If you try to access the database using invalid credentials you'll get a ORA-01017 error. If the connection is not available or the listener is running but the database is not mounted (i.e. it can't check the credentials even though it can connect) you'll get a different error code.

See also the Nagios check_oracle_v2 script for an example.

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

I had similar question. I discovered we could specify what failure code could be sent depending on the case:

sqlplus /nolog << EOF
 WHENEVER SQLERROR EXIT 50
 WHENEVER OSERROR EXIT 66
 connect /@${MISTERY_DB}
 exit;
EOF

Then to check the return code:

RET_CODE=$?
if [ ${RET_CODE} -eq 50 ] ; then
  echo "it was an SQLERROR"
fi
J. Chomel
  • 8,193
  • 15
  • 41
  • 69