Sunday, January 12, 2014

Enable Debug mechanism in Pro*C

In order to use this debug mechanism the Pro*C to be debugged must include the
following libraries:
- oracle library (oracle.pc) – from this library the program must use the LOGON
function to perform the login in the database.
- restart and recovery libraries (restart.pc or the retek_2.pc) – from this library the
program must use the restart_init, restart_file_init, restart_commit,
restart_file_commit and restart_close functions (if using the restart.pc) or the
retek_init, retek_commit and retek_close functions (if using the retek_2.pc) to
perform the commits and rollbacks in the database.
No other functions should be used to perform the login and the database commits.

To run the Pro*C in debug mode the user only needs to append the desired debug
parameters to the command line.

#Run in normal mode
$BIN/pro_c_name $MMUSER/$MMPASS

#Run in debug mode
$BIN/pro_c_name $MMUSER/$MMPASS

RMS allows the user to use six different debug parameters (RMS9 only support the first
three):
- SQL_TRACE=
- TIMED_STATISTICS=
- NO_COMMIT=
- PROFILER=
- RTK_TEST=
- TEST_CASE=

All these parameters support the following values: 0 or FALSE, 1 or TRUE, and some of
them support additional values.
If the user doesn’t want to use a specific debug parameter he can omit the parameter or
use the parameter with the values 0 or FALSE.


And please remember…
Always write the debug flags after the Pro*C parameters, as the Pro*c
parameters will be read inside the program as argv[1], argv[2], …


SQL_TRACE

The value of SQL_TRACE disables or enables the SQL trace facility. Setting this parameter
to true provides information on tuning that can be used to improve performance. This
information will be written in the trace file.
Typically oracle only writes in the trace file when one of the Oracle background processes
(such as dbwr, lgwr, pmon, smon and so on) encounter an exception or when an ORA-
00600 error occurs.
When SQL_TRACE is enabled it will record the events associated with the trace level
passed as parameter.

Turn off the SQL_TRACE

To turn off the SQL_TRACE the user can use this parameter with one of the following
values:

, SQL_TRACE=0 or SQL_TRACE=FALSE

This is the same that perform the following command in the database:

ALTER SESSION SET SQL_TRACE=FALSE;

3.1.2 Turn on the SQL_TRACE

To turn on the SQL_TRACE the user can use this parameter with one of the following
values:

SQL_TRACE=1 or SQL_TRACE=TRUE
SQL_TRACE=4
SQL_TRACE=8
SQL_TRACE=12

This is the same that perform the following command in the database:

ALTER SESSION SET EVENTS '10046 trace name context forever, level 1';
ALTER SESSION SET EVENTS '10046 trace name context forever, level 2';
ALTER SESSION SET EVENTS '10046 trace name context forever, level 8';
ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';

The event that will be traced is the event 10046, which is used to trace sql statements.
This is the most important event that can be set, it is functionally equivalent to setting
sql_trace=true. However, it allows outputting extra details by setting the level to one of
the following levels:
1: Standard SQL tracing
4: Level 1 + tracing bind variables
8: Level 1 + tracing wait events
12: Level 4 + Level 8 (SQL tracing, bind variables and wait events)
And please remember…
The trace files will be written into the directory pointed to by the parameter
user_dump_dest. You can query for the value with:
select value from v$parameter where name = 'user_dump_dest';

TIMED_STATISTICS

The value of TIMED_STATISTICS disables or enables Oracle to include timing information
into the sql trace files. Setting this parameter to true provides information on tuning that
you can use to improve performance.

Sunday, January 5, 2014

Producer / Consumer alternate run example

package concurrency;

import java.util.LinkedList;
import java.util.Queue;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class InterThreadCommunicationExample {

    public static void main(String args[]) {
        BasicConfigurator.configure();

        final Queue sharedQ = new LinkedList();

        Thread producer = new Producer(sharedQ);
        Thread consumer = new Consumer(sharedQ);

        producer.start();
        consumer.start();

    }
}

 class Producer extends Thread {
    private static final Logger logger = Logger.getLogger("Producer");
    private final Queue sharedQ;

    public Producer(Queue sharedQ) {
        super("Producer");
        this.sharedQ = sharedQ;
    }

    @Override
    public void run() {

        for (int i = 0; i < 4; i++) {

            synchronized (sharedQ) {
                //waiting condition - wait until Queue is not empty
                while (sharedQ.size() >= 1) {
                    try {
                        logger.debug("Queue is full, waiting");
                        sharedQ.wait();
                    } catch (InterruptedException ex) {
                        logger.error(ex);
                    }
                }
                logger.debug("producing : " + i);
                sharedQ.add(i);
                sharedQ.notify();
            }
        }
    }
}

 class Consumer extends Thread {
    private static final Logger logger = Logger.getLogger("Consumer");
    private final Queue sharedQ;

    public Consumer(Queue sharedQ) {
        super("Consumer");
        this.sharedQ = sharedQ;
    }

    @Override
    public void run() {
        while(true) {

            synchronized (sharedQ) {
                //waiting condition - wait until Queue is not empty
                while (sharedQ.size() == 0) {
                    try {
                        logger.debug("Queue is empty, waiting");
                        sharedQ.wait();
                    } catch (InterruptedException ex) {
                        logger.error(ex);
                    }
                }
               
                Integer number =(Integer) sharedQ.poll();
                logger.debug("consuming : " + number );
                sharedQ.notify();
             
                //termination condition
                if(number == 3){break; }
            }
        }
    }
}

HTTPClient PoolingHttpClientConnectinManager Example for Better Performance

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpClientExample{

    public static void main(String[] args) throws IOException {
        
        SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
        String datef = sdf.format(new Date());
        System.out.println(datef);
        ArrayList yrls = new ArrayList();
        String url = "www.yahoo.com";
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(10);
        cm.setDefaultMaxPerRoute(10);
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();
    
        long time1 = System.nanoTime();
        
        //    HttpClient httpClient = new DefaultHttpClient();
            HttpHead get = new HttpHead(url);
            HttpResponse resp = httpClient.execute(get);
            System.out.println(url +" :::"+ resp.getStatusLine().getStatusCode());
            
               long time2 = System.nanoTime();
        
        System.out.println("Difference = " + (time2 - time1));
        httpClient.close();
        
    }

  }

Forcing Runtime Exceptions

If you need to write unit tests to force Runtime Exceptions such as interruptedEx ception etc you can use ByteMan .

Querying in Neo4j Standalone Server Using Java Api

Get https://www.dropbox.com/s/qhiw5o86z965ony/neo4j-rest-graphdb-1.8.M07.jar

import static org.neo4j.helpers.collection.MapUtil.map;

import java.util.Map;

import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;


public class CypherQuery {
   
   
    public static void main(String[] args) {
        try{
    System.out.println("starting test");
    final RestAPI api = new RestAPIFacade("http://localhost:7474/db/data/");
    System.out.println("API created");
    final RestCypherQueryEngine engine = new RestCypherQueryEngine(api);
    System.out.println("engine created");
    final QueryResult> result = engine.query("start n=node({id}) return id(n) as id, n.node_id? as name;", map("id", 200));

    System.out.println("query created");
    for (Map row : result) {
       String id=(String) (row.get("name"));
       System.out.println("id is " + id);
    }
    }
    catch(Exception e)
    {
       e.printStackTrace();

    }
    }
  }

Windows 7 batch script to save Chrome broken downloads

for /l %x in (1, 1, 100000) do (
echo %x
sleep 30
copy /Y 
)