Tuesday 15 April 2014

Check locked Objects in Oracle database


LOCKED DATABASE OBJECTS

SELECT l.session_id || ',' || v.serial# sid_serial,       
       l.ORACLE_USERNAME ora_user,       
       o.object_name,       
       o.object_type,       
       DECODE(l.locked_mode,              
              0,
              'None',              
              1,
              'Null',              
              2,
              'Row-S (SS)',              
              3,
              'Row-X (SX)',              
              4,
              'Share',              
              5,
              'S/Row-X (SSX)',              
              6,
              'Exclusive',              
              TO_CHAR(l.locked_mode)              
              ) lock_mode,       
       o.status,       
       to_char(o.last_ddl_time, 'dd.mm.yy') last_ddl
  FROM dba_objects o, gv$locked_object l, v$session v
 WHERE o.object_id = l.object_id      
   and l.SESSION_ID = v.sid;


select a.sid, a.serial#, OBJECT_NAME , a.*
  from v$session a, v$locked_object b, dba_objects c
 where b.object_id = c.object_id
   and a.sid = b.session_id;
 
ALTER SYSTEM KILL SESSION 'sid,serial#';



SELECT T.*, sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.BIND_DATA
  FROM (select c.owner,
               c.object_name,
               c.object_type,
               b.sid,
               b.serial#,
               b.status,
               a.ORACLE_USERNAME,
               b.osuser,
               b.machine,
               DECODE(a.locked_mode,
                      0,
                      'None',
                      1,
                      'Null',
                      2,
                      'Row-S (SS)',
                      3,
                      'Row-X (SX)',
                      4,
                      'Share',
                      5,
                      'S/Row-X (SSX)',
                      6,
                      'Exclusive',
                      TO_CHAR(a.locked_mode)) alock_mode,
               b.STATUS SESSION_STATUS,
               b.LOGON_TIME,
               b.SQL_HASH_VALUE
          from v$locked_object a, v$session b, dba_objects c
         where b.sid = a.session_id
           and a.object_id = c.object_id) T
  left join v$sqlarea sa
    ON sa.HASH_VALUE = t.SQL_HASH_VALUE;
 


select a.sid, a.serial#, OBJECT_NAME, a.LOGON_TIME
  from v$session a, v$locked_object b, dba_objects c
 where b.object_id = c.object_id
   and a.sid = b.session_id;
    
    
SELECT SID, SQL_ID, USERNAME, BLOCKING_SESSION
  FROM v$session
 WHERE BLOCKING_SESSION IS NOT NULL;
  
  
 SELECT l1.sid || ' is blocking ' || l2.sid blocking_sessions
  FROM v$lock l1, v$lock l2
 WHERE l1.block = 1
   AND l2.request > 0
   AND l1.id1 = l2.id1
   AND l1.id2 = l2.id2;

SELECT s1.username || '@' || s1.machine || ' ( SID=' || s1.sid ||
       ' )  is blocking ' || s2.username || '@' || s2.machine || ' ( SID=' ||
       s2.sid || ' ) ' AS blocking_status
  FROM v$lock l1, v$session s1, v$lock l2, v$session s2
 WHERE s1.sid = l1.sid
   AND s2.sid = l2.sid
   AND l1.BLOCK = 1
   AND l2.request > 0
   AND l1.id1 = l2.id1
   AND l1.id2 = l2.id2;
    

select * from v$lock where type='TX' and request>0;
select * from v$lock where type='TX' and lmode>0;



Reference:

Wednesday 5 March 2014

Java Socket Example: Proxy server

This tool can be used for intercept services request and redirect or return error message for specified sources. Basically, reading and writing in same socket in java.


import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProxyServerTest extends Thread {

    final static Logger logger = Logger.getLogger(ProxyServerTest.class);

    private ServerSocket serverSocket;
    private String wsServer;
    private int wsServerPort;
    private List ipList = new ArrayList();


    public ProxyServerTest(String hostName, int port, int timeout, String wsServer, int wsServerPort, List ipList) throws IOException {
        InetAddress ia = InetAddress.getByName(hostName);
        serverSocket = new ServerSocket(port, 50, ia);
        serverSocket.setSoTimeout(timeout);
        this.wsServer = wsServer;
        this.wsServerPort = wsServerPort;
        this.ipList = ipList;
    }

    public void run() {
        logger.info("Filter source ips: " + ipList);
        while (true) {
            try {
                logger.info("Waiting for client " + " LocalSocketAddress: " + serverSocket.getLocalSocketAddress()
                        + ", HostAddress: " + serverSocket.getInetAddress().getHostAddress() +
                        ", HostName: " + serverSocket.getInetAddress().getHostName() + " on port " + serverSocket.getLocalPort() + "...");
                Socket clientSocket = serverSocket.accept();

                logger.info("Just connected to " + clientSocket.getRemoteSocketAddress());

                String hostName = clientSocket.getInetAddress().getHostName();
                String hostAddress = clientSocket.getInetAddress().getHostAddress();

                logger.info("Filer IPs" + ipList + ", hostName:" + hostName + ", hostAddress:" + hostAddress);

                if (ipList.contains(hostAddress) || ipList.contains(hostName)) {
                    /** Return error message to client */
                    logger.info("Return error message to client");
                    ClientReadMessageThread cht = new ClientReadMessageThread(clientSocket);
                    Thread t = new Thread(cht);
                    t.start();
                    ClientSendMessageThread csmt = new ClientSendMessageThread(clientSocket);
                    Thread t2 = new Thread(csmt);
                    t2.start();
                    try {
                        t.join(1000);
                        t2.join(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        logger.error(e);
                    }

                } else {
                    /** Redirect to sighting WebService */
                    logger.info("Forwarding request to sighting web service :" + wsServer + ":" + wsServerPort);
                    Socket wsSocket = new Socket(wsServer, wsServerPort);

                    wsSocket.setKeepAlive(true);
                    clientSocket.setKeepAlive(true);
                    logger.info("isConnected wsSocket:" + wsSocket.isConnected());

                    OutputStream os = wsSocket.getOutputStream();
                    InputStream is = clientSocket.getInputStream();

                    Thread clientToWS = new Thread(new ClientReader(is, os, "ID1:" + clientSocket.getInetAddress().getHostName() + " to " + wsSocket.getInetAddress().getHostName()));
                    clientToWS.start();

                    InputStream wsIS = wsSocket.getInputStream();
                    OutputStream csOS = clientSocket.getOutputStream();

                    Thread wsToClient = new Thread(new ClientReader(wsIS, csOS, "ID2:" + wsSocket.getInetAddress().getHostName() + " to " + clientSocket.getInetAddress().getHostName()));
                    wsToClient.start();

                    try {
                        clientToWS.join(1000);
                        wsToClient.join(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        logger.error(e);
                    }
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    logger.error(e);
                }
                logger.info("Close socket: " + clientSocket.getInetAddress());
                clientSocket.close();
            } catch (SocketTimeoutException s) {
                logger.error("Socket timed out!");
                logger.error(s);
                break;
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e);
                break;
            }
        }
    }

    public static class ClientReader implements Runnable {
        private InputStream is;
        private OutputStream os;
        private String id;

        public ClientReader(InputStream i, OutputStream o, String id) {
            this.is = i;
            this.os = o;
            this.id = id;
        }

        public void run() {
            try {
                logger.info(id + " start copying.");
                int x;
                x = is.read();
                while (x != -1) {
                    //    System.out.println(id);
                    os.write(x);
                    os.flush();
                    x = is.read();
                }
                logger.info(id + " data copied. x:" + x);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e);
            }
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("Usage: HOST_NAME PORT_NO TIME_OUT WS_HOST_NAME WS_PORT_NO COMMA_SEPARATED_IP_ADDRESS_TO_SEND_ERROR");

            String hostName = String.valueOf(args[0]);
            int port = Integer.parseInt(args[1]);
            int timeout = Integer.parseInt(args[2]);
            String wsHostName = String.valueOf(args[3]);
            int wsPort = Integer.parseInt(args[4]);
            String ipAddressToIgnore = String.valueOf(args[5]);
            logger.info("hostName:" + hostName);
            logger.info("port:" + port);
            logger.info("timeout:" + timeout);
            logger.info("wsHostName:" + wsHostName);
            logger.info("wsPort:" + wsPort);
            logger.info("ipAddressToIgnore:" + ipAddressToIgnore);
            List ipList = Arrays.asList(ipAddressToIgnore.split(","));
            Thread t = new ProxyServerTest(hostName, port, timeout, wsHostName, wsPort, ipList);
            t.start();

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
            System.out.println("Usage: HOST_NAME PORT_NO TIME_OUT WS_HOST_NAME WS_PORT_NO COMMA_SEPERATED_IP_ADDRESS_TO_SEND_ERROR");
        }
    }
}





import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientSendMessageThread implements Runnable {

    final static Logger logger = Logger.getLogger(ClientSendMessageThread.class);
    private Socket clientSocket;

    public ClientSendMessageThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void run() {
        try {
            String errorResponse = "soap:ServerObject \"org.mule.transport.NullPayload\" not of correct type. It must be of type \"java.util.List\"";
            logger.info("Sending error response:" + clientSocket.getLocalSocketAddress());
            logger.info("Error response:" + '\n' + errorResponse);

            PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());
            /** Set header details */
            pw.println("HTTP/1.1 500 Internal Server Error");
            pw.println("Content-Type: text/xml");
            pw.println("http.status: 500");
            pw.println("http.method: POST");
            pw.println("Content-Length: " + errorResponse.length());
            pw.print("\r\n");
            pw.println(errorResponse);
            pw.flush();
            // pw.close();
            logger.info("Response send to client:" + clientSocket.getLocalSocketAddress());
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e);
        }

    }
}




import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

public class ClientReadMessageThread implements Runnable {

    final static Logger logger = Logger.getLogger(ClientReadMessageThread.class);
    private Socket clientSocket;

    public ClientReadMessageThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void run() {
        try {
            logger.info("Reading data :" + clientSocket.getLocalSocketAddress());
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String tmp;
            StringBuffer inputLine = new StringBuffer();
            while ((tmp = in.readLine()) != null) {
                inputLine.append(tmp);
                logger.info(tmp);
            }
            logger.info("Finished reading data :" + clientSocket.getLocalSocketAddress());
        } catch (SocketException se) {
            // se.printStackTrace();
            logger.warn(se);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e);
        }
    }
}



Wednesday 5 February 2014

JVM - Memory Analysis


Decompiler
Yet another fast Java decompiler http://jd.benow.ca/

Memory Dump
To create a heap dump, use the jmap command from JDK:

  • jps - to find the process id
  • jmap -dump:format=b,file=heap.jmap java_PID 
  • -XX:+HeapDumpOnOutOfMemoryError VM parameter  
    • java -Xmx256m -XX:+HeapDumpOnOutOfMemoryError -jar *.jar
  •  jconsole /usr/jdk/jdk1.6.0_105/bin/jstack 24583 > outfile.txt 


Memory dump Analysis Tools 

  • jhat : http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html 
  • VISUALVM : http://visualvm.java.net/gettingstarted.html 
  • Eclipse Memory Analyzer (MAT)
  • JProfiler 












References : 

  • http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
  • http://www.oracle.com/technetwork/java/javase/index-138283.html#guides
  • http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html 
  • http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html 
  • http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jmap.html 
  • http://marxsoftware.blogspot.com.au/2009/06/heap-dump-and-analysis-with-visualvm.html 
  • http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html 
  • https://redstack.wordpress.com/2011/01/06/visualising-garbage-collection-in-the-jvm/
  • https://plumbr.io/outofmemoryerror/permgen-space

Saturday 18 January 2014

JMX Remote connection & VisualVM

JMX - Java Management Extensions

Enable remote access in tomcat. Include following options with JAVA_OPTS environment variables in tomcat start-up script.
  • -Dcom.sun.management.jmxremote 
  • -Dcom.sun.management.jmxremote.port=8999 
  • -Dcom.sun.management.jmxremote.authenticate=false 
  • -Dcom.sun.management.jmxremote.ssl=false 
  • -Dcom.sun.management.jmxremote.password.file=pw file path  
  • -Dcom.sun.management.jmxremote.access.file=access file path 
  • -Djava.rmi.server.hostname=(Optional)
You can place the password and permissions files in any location, such as "$CATALINA_HOME/conf/". Make sure to modify the permissions of these files so that they can only be viewed by users with appropriate access :
  • $ chmod 600 /path/to/pw/file
  • $ chmod 600 /path/to/access/file
Include in start-up scipt start_admin.sh

export CATALINA_OPTS="-XX:MaxPermSize=512m -Xms1024M -Xmx2048M -XX:NewRatio=3 -XX:MaxNewSize=700M -XX:-UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=$TOMCAT_HOME/conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=$TOMCAT_HOME/conf/jmxremote.access -Djava.rmi.server.hostname=$TOMCAT_HOST"



Process arguments



test@[49]/log% pargs -a 2288
2288:   /usr/java1.6/bin/java -Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf
argv[0]: /usr/java1.6/bin/java
argv[1]: -Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf/logging.properties
argv[2]: -DthisProcess=CEPServer
argv[3]: -Djava.awt.headless=true
argv[4]: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
argv[5]: -XX:MaxPermSize=512m
argv[6]: -Xms1024M
argv[7]: -Xmx2048M
argv[8]: -XX:NewRatio=3
argv[9]: -XX:MaxNewSize=700M
argv[10]: -XX:-UseConcMarkSweepGC
argv[11]: -XX:+UseParNewGC
argv[12]: -XX:+CMSParallelRemarkEnabled
argv[13]: -Dcom.sun.management.jmxremote
argv[14]: -Dcom.sun.management.jmxremote.port=8999
argv[15]: -Dcom.sun.management.jmxremote.ssl=false
argv[16]: -Dcom.sun.management.jmxremote.authenticate=true
argv[17]: -Dcom.sun.management.jmxremote.password.file=/opt/ITREfdsc/tomcat/conf/jmxremote.password
argv[18]: -Dcom.sun.management.jmxremote.access.file=/opt/ITREfdsc/tomcat/conf/jmxremote.access
argv[19]: -Djava.rmi.server.hostname=ceb-zone
argv[20]: -Djava.endorsed.dirs=/opt/ITREfdsc/tomcat/endorsed
argv[21]: -classpath
argv[22]: /opt/ITREfdsc/tomcat/bin/bootstrap.jar
argv[23]: -Dcatalina.base=/opt/ITREfdsc/tomcat
argv[24]: -Dcatalina.home=/opt/ITREfdsc/tomcat
argv[25]: -Djava.io.tmpdir=/opt/ITREfdsc/tomcat/temp
argv[26]: org.apache.catalina.startup.Bootstrap
argv[27]: start
fdsc@ceb-zone[50]/opt/ITREfdsc/log%


Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning

Java HotSpot VM Options

--------------------------------------------------------------------------------------------------------------------------
-Djava.awt.headless=true
To use any AWT libraries in your server code on a machine that's not running a windowing interface
CATALINA_OPTS is immensely useful for setting debug parameters, Java memory settings, etc. One very common use is to set the system property java.awt.headless to true. Most graphical applications (like Jasper, JFreechart, LiquidOffice, StyleReport, etc.) will halt Tomcat when they render if something isn't done to disable display rendering, and this is by far the easiest method. (This problem occurs with most J2EE servers, not just Tomcat). Here's how to make this setting in UNIX.
--------------------------------------------------------------------------------------------------------------------------
-Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
To provide customised log file config. It is default behaviour of Tomcat and included in catalina.sh
--------------------------------------------------------------------------------------------------------------------------
-XX:MaxPermSize=512m
The permanent space is where are stored the class, methods, internalized strings, and similar objects used by the VM and never deallocated.
-XX:PermSize specifies the initial size that will be allocated during startup of the JVM. If necessary, the JVM will allocate up to -XX:MaxPermSize.
For most applications the permanent generation is not relevant to GC performance. However, some applications dynamically generate and load many classes. For instance, some implementations of JSPs do this. If necessary, the maximum permanent generation size can be increased with MaxPermSize.
--------------------------------------------------------------------------------------------------------------------------
-Xms1024M
-Xmx2048M
-Xms defines the initial size of the heap and -Xmx defines the maximum size of the heap.

--------------------------------------------------------------------------------------------------------------------------
 -XX:NewRatio=3
By default, the young generation size is controlled by NewRatio. For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.
--------------------------------------------------------------------------------------------------------------------------

-XX:MaxNewSize=700M
-XX:NewSize

--------------------------------------------------------------------------------------------------------------------------

Run Visualvm


Create remote connection


View remote process details

References:

Wednesday 15 January 2014

Setup GXT 3 examples in Eclipse


TODO : Update step by step guide
Reference:
http://www.gwtproject.org/
http://www.sencha.com/examples/
http://eclectide.com/blog/2013/10/11/compiling-sencha-gxt-showcase/
http://www.sencha.com/forum/showthread.php?248378-GWT-2.5-support