Changing the context root of your java web app

Problem: In Eclipse, I made a copy of an existing web app and tried to deploy on my local tomat. This failed as it had the same context root (e.g. http://localhost.8080/webapp1) as the existing project I copied from. To fix this, I changed the context root on the new project to something new. I did this by selecting project properties -> Web Project Settings -> Context Root. From here I was able to change the webapp context root to something else (e.g. http://localhost.8080/webapp2), which should allow both projects to be deployed in parallel. While this seemed to be all that was needed, I received the following error when trying to actually deploy the app.

context_error

Solution:
To resolve this issue, tomcat needs to be issued with the ‘clean’ command in order to pick up and activate the changes to the context root of deployed applications. To do this…
1) Stop tomcat.
2) right click on your server from within the servers window and select ‘clean’.
tomcat_clean
3) Click OK when asked to republish from scratch.
4) Click YES to when asked if you want to update the context root of the module you just changed.
5) The server should now automatically restart. You should now be able to successfully navigate to both applications as they are deployed on different context roots.

Exception hanlding and logging of pl/SQL

pl/SQL Logging
During a batch process or the execution of pl/SQL, you will want to log the output and the success or failure of the process.

Typically, I always find it good practice to output some simple information such as the start and end date/time of the process as well as any errors that may occur. This is really handy if you are wanting to look at how long a process takes and will give you a feel for if something ‘went wrong’ while running the script.

If you run a script as a one off, you will see the output of your code (package, procedure, anonomous block) in the output from your IDE such as TOAD, SQL developer or SQL*Plus.

Example:

set serveroutput on

... your code

or you can redirect it to a file (see below)

Example:

set serveroutput on
spool log.txt

... your code

spool off

If you run a batch process using crontab or some other scheduler, use this to redirect your output into a file. If you do this, it is always a good idea to time stamp the file.

Exception handling
In your batch pl/SQL applications, you will need to have some exception handling.

If anything, you will want to catch some specific errors as well as others.

In my examples below, I am catching a NO_DATA_FOUND exception and outputting a different error to when any other exception occurs.

Example:

BEGIN 

	DBMS_OUTPUT.PUT_LINE('Batch process started - '||SYSDATE);
	
	... your pl/SQL block that does stuff...

	COMMIT; --commit changes 
	DBMS_OUTPUT.PUT_LINE('Batch process ended - '||SYSDATE);
	
EXCEPTION
   WHEN NO_DATA_FOUND THEN
      ROLLBACK;
	  
      DBMS_OUTPUT.PUT_LINE('No data was found - '||SQLCODE||' -ERROR- '||SQLERRM);

EXCEPTION
   WHEN OTHERS THEN
	  ROLLBACK;
      DBMS_OUTPUT.PUT_LINE('An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;

Raising your own exceptions
Another option is when you come across a condition in your code where you want to throw an exception. A really good example of this is if a variable ends up with a negative number you may want to raise an exception rather than let the program continue on.

Example:

BEGIN

	IF input > 0 THEN
		... your pl/SQL block that does stuff...
	ELSIF input = -1
		--Raise no data found exception if 
		RAISE NO_DATA_FOUND; 
	
	ELSE
		--Something wrong has happened. Raise an exception. 
		RAISE;
	END IF:

	EXCEPTION
	   WHEN NO_DATA_FOUND THEN
		  DBMS_OUTPUT.PUT_LINE('Please enter a correct paramater and try again.');

	EXCEPTION
	   WHEN OTHERS THEN
		  DBMS_OUTPUT.PUT_LINE('An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
	END;
END;

Creating your own custom exceptions
It is also possible to create your own exceptions and even allocate them to their own error number within the user-specified range of between -20000 – -20999. All other error numbers are reallocated to standard oracle errors. While this is certainly possible, I’ve never found a real life scenario where I have wanted to create my own exceptions as the Oracle keywords and mapping to error numbers have been sufficient for most needs. (hence why I haven’t included an example)