Sunday, May 15, 2011

New On Java 7

Following are some of the things new on Java 7. Taken from various java documentations.

Catching More Than One Type of Exception with One Exception Handler

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.

In the catch clause, specify the types of exceptions that block can handle, and separate each exception type with a vertical bar (|):http://www.blogger.com/img/blank.gif

catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.


Using Non-Reifiable Parameters with Varargs Methods

Generics in varargs
The compiler issues a warning "warning: [unchecked] unchecked generic array creation" when generics are passed as varargs. This warning could be suppressed with @SafeVarArgs annotation on the method, which would ensure that the method wouldn't perform any unsafe operation.
public static void main(String[] args) {
        compute(new HashMap(), 
new TreeMap());
       }

@SafeVarargs
    static void compute(Map... args) {
        }
}
Using Strings in switch Statements
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

In Java SE 7 and later, you can use a String object in the switch statement's expression.
public static int getMonthNumber(String month) {

int monthNumber = 0;

if (month == null) { return monthNumber; }

switch (month.toLowerCase()) {
case "january":    monthNumber =  1; break;
case "february":   monthNumber =  2; break;
case "march":      monthNumber =  3; break;
case "april":      monthNumber =  4; break;
case "may":        monthNumber =  5; break;
case "june":       monthNumber =  6; break;
case "july":       monthNumber =  7; break;
case "august":     monthNumber =  8; break;
case "september":  monthNumber =  9; break;
case "october":    monthNumber = 10; break;
case "november":   monthNumber = 11; break;
case "december":   monthNumber = 12; break;
default:           monthNumber =  0; break;
}

return monthNumber;
}
The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used.

Diamond syntax
Until Java 7, generic instances were created with
Map<Integer, String> map = new HashMap<Integer, String>();
In Java 7 only refernce requires type information and actual type could be without type information.
Map<Integer, String> map = new HashMap<>();

Binary literal and underscore in numeric literals
Binary values could be defined as
int number = 0b1100110; // decimal 102
Lengthy numeric values can be separated using underscores
long longnum = 28_06_86L;
long verylongnumber = 2334_7656_4503_3844L;
Automatic Resource Management
With the new Java 7 try block syntax, resources declared while defining the try block will automatically close when the control comes out of the try block.
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
while ((line = reader.readLine()) != null) {
         //do some work
       }
    } catch (IOException ex) {
 }


Monday, April 4, 2011

Get ASM Files Using LFTP

If a database has XML DB set and ftp port configured then lftp could be used to get files out of the ASM.
FTP port could be configured with
SQL> @$ORACLE_HOME/rdbms/admin/catxdbdbca.sql 7001 8001
7001 is the ftp port 
8001 is the http port
Listener status would display these ports
lsnrctl status
...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=hostname)(PORT=7001))(Presentation=FTP)(Session=RAW))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=hostname)(PORT=8001))(Presentation=HTTP)(Session=RAW))
...
To get a file for example an archive log file use
lftp rac4 -usystem,passwd -p7001 -e 'get /sys/asm/FLASH/RAC11G2/ARCHIVELOG/2011_04_02/thread_2_seq_472.390.747360023;exit;'
41125376 bytes transferred in 4 seconds (11.02M/s)
Here rac4 is the host on which the ASM instance is running, command could be executed from a remote node as well. Following -u is the username used to login into the system, in this case database's system user and comma separated by the password for system user. FTP port is specified with -p.

Without the exit at the end, after the file is transferred shell prompt will end up in the ftp prompt.

To remove the http and ftp ports get rid of the dispatcher parameter (as per 274508.1 Listener Issue: Removing XDB Handlers for HTTP and FTP Ports) or
conn / as sysdba
exec dbms_xdb.sethttpport(0);
exec dbms_xdb.setftpport(0);
alter system register;
More on Master Note for Oracle XML DB Protocols: FTP HTTP HTTPS WebDAV, APEX and Native Database Web Services [ID 1083991.1]

Apart from lftp ftp client such filezilla could be used to connect to the ftp port specified and get the files.

Friday, April 1, 2011

Enable Flashback On Standby

It is possible to enable flashback on a standby independent of the primary. (It's good to have flashback enabled both on primary and standby, if flashback IO is no concern).
Data guard environment created earlier is used here. Steps are similar to How to Enable Flashback for a RAC Database on ASM ( 819905.1)

To enable flashback on standby bring the stop log apply on standby (transport could also be stopped for the duration).
DGMGRL>  edit database rac11g2 set state='TRANSPORT-OFF';
Succeeded.
DGMGRL> edit database rac11g2s set state='APPLY-OFF';
Succeeded.
Trying to enable flashback while log apply is on will result in
SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-01153: an incompatible media recovery is active
If the standby is open in read only mode then shutdown and bring it up only one instance in mount mode as this is required to enable flashback.
srvctl stop database -d rac11g2s
srvctl start instance -d rac11g2s -i rac11g2s1 -o mount
Once in mount mode enable flashback on the standby
SQL> alter database flashback on;

Database altered.
Enable log apply and transport on
DGMGRL> edit database rac11g2 set state='TRANSPORT-ON';
Succeeded.
DGMGRL> edit database rac11g2s set state='APPLY-ON';
Succeeded.
Open the standby mode in read only if required. Check the flashback on status of the database
SQL> select flashback_on from v$database;

FLASHBACK_ON
------------
YES
Having flashback on standby is important especially to recover from open resetlogs situations in primary. If the standby has applied changes past the new resetlog scn and there's no flashback enabled on standby, only way to recover is to recreate the standby. If flashback was enabled on standby then it could be used to flashback to a scn prior to resetlogs and continue to use the standby from then onwards.