Thursday, November 6, 2014

Java: what is your default timezone?

cat MyTimezone.java

import java.util.TimeZone;

public class  MyTimezone {
    public static void main(String[] args) throws Exception {
        TimeZone timeZone = TimeZone.getDefault();
        System.out.println(timeZone);
    }
}


javac MyTimezone.java

java MyTimezone

on one machine I have:

sun.util.calendar.ZoneInfo[id="Europe/Zurich",offset=3600000,dstSavings=3600000,useDaylight=true,
transitions=119,lastRule=java.util.SimpleTimeZone[id=Europe/Zurich,offset=3600000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,
endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]


on another machine I have:

sun.util.calendar.ZoneInfo[id="Europe/Vaduz",offset=3600000,dstSavings=3600000,useDaylight=true,
transitions=119,lastRule=java.util.SimpleTimeZone[id=Europe/Vaduz,offset=3600000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,
endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]


The second machine has timezone "Europe/Vaduz", and the Oracle DB doesn't recognize it:
SELECT * FROM V$TIMEZONE_NAMES where TZNAME = 'Europe/Vaduz';
nothing!

This explains why your JVM might get in trouble with the Oracle DB and you have to set explicitly the timezone yourself...

See also http://www.javamonamour.org/2014/11/ora-01882-timezone-region-not-found.html

Funnily:
diff /usr/share/zoneinfo/Europe/Vaduz /etc/localtime

diff /usr/share/zoneinfo/Europe/Zurich /etc/localtime


these 2 files are identical.

How does Java determine its default timezone from the Linux machine it's running on?

on RHEL, use redhat-config-date system-config-time timeconfig tzselect

Wednesday, November 5, 2014

ORA-01882: timezone region not found

After applying package update on Linux RHEL, we get on all Datasources:

ORA-01882: timezone region not found

First let's determine on which timezone our server is:

grep "ZONE=" /etc/sysconfig/clock

ZONE="Europe/Zurich"

let's open a connection to our DB and check:

SELECT * FROM V$TIMEZONE_NAMES where TZNAME = 'Europe/Zurich';

Europe/Zurich LMT

Europe/Zurich BMT

Europe/Zurich CET

Europe/Zurich CEST



I think CET is pretty decent...

I can fix the issue in 2 ways:

either I do "export TZ=CET" in my "soa" Linux user profile (.bashrc)

or I set the property in DOMAIN_HOME/bin/setDomainEnv.sh

-Duser.timezone=CET

Either will work.

Small test harness:

cat DBPing.java


import java.sql.DriverManager;
import java.sql.SQLException;

public class DBPing {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                System.out.println("length " + args.length);
                String user = args[0];
                String password = args[1];
                String url = args[2];
                String now = new java.util.Date().toString();
                System.out.println(now + " user= " + user + " password=" + password + " url=" + url);
                java.sql.Connection conn = DriverManager.getConnection(url, user, password);
                System.out.println("ok");
        }
}



javac DBPing.java

java -cp /opt/oracle/fmw11_1_1_5/wlserver_10.3/server/lib/ojdbc6.jar:. DBPing acme ********** jdbc:oracle:thin:@mydb.acme.com:1551/s_gr00

length 3
Wed Nov 05 15:41:24 CET 2014 user= acme password=bla
url=jdbc:oracle:thin:@mydb.acme.com:1551/s_gr00
Exception in thread "main" java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1
ORA-01882: timezone region not found

        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
        at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:573)
        at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:431)
        at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
        at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
        at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:366)
        at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:752)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:366)
        at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:538)
        at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:228)
        at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at DBPing.main(DBPing.java:13)


second edition:

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.TimeZone;

public class DBPing {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
                TimeZone timeZone = TimeZone.getTimeZone("Europe/Zurich");
                TimeZone.setDefault(timeZone);
                Class.forName("oracle.jdbc.driver.OracleDriver");
                System.out.println("length " + args.length);
                String user = args[0];
                String password = args[1];
                String url = args[2];
                String now = new java.util.Date().toString();
                System.out.println(now + " user= " + user + " password=" + password + " url=" + url);
                java.sql.Connection conn = DriverManager.getConnection(url, user, password);
                System.out.println("ok");
        }
}




by setting the timezone we fix the error...

Setting "oracle.jdbc.timezoneAsRegion=false" in the WebLogic Datasource properties also does the job. This probably is the least impact solution, as it affects only the DB...
CONNECTION_PROPERTY_TIMEZONE_AS_REGION

static final java.lang.String CONNECTION_PROPERTY_TIMEZONE_AS_REGION

Use JVM default timezone as specified rather than convert to a GMT offset. Default is true. 



Tuesday, November 4, 2014

Book: A Splendid Exchange: How Trade Shaped the World



This is a very enjoyable book, showing how trade shaped history, from Phoenician times to http://en.wikipedia.org/wiki/Zheng_He, Marco Polo, the British pirates and the http://en.wikipedia.org/wiki/Dutch_East_India_Company...

The only drawback is that it covers too many subjects, and finally none in a very specialized form. And of course his positive view of modern trade is totally delusional, and doesn't take into account the immense toll on environment.