CSS

Thursday, May 17, 2012

DB2 Converting YYYYMMDD to timestamp

So my company has a iSeries server that includes a legacy application that stored dates as YYYYMMDD numeric(8,0). Time are stored as HHMM numeric(4,0). I have finally been able to create a function that take these 2 types of columns and return a timestamp.

 On of the cool things about the iSeries is that a data file that is written to my a legacy RPG program can be munipultated using SQL. From a client like JDBC or some DB2 client those file look just like a table. This is why the legacy applications did not store the date in a TIMESTAMP column. below is the function.

CREATE FUNCTION COMMON.TIMESTAMP_FROM_YFU (
YFUDATE NUMERIC(8, 0) ,
YFUTIME NUMERIC(4, 0) )
RETURNS TIMESTAMP  
LANGUAGE SQL
SPECIFIC COMMON.TIMESTAMP_FROM_YFU
DETERMINISTIC
CONTAINS SQL
RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION
SET OPTION  ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *NONE ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX  
RETURN TIMESTAMP_FORMAT ( VARCHAR ( YFUDATE ) || DIGITS ( YFUTIME ) , 'YYYYMMDDHH24MI' )  ;
 
COMMENT ON SPECIFIC FUNCTION COMMON.TIMESTAMP_FROM_YFU
IS 'Timestamp from YFU Date and time' ;

Monday, May 14, 2012

Death to a Deadlock

One of the systems I have been working on has has had a deadlock that happens intermittently for the last 6 years.  Well today I finally found it.  We have a thread that runs in our application that times how long a web request takes.  If the request takes more then 30 minuets it assumes a deadlock and kills the application.  The thread also send an email to the programmers.  Below is the code I inserted in the monitoring thread code that allowed me to find the deadlock.



public static StringBuffer getDeadTraces(StringBuffer errorStringBuff) {
        StringBuffer deadtraces = new StringBuffer();
	try {
	    long[] ids = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
	    if(null != ids && ids.length > 0) {
	         Map map = Thread.getAllStackTraces();
		 Set set = map.entrySet();
		 for (long id : ids) {
		    for (Entry entry : set) {
			 Thread thread = entry.getKey();
			if (thread.getId() == id) {
				deadtraces.append("\n===================  ");
				deadtraces.append(thread.getName());
				StringBufferUtils.append(deadtraces, "  ===================","\n");
							
				for (StackTraceElement el : entry.getValue()) {
					StringBufferUtils.append(deadtraces, el.toString(),"\n");                    						
				}												
			}
		    }				 
	         }
	    }
	} catch (Exception e) {
		errorStringBuff.append("Error trying to create a snapshot: " + 
		e.getMessage() + org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e));
	}
	return deadtraces;
}