@Override
    public synchronized void updateServerLease(String recoveryIdentity, String recoveryGroup, boolean isServerStartup) throws Exception
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "updateServerLease", new java.lang.Object[] { recoveryIdentity, recoveryGroup, isServerStartup, this });

        Connection conn = null;
        Exception currentEx = null;
        Statement lockingStmt = null;
        PreparedStatement updateStmt = null;
        ResultSet lockingRS = null;

        if (tc.isDebugEnabled())
            Tr.debug(tc, "Work with recoveryIdentity - ", recoveryIdentity);

        // Reset a null recoveryGroup to an empty string
        if (recoveryGroup == null)
            recoveryGroup = "";

        try
        {
            // Have we acquired the reference to the DataSource yet?
            if (_theDS == null)
            {
                _theDS = getDataSourceFromProperties();
                // We've looked up the DS, so now we can get a JDBC connection
                if (_theDS != null)
                {
                    conn = getConnection(_theDS);
                }
            }
            else
            {
                // Try and get a new connection
                conn = _theDS.getConnection();
            }

            // If we were unable to get a connection, throw an exception
            if (conn == null)
            {
                if (tc.isEntryEnabled())
                    Tr.exit(tc, "updateServerLease", "Null connection InternalLogException");
                throw new InternalLogException("Failed to get JDBC Connection", null);
            }

            if (tc.isDebugEnabled())
                Tr.debug(tc, "Set autocommit FALSE on the connection");
            conn.setAutoCommit(false);

            // We can go ahead and write to the Database
            lockingStmt = conn.createStatement();
            //lockingStmt.setCursorName("UPDATESTATEMENT");
            boolean newTable = true;

            try
            {
                String queryString = "SELECT LEASE_TIME, LEASE_OWNER" +
                                     " FROM " + _leaseTableName +
                                     " WHERE SERVER_IDENTITY='" + recoveryIdentity + "' FOR UPDATE OF LEASE_TIME";
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Attempt to select the row for UPDATE using - " + queryString);
                lockingRS = lockingStmt.executeQuery(queryString);

                newTable = false;
            } catch (Exception e)
            {
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Query failed with exception: " + e);
                currentEx = e;
            } // eof Exception e block

            if (currentEx != null)
            {
                if (isServerStartup)
                {
                    // Perhaps we couldn't find the table ... so attempt to create it
                    synchronized (_CreateTableLock) // Guard against trying to create a table from multiple threads
                    {
                        try
                        {
                            Tr.audit(tc, "WTRN0108I: Create Shared Lease Table");
                            createLeaseTable(conn);

                            conn.commit();

                            newTable = true;
                        } catch (Exception ine)
                        {
                            if (tc.isDebugEnabled())
                                Tr.debug(tc, "Table Creation failed with exception: " + ine);
                            // Set the current exception to ine
                            throw ine;
                        }
                    } // eof synchronize block
                } // eof isServerStartup
                else
                {
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Lease select update failed with exception: " + currentEx);
                    // Set the current exception to ine
                    throw currentEx;
                }
            }

            boolean needInsert = true;
            if (!newTable)
            {
                // We havent just created the table, see if we have a row to update
                if (lockingRS.next())
                {
                    // We found the Server row
                    long storedLease = lockingRS.getLong(1);
                    String storedLeaseOwner = lockingRS.getString(2);
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Acquired lock row, stored lease value is: " + storedLease + ", stored owner is: " + storedLeaseOwner);

                    // If this is startup, check whether lease has expired
                    if (isServerStartup)
                    {
                        PeerLeaseData pld = new PeerLeaseData(recoveryIdentity, storedLease, _leaseTimeout);
                        if (pld.isExpired())
                        {
                            if (tc.isDebugEnabled())
                                Tr.debug(tc, "Lease has expired, we should update lease and recover");
                            // We have acquired the lock. If any other server is also trying to peer recover then
                            // they can fail. To make this happen, we'll update the lease time ourselves.
                        }
                        else
                        {
                            // Lease has not expired, are we the owner or is some other server peer recovering
                            // If the latter, then we should barf
                            if (!storedLeaseOwner.equals(recoveryIdentity))
                            {
                                if (tc.isDebugEnabled())
                                    Tr.debug(tc, "A peer is recovering, we will fail our recovery and exit");
                                Object[] errorObject = new Object[] { recoveryIdentity };
                                RecoveryFailedException rex = new RecoveryFailedException();
                                throw rex;
                            }
                        }
                    }

                    // Construct the UPDATE string
                    String updateString = "UPDATE " + _leaseTableName +
                                          " SET LEASE_TIME = ?, RECOVERY_GROUP = ?, LEASE_OWNER = ?" +
                                          " WHERE SERVER_IDENTITY='" + recoveryIdentity + "'";
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "update lease for " + recoveryIdentity);

                    updateStmt = conn.prepareStatement(updateString);
                    // Set the Lease_time to the current time
                    //TODO:
                    long fir1 = System.currentTimeMillis();
                    updateStmt.setLong(1, fir1);
                    updateStmt.setString(2, recoveryGroup);
                    updateStmt.setString(3, recoveryIdentity);
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Ready to UPDATE using string - " + updateString + " and time: " + fir1);
                    Tr.audit(tc, "WTRN0108I: Update Lease for server with recovery identity " + recoveryIdentity);

                    int ret = updateStmt.executeUpdate();

                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Have updated Server row with return: " + ret);
                    needInsert = false;
                }
                else
                {
                    // We didn't find the row in the table
                    //TODO: Should we insert a new one?
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Could not find row");
                }
            }

            // Either a new table or we couldn't find the row for our server. Insert it.
            if (needInsert)
            {
                // Insert a new row into the lease table
                insertNewLease(recoveryIdentity, recoveryGroup, conn);
            }
            if (tc.isDebugEnabled())
                Tr.debug(tc, "COMMIT the change");
            conn.commit();
        }
        // Catch and report an SQLException. In the finally block we'll determine whether the condition is transient or not.
        catch (SQLException sqlex)
        {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Lease update failed with exception: " + sqlex);
        } finally
        {
            if (lockingRS != null && !lockingRS.isClosed())
                lockingRS.close();
            if (lockingStmt != null && !lockingStmt.isClosed())
                lockingStmt.close();
            if (updateStmt != null && !updateStmt.isClosed())
                updateStmt.close();
            if (conn != null)
                conn.close();

        }

        if (tc.isEntryEnabled())
            Tr.exit(tc, "updateServerLease");
    }
@ Override public synchronized void updateServerLease ( String recoveryIdentity , String recoveryGroup , boolean isServerStartup ) throws Exception { if ( tc . isEntryEnabled ( ) ) Tr . entry ( tc , "updateServerLease" , new java . lang . Object [ ] { recoveryIdentity , recoveryGroup , isServerStartup , this } ) ; Connection conn = null ; Exception currentEx = null ; Statement lockingStmt = null ; PreparedStatement updateStmt = null ; ResultSet lockingRS = null ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Work with recoveryIdentity - " , recoveryIdentity ) ; if ( recoveryGroup == null ) recoveryGroup = "" ; try { if ( _theDS == null ) { _theDS = getDataSourceFromProperties ( ) ; if ( _theDS != null ) { conn = getConnection ( _theDS ) ; } } else { conn = _theDS . getConnection ( ) ; } if ( conn == null ) { if ( tc . isEntryEnabled ( ) ) Tr . exit ( tc , "updateServerLease" , "Null connection InternalLogException" ) ; throw new InternalLogException ( "Failed to get JDBC Connection" , null ) ; } if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Set autocommit FALSE on the connection" ) ; conn . setAutoCommit ( false ) ; lockingStmt = conn . createStatement ( ) ; boolean newTable = true ; try { String queryString = "SELECT LEASE_TIME, LEASE_OWNER" + " FROM " + _leaseTableName + " WHERE SERVER_IDENTITY='" + recoveryIdentity + "' FOR UPDATE OF LEASE_TIME" ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Attempt to select the row for UPDATE using - " + queryString ) ; lockingRS = lockingStmt . executeQuery ( queryString ) ; newTable = false ; } catch ( Exception e ) { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Query failed with exception: " + e ) ; currentEx = e ; } if ( currentEx != null ) { if ( isServerStartup ) { synchronized ( _CreateTableLock ) { try { Tr . audit ( tc , "WTRN0108I: Create Shared Lease Table" ) ; createLeaseTable ( conn ) ; conn . commit ( ) ; newTable = true ; } catch ( Exception ine ) { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Table Creation failed with exception: " + ine ) ; throw ine ; } } } else { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Lease select update failed with exception: " + currentEx ) ; throw currentEx ; } } boolean needInsert = true ; if ( ! newTable ) { if ( lockingRS . next ( ) ) { long storedLease = lockingRS . getLong ( 1 ) ; String storedLeaseOwner = lockingRS . getString ( 2 ) ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Acquired lock row, stored lease value is: " + storedLease + ", stored owner is: " + storedLeaseOwner ) ; if ( isServerStartup ) { PeerLeaseData pld = new PeerLeaseData ( recoveryIdentity , storedLease , _leaseTimeout ) ; if ( pld . isExpired ( ) ) { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Lease has expired, we should update lease and recover" ) ; } else { if ( ! storedLeaseOwner . equals ( recoveryIdentity ) ) { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "A peer is recovering, we will fail our recovery and exit" ) ; Object [ ] errorObject = new Object [ ] { recoveryIdentity } ; RecoveryFailedException rex = new RecoveryFailedException ( ) ; throw rex ; } } } String updateString = "UPDATE " + _leaseTableName + " SET LEASE_TIME = ?, RECOVERY_GROUP = ?, LEASE_OWNER = ?" + " WHERE SERVER_IDENTITY='" + recoveryIdentity + "'" ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "update lease for " + recoveryIdentity ) ; updateStmt = conn . prepareStatement ( updateString ) ; long fir1 = System . currentTimeMillis ( ) ; updateStmt . setLong ( 1 , fir1 ) ; updateStmt . setString ( 2 , recoveryGroup ) ; updateStmt . setString ( 3 , recoveryIdentity ) ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Ready to UPDATE using string - " + updateString + " and time: " + fir1 ) ; Tr . audit ( tc , "WTRN0108I: Update Lease for server with recovery identity " + recoveryIdentity ) ; int ret = updateStmt . executeUpdate ( ) ; if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Have updated Server row with return: " + ret ) ; needInsert = false ; } else { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Could not find row" ) ; } } if ( needInsert ) { insertNewLease ( recoveryIdentity , recoveryGroup , conn ) ; } if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "COMMIT the change" ) ; conn . commit ( ) ; } catch ( SQLException sqlex ) { if ( tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Lease update failed with exception: " + sqlex ) ; } finally { if ( lockingRS != null && ! lockingRS . isClosed ( ) ) lockingRS . close ( ) ; if ( lockingStmt != null && ! lockingStmt . isClosed ( ) ) lockingStmt . close ( ) ; if ( updateStmt != null && ! updateStmt . isClosed ( ) ) updateStmt . close ( ) ; if ( conn != null ) conn . close ( ) ; } if ( tc . isEntryEnabled ( ) ) Tr . exit ( tc , "updateServerLease" ) ; }
Will update the lease for an existing server in the table. Or insert a new row if required.

@param recoveryIdentity
@param leaseTime
@throws Exception