Tuesday, April 28, 2009

Cascade

We may get FK constraint exception when we try to delete the parent record (e.g., Player table). We can define database level cascade such as "on delete cascade|set null".


create table player_stats (
`id` integer unsigned not null auto_increment,
`player_id` integer unsigned not null,
primary key (`id`),
foreign key (`player_id`) references player (`id`) on delete cascade
)
engine = InnoDB;

Note that it is uni-directional. Only works for parent -> child cascade.

With Hibernate cascade, we don't need to define this on DDL. And the cascade is more flexible - bi-directional. We can cascade change from any side following the association link (one-to-many, many-to-one, many-to-many etc). It handles cascade on higher level, i.e., entity to entity. On example is tbl_apGroup, tbl_apGroup_ap, tbl_ap. The database cascade would be defined on FK tbl_apGroup_ap referencing to tbl_apGroup. But Hibernate cascade is from domain object ApGroup to Ap.

Friday, April 24, 2009

ConcurrentModificationException

If you try to modify a list while iterating through it, then you will encounter the ConcurrentModificationException.

The foreach loop is *is* syntactic sugar for iterating. However, you need to call remove on the iterator - which foreach doesn't give you access to.


for (SgeApAdapter apAdapter : apsToUpdate) {
if (...) {
apsToUpdate.remove(apAdapter);
}
}


It throws ConcurrentModificationException. We have to use iterator explcitly to remove item in collection.

Wednesday, April 8, 2009

Process.getInputStream()

The concept of an InputStream always refers to data coming IN to the thread which is invoking the InputStream method. Similarly, an OutputStream always refers to data pushed OUT by the thread which is invoking the OutputStream method.

The only place where this is really tricky is when you are working with Process objects. The getInputStream() method of Process returns a Java InputStream that reads from the standard output stream of the running Process.

The key thing to remember when using Runtime.exec() is you must consume everything from the child process' input stream. Otherwise, the child process may hang due to the buffer filling up.


Process proc = Runtime.getRuntime().exec(cmd);

StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
errorGobbler.start();
outputGobbler.start();

if (proc.waitFor() != 0) {
System.out.println("RestoreMain: Migration failed.");
result = false;
}


StreamGobbler

public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// ------------------------------------------------------------
// Exhaust Stream
// ------------------------------------------------------------
log.debug("StreamGobbler: " + line);
}
} catch (Exception e) {
log.debug("Failed to exhaust stream.");
}
}

Friday, March 20, 2009

Inner Classes

Inner Classes gives us a good introductions. Inner classes come in four flavors:: Static member class, Member class, Local class, Anonymous class. And the advantages of inner classes can be divided into three categories: an object-oriented advantage, an organizational advantage, and a call-back advantage.

The OO advantage is not well known for me compared with call-back advantage. One good example is to separate search algorithm as inner class in Tree class. Another is to nest MuListElement class inside BeastGetActiveMuRespMsg - composition relationship in UML.


public class BeastGetActiveMuRespMsg {
private int retCode;
private MuListElement[] muList;
....
public static class MuListElement {
private String macAddress;
private String ipAddress;
...
}
}

A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.