Saturday, April 16, 2011

Calling Java Methods in Oracle Database

With loadjava utility we can call java methods from the oracle database.

public class Hello
{
  public static String world()
  {
    return "Hello world";
  }
}
Save the file as a Hello.java file
Compile it using the standard Java compiler (it is better to compile it with oracle's own compiler)

Error while calling function if the class is not loaded successfully.
ORA-29541: Class MYTEST.Hello could not be resolved.

Try Loading the class on the server using the loadjava tool. You must specify the user name and password. Run the loadjava tool as follows.

D:\oracle\ora92\jdk\bin>
loadjava -resolve -verbose -user mytestuser/mytestpasswd "Hello.class"

arguments: '-resolve' '-verbose' '-user' 'mytestuser/mytestpasswd' 'Hello.class'
creating : class Hello
loading  : class Hello
resolving: class Hello

the above stmts shows it is successfully loaded.

In SQL*Plus, connect to the database and define a top-level call specification for Hello.world() as follows:

SQL> CREATE OR REPLACE FUNCTION helloworld RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'Hello.world () return java.lang.String';
     /
Function created

Call the stored procedure, as follows:

SQL> VARIABLE myString VARCHAR2(20);
SQL> CALL helloworld() INTO :myString;
Call completed.
SQL> PRINT myString

MYSTRING
---------------------------------------
Hello world

Sunday, April 3, 2011

TableSpace Check in Oracle

select file_name, bytes, autoextensible, maxbytes from dba_data_files;

SELECT tablespace_name FROM dba_tablespaces;

SELECT file_name, tablespace_name, bytes, blocks, autoextensible, increment_by FROM dba_data_files;

SELECT file_name, tablespace_name, bytes, blocks, autoextensible, increment_by FROM dba_temp_files;

SELECT tablespace_name, status FROM dba_tablespaces;

SELECT dd.tablespace_name tablespace_name, dd.file_name file_name, dd.bytes/1024 TABLESPACE_KB, SUM(fs.bytes)/1024 KBYTES_FREE, MAX(fs.bytes)/1024 NEXT_FREE
FROM sys.dba_free_space fs, sys.dba_data_files dd
WHERE dd.tablespace_name = fs.tablespace_name
AND dd.file_id = fs.file_id
GROUP BY dd.tablespace_name, dd.file_name, dd.bytes/1024
ORDER BY dd.tablespace_name, dd.file_name;

clear breaks
set linesize 132
set pagesize 60
break on tablespace_name skip 1
col tablespace_name format a15
col file_name format a50
col tablespace_kb heading 'TABLESPACE|TOTAL KB'
col kbytes_free heading 'TOTAL FREE|KBYTES'

SELECT dd.tablespace_name tablespace_name, dd.file_name file_name, dd.bytes/1024 TABLESPACE_KB, SUM(fs.bytes)/1024 KBYTES_FREE, MAX(fs.bytes)/1024 NEXT_FREE
FROM sys.dba_free_space fs, sys.dba_data_files dd
WHERE dd.tablespace_name = fs.tablespace_name
AND dd.file_id = fs.file_id
GROUP BY dd.tablespace_name, dd.file_name, dd.bytes/1024
ORDER BY dd.tablespace_name, dd.file_name;

SELECT dd.file_name file_name, dd.bytes/1024 TABLESPACE_KB, SUM(fs.bytes)/1024 KBYTES_FREE, MAX(fs.bytes)/1024 NEXT_FREE
FROM sys.dba_free_space fs, sys.dba_data_files dd
WHERE dd.tablespace_name = fs.tablespace_name
AND dd.file_id = fs.file_id
GROUP BY dd.tablespace_name, dd.file_name, dd.bytes/1024
ORDER BY dd.tablespace_name, dd.file_name;

clear breaks
set linesize 220
set pagesize 60
break on autoextensible skip 1
select tablespace_name as TablespaceName, bytes/1024/1024 as Allocated_Size_MB,
user_bytes/1024/1024 as Space_Used_MB,increment_by as Increment_By_Bytes
from dba_data_files;

For Help & Reference, kindly Refer.
http://psoug.org/reference/tablespaces.html

Friday, April 1, 2011

Rotate Apache Tomcat std Logs using log4j

Apache Tomcat std_out logs can be rotated by using log4j functionality based on its file size as mentioned below.

Modify log4j.properties

log4j.rootCategory=DEBUG,stdout, Sample

#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#### Second appender writes to a file
log4j.appender.Sample=org.apache.log4j.RollingFileAppender
log4j.appender.Sample.File=D:/ApacheSoftwareFoundation/Tomcat5.5/logs/SampleSiteLogs.txt
### Control the maximum log file size
### Archive log files
### (one backup file here Attribute MaxBackupIndex is where we can define max no of files to be written)
log4j.appender.Sample.MaxFileSize=100KB
log4j.appender.Sample.MaxBackupIndex=5
log4j.appender.Sample.layout=org.apache.log4j.PatternLayout
log4j.appender.Sample.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %p %t %c - %m%n