Thursday, November 7, 2013

Sequences in Oracle.

A Sequences in oracle are always operated in a Atomic operation. (Its very imp to be this way)

How to alter a sequence by advancing it by specified account

DECLARE
temp NUMBER := 0;
BEGIN
FOR idx IN 1..80000 LOOP
select seq.nextval into temp from dual;
END LOOP;
END;
/

Why do you need to do this?
The above statement requires only DML permissions, where as an ALTER statement requires DDL permissions.

Monday, July 22, 2013

Speed Reading

Most people read word by word, and often say the words to themselves using the voices in their heads. Why do we do this? I fully blame kindergarten reading programs, which force children to read out loud both in class and at home. Who thought of this? Anyway, that is not at all the most effective way to read, in terms of both speed and comprehension. Therefore, the first thing you need to do is eliminate the voice inside your head. This took me at least 1 hour to do. Read with your eyes. ONLY. Whatever you do don't use the voice inside your head. As you learn to do this part go ahead and read word by word and go over the lines with your finger. Just work on eliminating the voice. Why? You cannot say words nearly as fast as you can comprehend them, believe it or not. Don't limit your reading speed to your speaking speed. This process really sucks, by the way. If you really believe that the voice in your mind is your enemy, you will probably be extremely frustrated for the first half hour of your journey. You may get better, then get a snack, then come back to continue your training and realize you are back where you started. Feelings of hopelessness will arise. Be a man/woman, and push on. 

A tip for those who are having trouble not subvocalizing is to hold your breath as you read. As Sean O'Connor pointed out, we breathe out as we read, even when we do so silently, since we're used to speaking the words aloud. A lot of the time, you even notice yourself making movements with your mouth, though it's closed. Hold your breath as you begin trying to eliminate subvocalization. Breathe in when you need to. (Don't force discomfort or anything) Once you feel that you've effectively eliminated that voice, try to return to normal breathing. Remember to use breath-holding as a crutch whenever you are having trouble. It has no noticeable effect on your speed or comprehension, and it helps immensely when you are having a rough time. 

Once you are able to pretty much eliminate the voice inside your head, you have unlocked a huge amount of potential in terms of how fast you can now read. Since you're reading with your eyes, try reading two or three words at a time, and then move up once you get comfortable. Trust me, if you don't skip the voice elimination step, you will make progress fast. After a couple days I was reading pages out of an assigned novel within 15-20 seconds, the book wasThe Guide, and with full comprehension. I am not exaggerating. This is when I got really in the zone of course. Not having the initial tunnel vision can be frustrating but after about 10 minutes you get it down and you fly through pages. I was reading about a line at a time. One tip I have that helped me read more words at a time is to think about opening your eyes REALLY WIDE. Actively try to take in as many words at a time as you can and try to move along the lines as fast as you can while doing this. You'll build your own rhythm eventually.

Hopefully this helps someone. It really helped me. I was able to crush estimated hour-long reading assignments in 10 minutes or less. I'm out practice right now, but I want to get back into it this summer so I can complete the books listed on the answer wiki for Books: What are some potentially life-changing books?

P.S. Here's a link (thanks Mike Stenhouse) that teaches you a lot of about speed-reading technique. The explanation it gives encompasses a lot of what I've said, and I found it pretty useful in cleaning up how I read and in increasing my reading ability generally: Scientific Speed Reading: How to Read 300% Faster in 20 Minutes



copied from post on quora..

Tuesday, June 11, 2013

Unit testing in java

1. Make sure to write each tests completely independent of other.
2. Don't depend on the order of execution of the tests and it is not guarenteed.
3. Use mocks for external classes and test logic for only one class at a time.
4. And always verify the mocks to validate execution behavior with expected results.
5. Divide the steps in a test into setup, test, verify sections so that its easy to read.

when using spring-context.xml for spring applications.

@DirtiesContext is useful when it is necessary to re-initialize the auto-wiring of beans so that we make use of fresh copies of beans on each test.
This annotation will make sure that after the test, the new objects are created and injected.

Friday, June 7, 2013

Eclipse setup - useful plugins

Download the latest version of eclipse.
Install the following plugins

1. FindBugs - Static code analysis tool for possible bugs or performance.
2. CheckStyle - Java coding standand suggestions and violation detector
3. Cobertura - Unit test code coverage tool. shows line and branch coverage
4. Eclipse Color themes - Better (may be not) backgroup theme for editors
5. Git - Source Control. this is a must.

And this is the end of the list. feel free to suggest any better tools.

Thursday, May 16, 2013

Mocking a Static Method


import the following:

import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


Add the following annotations to the class:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Static_Class.class)

public class ClassNameTest

And mock the class with the following Code.

                PowerMock.mockStatic(Static_Class.class);
        expect(
                StaticClass.Static_Method(params)
                .andReturn(mockObject);
        PowerMock.expectLastCall().atLeastOnce();
        PowerMock.replayAll();

Wednesday, May 8, 2013

Use of @Override annotation JDK 1.5 vs JDK 1.6


The JDK version 1.5 doesn't require the Implementing class for an Interface to use @Override annotation for the implemented methods.

Usually when eclipse has the settings to run on JDK 1.6, it adds @Override annotation, as JDK 1.6 allows the use of @Override annotation even for implemented methods from an interface.

Its documented well here.

http://stackoverflow.com/questions/2335655/why-is-javac-failing-on-override-annotation

Tuesday, April 16, 2013

Adding a SystemEnvironment variable - for unit-tests

I recently came across a situation where I want to test a piece of code which looks for Environment variable and it is not available to during testing. (Especially when the application is launched from spring configuration, and the environment variables are set from application-context.xml.

Use the following piece of code to set the system environmnet variable in memory and do not forget to remove after the test case is successful.


public static void setSytemEnvInMemory(
            String key, String value) throws Exception {
        Map<String, String> env = System.getenv();
        Field field = env.getClass().getDeclaredField("m");
        field.setAccessible(true);
        ((Map<String, String>) field.get(env))
                .put(key, value);
    }

The above code basically uses reflection to get the map that stores the environment variables in System class.

The use of reflection is sometimes limited by SecurityManager, but in most cases this should work seamlessly.

This works well on RHEL.
There is post in stack-overflow for code to work on "Windows"

Wednesday, March 13, 2013

Use of comments (Good coding practices)

If certain piece of code requires an explanation, we often add comments for the block or the logic.

Always try to avoid comments if the code can be moved to a new private method where the methodName can eliminate the need for comment.

Note: comments should not be used for bad code. it should explain in short about what "this good code does"

Wednesday, February 6, 2013

Handling Interrupted Exception (Good Coding Practices)


Never swallow interrupted exception.

Always either re-throw the exception (or) call Thread.currentThread().interrupt() which sets a flag that this thread has been interrupted, perform any cleaning tasks and make use of this flag to take the appropriate action by checking Thread.currentThread().isInterrupted()

The last thing to do will be to swallow the Exception.

Note: Thread.currentThread().interrupt() doesn't actually throw an interrupted exception, but sets a flag that indicates this thread has been interrupted by other application.

Reference: http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html


Creating a temporary file (to be deleted after immediate use) {good coding practices}

Creating a temporary file (to be deleted after immediate use) {good coding practices}

File f = null;
try {
    f = createNewFile();
    // do your stuff here.
}
catch (Exception e) {

}
finally {
     if( f != null ) {
         f.delete();
     }
}

This way, we always ensure that file is deleted before use. and any return statements within try{} block will still doesn't prevent file from deleting.