Friday, October 23, 2009

Recursive SQL statement for Oracle database

Many people may still in doubt about how to do query against table which link recursively to itself. i.e. if you want to select all tree relate to folder 'A' from data as shown in the sample table below
Folders
 +-----------+-------------+
| Folder | ParentFolder|
| --------- | ----------- |
| A | B |
| B | C |
| E | A |


This can be done by using start with ... connect by clause like below

select folder from folders start with groupname = 'A'
connect by prior parentfolder = folder



Enjoy coding!


Thursday, October 22, 2009

[C++] Safe and efficient way to release session/lock

Have you ever found problem in managing lock or something similar to below?

void MyClass::MyMethod()
{
try{

LockAble.lock();
// do sensitive stuff

if (FaltalError){
// i worry about unlock the lock here
LockAble.unlock();
return;
}

// i worry about unlock the lock here
LockAble.unlock();
}
catch(MyClassException &)
{
// i worry about unlock the lock here
LockAble.unlock();
}
}


If you are finding alternative way to handle the above situation, you are reading the right blog :)..

The aim of writing this blog is explain a way to cope with pair operation such as lock/unlocked, check-in/check-out, acquire/release etc. Would that be cool if just declare only one statement then forget about it? Just look what it should be :)



void MyClass::MyMethod()
{
try{
ScopeLock lock(mLockableObj);
// do sensitive stuff
if (FaltalError){
// i don't worry about unlock the lock here
return;
}
// i don't worry about unlock the lock here
}
catch(MyClassException &)
{
// i don't worry about unlock the lock here
}
}


As above, you might already spotted that there is a class ScopeLock appears, what magic it is to solve it? Well, it just utilize destructor to unlock to lock automatically when that object is out from scope. See below for implementation..


class ScopeLock {
public:
ScopeLock(ILockAble &lockable):mLock(lockable)
{
lock.lock();
}

~ScopeLock()
{
lock.unlock();
}

private:
ILockAble &mLock;
};




That's it! Very easy to adapt to your existing implementation. Just put the starting operation in constructor and exiting operation in destructor. Enjoy coding!

Friday, October 16, 2009

Java Remote Debugging with JDPA with Eclipse

Many Java developers doesn't realize about remote debugging capability, thus make their life stay in trouble when their program start acting weirdly when it's running outside their development environment. Almost all of them start to add special code to print some useful information in some place to diagnose the problem. But is that the best way to do? In my aspect, the easiest way to spot the problem is using JDPA capability to debug it without touching source code...

To do that, these few steps are required.

1) Compile Java source code with debugging information attached.
2) Allow remote debugging in target JVM
3) Start remote debugging from supported client (Eclipse is perfect for me)


First step - Compile with debug information.

Go to project->properties->Java Compiler then select the check box as in the pic below








Next step - Deploy the compiled classes then enable remote debug in execution JVM

Build your application which contains debug information from first step then deploy it to target execution environment, then add the following execution arguments.

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar TechTemp.jar

Note: The target VM will be suspended until the debugger connects, just to ensure the program will not be executed pass the spot that you need to debug :)


The last step
- Start debugger to attach to remote JVM

Go to run->debug configurations then select Remote Java Application

















Change config to fit with your application then hit the Debug button and enjoy debugging!

Acronym:
JDPA - Java Platform Debugger Architecture

Thursday, October 1, 2009

Speedup C++ application using Hoard library

Does your C++ based application slow down, latency drop or stop working when dealing against a lot of data? Using Hoard might help you get rid of those problem.

What is Hoard?
Hoard library, the great memory allocator for C++ based program which is executed on multi-processor machine.
"Hoard is a drop-in replacement for malloc() that can dramatically improve application performance, especially for multithreaded programs running on multiprocessors"
- Hoard Site

How do i adapt Hoard to my program?

You do not need to touch any source code / build procedure to adopt Hoard to your C++ application. Just simply add Hoard lib to your execution environment, that's all! Just read here

Download Hoard here


Enjoy!