Wednesday, June 30, 2010

Are u sure about Oracle ROWNUM ?

http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html

A simple implementation with JPA  on oracle dbms

    /*
     I had to make some improvements in query with oracle bec.of performance of the default jpa implementation.
     */
    public User[] getUsersInPage (int startIndex, int stopIndex) throws Exception {

        EntityManager em = createEntityManager ();
        List ret = new ArrayList();
        try {
            if (isOracle()){
                Query selectQuery = em.createNativeQuery ("SELECT * FROM (select a.*,rownum rnum from (select * from provisioning_subsys.VIP_USERS) a where ROWNUM <= " +
                    stopIndex + ") where rnum >=" + startIndex + "", User.class);
                ret = (List) selectQuery.getResultList ();
            }else{
                String qry = "select u from User u";
                Query selectQuery = em.createQuery (qry);
                selectQuery.setMaxResults (stopIndex - startIndex);
                selectQuery.setFirstResult (startIndex);
                ret = (List) selectQuery.getResultList ();
            }
        } finally {
            em.close ();
        }

        return getUserArrayFromList (ret);
    }

    private boolean isOracle () {
        if (emf instanceof EntityManagerFactoryImpl) {
            Properties jpaProperties = ((EntityManagerFactoryImpl) emf).getProperties ();
            String dbms = (String) jpaProperties.get ("Platform");
            if (dbms.indexOf ("Oracle") != -1) {
                return true;
            } else return false;
        }
        return false;
    }

Tuesday, June 15, 2010

CyclicBarrier Test

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTester {
    private static final int numOfThreads = 5;
   
    public static void main (String[] args) {
        startTasks ();
    }


    private static void startTasks () {
        try {
            CyclicBarrier barrier = buildBarrier();
            startThreads(barrier);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

 
    private static CyclicBarrier buildBarrier () {
        CyclicBarrier barrier = new CyclicBarrier (numOfThreads, new Runnable () {
            public void run () {
                Logger.log (LogLevel.LOG, " -- barrier action done");
            }
        });
        return barrier;
    }
   
    private static void startThreads (CyclicBarrier barrier) {
        Thread[] threads = new Thread[numOfThreads];
        for (int i = 0; i < threads.length; i++) {
            TaskExecuter executor = new TaskExecuter ();
            executor.setName ("CleanupThread-" + i);
            executor.setBarrier (barrier);
            threads[i] = executor;
            threads[i].start ();
        }
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join ();
            } catch (InterruptedException ignore) {
            }
        }
    }

}









import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.BrokenBarrierException;

public class TaskExecuter extends Thread {
    private CyclicBarrier barrier ;

    public void setBarrier (CyclicBarrier barrier){
        this.barrier = barrier;
    }

    public void run () {
            Logger.log (LogLevel.LOG, "   --- {0} is started processing....", this.getName ());
            try {
                doAction ();
            } catch (Exception e) {
                Logger.log(LogLevel.EXCEPTION,"[{0}]",e);
            }
            Logger.log (LogLevel.LOG, "   --- {0} is finished processing....", this.getName ());
    }

    private void doAction () {
            try {
                //do your work
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                waitForOthers ();
            }
    }

    private void waitForOthers () {
            try {
                Logger.log (LogLevel.LOG, "{0} is waiting for the others, currentwaiters count :{1}",
                    getName (), barrier.getNumberWaiting ());
                barrier.await (100000L, TimeUnit.MILLISECONDS);
                Logger.log (LogLevel.LOG, "{0} is out of the barrier ", getName ());
            } catch (BrokenBarrierException e) {
                barrier.reset();
                Logger.log (LogLevel.WARNING, " -- resetting the barrier for the exception : {0}", e);
            } catch (InterruptedException ex) {
                Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
            } catch (TimeoutException ex) {
                Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
            }
    }

}

introduction-to-google-guice

introduction-to-google-guice

update date column in mysql

update `myDb`.`myTable` set myColumn = (DATE_SUB(sysdate(), INTERVAL 1 DAY));