21 June 2007

ORACLE_SID and TWO_TASK Environment Variables For Unix To Connect Oracle

There are two environment variables for Unix to connect an Oracle database.One of them is ORACLE_SID and the other one is TWO_TASK. ORACLE_SID is used for connecting a database which is installed to the same host. TWO_TASK is used for connecting an oracle database which is installed another host.
Normally, when you connect oracle via SQL*Plus you can specify a connect string such scott/tiger@PRODB. If this is specified, oracle uses tnsnames entry which is included in tnsnames.ora file. This kind of connection, uses listener and needs a running listener.
oracle@aurora:/usr/users/oracle> sqlplus scott/tiger@prodb
oracle@aurora:/usr/users/oracle> ps -ef | grep oracleprodb
oracle 351026 1 0.0 Jun 18 ?? 0:00.20 oracleprodb (LOCAL=NO)

If you do not specify connect string which is @PRODB suffix in given instance, uses BEQ adapter.
oracle@aurora:/usr/users/oracle> sqlplus scott/tiger@
oracle@aurora:/usr/users/oracle> ps -ef | grep oracleprodb
oracle 482176 482205 0.1 01:44:54 ?? 0:00.14 oracleprodb (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))


When clients connects to oracle they either use listener or not. Connection via ORACLE_SID does not require LISTENER process and uses BEQ SQL*Net protocol adapter which uses "Unix pipe"s ,not network hardware, to connect database whereas TWO_TASK not.

bash-3.00$ export ORACLE_SID=testpoc
bash-3.00$ export TWO_TASK=atest04.world
bash-3.00$ sqlplus test/test

SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:28:10 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;
NAME
---------
ATEST04

SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
bash-3.00$ echo $TWO_TASK
atest04.world
bash-3.00$ echo $ORACLE_SID
testpoc
bash-3.00$ export TWO_TASK=testpoc
bash-3.00$ export ORACLE_SID=testpoc
bash-3.00$ sqlplus test/test

SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:30:52 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;
NAME
---------
TESTPOC

SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
bash-3.00$ export ORACLE_SID=atest04.world
bash-3.00$ export TWO_TASK=testpoc
bash-3.00$ sqlplus test/test

SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:31:20 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;
NAME
---------
TESTPOC

SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
bash-3.00$ export TWO_TASK=atest04.world
bash-3.00$ export ORACLE_SID=atest04.world
bash-3.00$ sqlplus test/test

SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:31:49 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;
NAME
---------
ATEST04

SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
bash-3.00$


TWO_TASK has higher priority than ORACLE_SID
ORACLE_SID TWO_TASK SQLPLUS
____________ ____________ _______
testpoc atest04.world ATEST04
testpoc testpoc TESTPOC
atest04.world testpoc TESTPOC
atest04.world atest04.world ATEST04


Connection via BEQ, do not need a listener whereas w/out needs.
bash-3.00$ lsnrctl stop
LSNRCTL for Solaris: Version 10.1.0.4.0 - Production on 20-JUN-2007 12:53:05
Copyright (c) 1991, 2004, Oracle. All rights reserved.
Connectingto(DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC)))
The command completed successfully
bash-3.00$ sqlplus
test/test@testpoc
SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:53:28 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ERROR:
ORA-12541: TNS:no listener


bash-3.00$ export ORACLE_SID=testpoc
bash-3.00$ unset TWO_TASK
bash-3.00$ sqlplus test/test

SQL*Plus: Release 10.1.0.4.0 - Production on Wed Jun 20 12:55:51 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;
NAME
---------
TESTPOC

SQL>

No comments: