Wednesday, July 27, 2011

How to create Auto Generated Transaction ID in java ( Unique key )



Note : This auto generated Transaction unique key is in this format : eg : ( DASARI_1212121212  )
Note : where DASARI IS PREFIX AND 1212121212 IS DateTime in Milliseconds.

Instance Variables :

   private static long m_nMaxTID = 1000;
    private static long m_nMinTID = 0;
    private static long m_iCount = 0;
    private static String m_strTmp = "000";
    private static int m_nTmpLen = 3;





public static synchronized String generateTID(String strPrefix) {
        strPrefix = strPrefix + "_" + (new java.util.Date()).getTime();
        System.out.println("");
        String strSuf = m_strTmp + m_iCount;
        m_iCount++;
        if (m_iCount >= m_nMaxTID) {
            m_iCount = m_nMinTID;
        }
        return strPrefix + strSuf.substring(strSuf.length() - m_nTmpLen);
    }

Note : simply call String transactionID=generateTID("DASARI");

Cheers
Dasari

No comments:

Post a Comment