Thursday, January 7, 2010

Windows Service Not Starting Your Database?

If you are on a Windows Server you need to know about the Windows Services and the oradim utility.

On all versions of Windows the Oracle Database (and the Application Server) are started as Windows Services. This allows them to start in the background, normally owned by SYSTEM.

In an earlier Tip I discussed starting and stopping the Application Server 10g on Windows using a script to start and stop the component's service. The AS10g (and 9iAS) components can be started from the command line and will start the windows service however, the database (both the back end and the Metadata Repository) can not start without the database service running.

The Oracle Database must have a running service.

The Oracle database must attach itself to a running process. It is the Win Service that provides this process. Most DBAs think that the service is the database but that is not true. The service can start/stop the database but it can also be started without starting the database.

Most Problem with the Win Service for the Database involve the service starting but the database not starting.

If the Win service is set to automatically start when the server boots but the database does not start, you may have a improper registry setting or you may have a bad service.

1. Check Task Manager for the ORACLE.EXE process. If it is present, then the service started.

2. Check the Alert Log for the database. If the problem is not with the database, there will be no indication in the log that the database even tried to start.

3. Check the oradim.log in the $ORACLE_HOME/database directory for errors. Check the date on the log file as versions before 9i did not date/time stamp the entries.

If there are no errors in the logs then try and start the database.

C:> sqlplus "/ as sysdba"
connected to an idle instance
SQL> starup

If the database starts great, the problem is in the service.

To check the Win service:

Open the registry with regedit. Always back up the registry before making changes. Navigate to the key:

HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\oracle_home_name.

There will be a key called:

ORA_SID_AUTOSTART (SID is your database SID)

This key should be set to TRUE. If not the server starts but does not start the database. There is also an ORA_SID_SHUTDOWN which you want to be TRUE so that if the server is shut down the service will shutdown the database.

If you want to manually start the database set ORA_SID_AUTOSTART to FALSE. The service will start but not the database.

Test the service.

If the ORA_SID_AUTOSTART setting was the problem, change it to TRUE and then test the service by stopping and then restarting the service to see it the database automatically starts. If it does, then that fixed your problem......or maybe is didn't. Reboot the server to verify that the database will start automatically. Sometimes the service will work, only to fail again after a reboot. If the service fail after rebooting you need to recreate it. This is where the oradim utility comes in.
Deleting a Service
First delete or rename the oradim.log file.
Next delete the current service.
C:>oradim -delete -sid SID
SID is of course you database SID. This may take a while so check the services to insure that the OracleServiceSID is removed before proceeding.
Creating a new Service.
Again we use oradim to recreate the service. This entire command is on one line.
c:> oradim -new -sid SID -intpwd password -startmode AUTO -pfile c:\oracle\admin\SID\pfile\initSID.ora
This command does a lot and will take some time to complete (if startmode is set to AUTO it will start the database). It recreate the database password and sets the internal password to password. NOTE: oradim changes the internal password.
The startmode determines if the service starts the database when it starts. If set to AUTO then the key ORA_SID_AUTOSTART is set to TRUE. If startmode is MANUAL then the key is set to FALSE.
Check the oradim.log for errors. Finally, verify the service works as needed by starting and stopping it. Then test with a reboot. If the service fails try recreating it again.
Scripting the Database Start in Windows
The dbstart and dbstop shell scripts do not exist on Windows platforms. Consequently Oracle database startup and shutdown is implemented completely differently. The oradim utility is used on the Windows platform to perform these tasks.
C:\oracle9i\bin\oradim -startup -sid ORCL92 –usrpwd manager
-starttype SRVC,INST -pfile
C:\oracle9i\admin\ORCL92\pfile\init.ora
• startup – Indicates that the specified instance should be started.
• sid – The SID of the database to start.
• usrpwd – The password for the database user.
• starttype – Specifies whether to start the instance, the service, or both (SRVC, INST).

The following command can be used to shutdown the instance with oradim:

C:\oracle9i\bin\oradim -shutdown -sid ORCL92 -shutttype SRVC,INST
–shutmode A

Notice that no password is needed to perform this task.

If you can't get the service to function properly you are left with scripting the starting of the database. Recreate the service with the -startmode set to MANUAL. Then create a batch file as follows:
REM Wait for the server to start.
sleep 60
REM Start the database
%ORACLE_HOME%\bin\sqlplus -s "/ as sysdba" @startup.sql
exit
The startup.sql file
-- start the database
startup
exit
-Now schedule the batch file in the Windows Scheduler to run at startup.
-If this is part of you AS10g, you can implement the startup in the script used to start the application server components.