Wednesday, January 21, 2009

Hibernate in Action (notes)

- Two paradigms: SQL/JDBC relational model vs. object-oriented domain model
SQL operations such as projection and join always result in a tabular representation
of the resulting data. This is quite different than the graph of interconnected
objects used to execute the business logic in a Java application.

  • Granularity
  • Subtype
  • Identity
  • Associations
  • Object graph navigation

* PK update issue: not only update PK, but also FK in reference tables if using a column like USERNAME as PK. It is recommended to use surrogate keys (with no meaning to the user) as PK wherever possible.

Tuesday, January 20, 2009

JTA - Java Transaction API

The numbered boxes around the transaction manager correspond to the three interface portions of JTA:

1—UserTransaction—The javax.transaction.UserTransaction interface provides the application the ability to control transaction boundaries programmatically. The javax.transaction.UserTransaction method starts a global transaction and associates the transaction with the calling thread.

2—Transaction Manager—The javax.transaction.TransactionManager interface allows the application server to control transaction boundaries on behalf of the application being managed.

3—XAResource—The javax.transaction.xa.XAResource interface is a Java mapping of the industry standard XA interface based on the X/Open CAE Specification

Notice that a critical link is support of the XAResource interface by the JDBC driver. The JDBC driver must support both normal JDBC interactions, through the application and/or the application server, as well as the XAResource portion of JTA.(So called XP compliant resource manager)

See: Understanding JTA—the Java Transaction API

Friday, January 16, 2009


The ThreadLocal class behaves much like the various Reference classes in java.lang.ref; it acts as an indirect handle for storing or retrieving a value. Listing 1 shows the ThreadLocal interface.

public class ThreadLocal {
public Object get();
public void set(Object newValue);
public Object initialValue();

See: Exploiting ThreadLocal to enhance scalability

public class EntityChangeTracker {

private static ThreadLocal<List<EntityChange>> entityChangeThreadLocal = new ThreadLocal<List<EntityChange>>() {
protected List<EntityChange> initialValue() {
return new ArrayList<EntityChange>();

public static List<EntityChange> getEntityChanges() {
return entityChangeThreadLocal.get();

public static void addEntityChange(Entity<?> e, AuditOperation opr) {
List<EntityChange> list = entityChangeThreadLocal.get();
list.add(new EntityChange(e, opr));

public static void clear() {

volatile keyword

Lock free, an alternative to synchronization and lock. A volatile variable is one whose value is always written to and read from "main memory" and not cached by register or thread. That means that different threads can access the variable.

A typical example of a "stop request" flag allowing one thread to signal to another to finish:

public class StoppableTask extends Thread {
private volatile boolean pleaseStop;
public void run() {
while (!pleaseStop) {
// do some stuff...

public void tellMeToStop() {
pleaseStop = true;

If the variable were not declared volatile (and without other synchronization), then it would be legal for the thread running the loop to cache the value of the variable at the start of the loop and never read it again.

See: The volatile keyword in Java 5

Tuesday, January 13, 2009


-Xloggc:[filename] is used to log GC. verbose:gc output to stdout.
-XX:+PrintGCDetails is to display more GC details on stdout.

There are two primary measures of garbage collection performance. Throughput is the percentage of total time not spent in garbage collection, considered over long periods of time. Throughput includes time spent in allocation (but tuning for speed of allocation is generally not needed.) Pauses are the times when an application appears unresponsive because garbage collection is occurring.

[1] Java theory and practice: A brief history of garbage collection
[2] Java theory and practice: Garbage collection in the HotSpot JVM
[3] Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine

Friday, January 2, 2009


private MutableLocation loc = new MutableLocation();
public Location getLocation() {
return loc;

Internally, you store a mutable object, but return it typed as a simple interface when you want to allow only read-only access.This is done to preserve encapsulation and prevents the location fields from being modified by external code.