2010/03/15

Improvement on Hibernate Query Cache

Cache improves performance by transparently storing data such that future requests for that data can be served faster. Hibernate offers a session scope transactional cache which spans the scope of the session. Hibernate also offers a configurable secondary level cache which spans across multiple sessions. Clustered/Distributed Caches can be configured so that it can be accessed across the entire application server cluster.

Hibernate secondary cache offers pretty good entity cache. Entity cache speeds up entity loading by its primary key. For instance:

select c from Cat c where c.id = :id

It supports query cache too, to enable query cache, you'll need to specify the hibernate property:

hibernate.cache.use_query_cache true

Query cache caches Query result sets, so the cache can provide you an answer for the queries like:

select p from Person p where p.sin = :sin

Let's say, the SIN of a person is never gonna change, so cache the above query will be useful.

Hibernate provides 'natural id' based on queries which can effectively solve the above problem. However, there are many other queries that hibernate cannot handle effectively. For instance, if we want to find all contact information (phone numbers only) for a person:

select pn from PhoneNumbers pn where pn.person.id = :pid 


The primary problem for Query caches, is that hibernate will completely invalidated by any modification to the underlying table, even on entities that's totally unrelated to the query. In the above example, any insertion/update/deletion to the telephone table, on any record, will completely wipe out the entire cache for the above query.

There needs to be a 'smart query cache' to solve this problem. Fortunately, this can be done by providing custom query cache, and we can then refreshes them by listening on hibernate 'entity modification' events. The listener will allow us to intercept any insertion/update/deletion to any telephone record, and then we can determine to only refresh the affected query cache.

2009/02/15

DisplayTag 1.2 Form Submission Problem

Prior to 1.2, all the links generated by displaytag are Http Get only, which prevented it from being integrated into many web applications. Including ours. Those links includes paging/sorting and exporting links.

I had to solve the problem by using a customized TableTag along with custom javascript code. The solution was applied around the 1st week of December.

DisplayTag 1.2, released Dec. 27, 2008, solves the form submission problem. Now The paging/sorting links can actually be Http Post instead of Get. Woot!

However, they must have forgot the export link - it's still get only... Another custom patch...

Well one day I'll have to contribute it back to the open source project. Too busy recently.

2008/09/03

Simple tip: Inject the nested class in Spring context

It's a simple tip and is not mentioned in Spring reference docs:

When you want create a bean in Spring which is a nested class of some owner class, use the "$" to separate the owner class name and the nested class name like following example:

<bean class="org.balah.OwnerClass$NestedClass"
.....

Don't use the convention like "org.balah.OwnerClass.NestedClass" that is used in the source code usually. Because the Spring will use Class.forName() similar mechanism to find the class specified in the bean definition. As you use the Class.forName() to load a nested class, always use "$" to let the classloader locate the exact class file from your classpathes. The "." sperated convention just confused the class resolver anf finally throw a ClassNotFoundException.

2008/04/24

Java7 new features: Personal View

Ref: The incoming Java 7 new features

All about just one thing: Java is becoming a platform and heavier and heavier version by version.Losing its very authentic and original simple beauty purely from language perspective.

Actually all the ideas are not quite new or very original innovative, they have already appeared in various libraries, components, frameworks and products. Sun is just collecting and refurbishing those ideas in name of JSR process then put them in its own JDK's shoes. Afterwards, MS will borrow the ideas but make it much easier to use in its monolithic .NET tools than diverted java tools world.

However, it's still a good news that the new changes on the heavier new "JAVA platform" will definitely drive the success of simpler and straightforward emerging dynamic 2G languages on top of Java, such as Groovy, which is just copying the same story Java replacing C/C++/Delphi 10 years ago!

Let's see.

Inter-thread coordination: wait, notify and interrupt

Just a sample code to test the inter-thread coordinating by using the object's wait and notify methods.

public class TestWaitAndNotify {
private final Object event = new Object(); private final Task1 task1 = new Task1(); private final Task2 task2 = new Task2(); private boolean stop; public TestWaitAndNotify() {
stop = false;
} public static void main(String[] args) throws InterruptedException {
TestWaitAndNotify twan = new TestWaitAndNotify(); twan.doIt();
} void doIt() throws InterruptedException {
task2.start(); task1.start(); stop = true; System.out.println("main:t [try to stop]"); synchronized (event){
System.out.println("main:t [notify all waiting threads]"); event.notifyAll();
} System.out.println("main:t [quit]");
} class Task1 extends Thread{
public void run(){
synchronized (event){
System.out.println("task1 running."); while(!stop){
System.out.println(">>>>>>>>task1 did"); event.notifyAll(); try {
System.out.println("task1 waiting."); event.wait(0); System.out.println("task1 wake");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} System.out.println("task1 quit");
}
}
} class Task2 extends Thread{
public void run(){ synchronized (event){
System.out.println("Task2 running."); while(!stop){
try {
//Thread.sleep(10000); System.out.println("task2 waiting."); event.wait(0); System.out.println("task2 wake");
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("<<<<<<<task2 did"); event.notifyAll();
} System.out.println("task2 quit");
} }
}
}
Conclusion:
  1. The order of threads starting is important in order to make sure the threads are waiting and notifying in sequence per our expectation.
  2. One object could be used as an event/signal to coordinate the threads behaviors by using wait and notify combination.
  3. Don't swallow or ignore the InterruptedException as you usually will do. Try to handle it properly, because someone might try to stop the blocking thread by interrupt it. Especially you are doing some common library which might be called by various unknown classes in various unknown conditions. Ignore it in your codes might cause other guy's attempts fail.
  4. Gracefully shutting down a thread by using an central control flag. However, a control thread or the main thread should try to call the coordinating object's notifyall before quit itself in order to wake up all the waiting threads and let them have a chance to shutdown gracefully.
  5. You can not always assume the call to the interrupt method of a blocking thread will work as your wish if you try to wake up it and let it quit. The probable failure case is:
    • If the blocking of thread is caused by waiting for the monitor of a synchronized method, it can not release the lock. However, if it's the case that the thread is waiting for a synchronized object, it's possible the interrupt will release the lock.
    • InterruptedException ignored by any codes is running in the thread when its interrupt is called.

2008/04/22

Incoming new Java 7 Featuers

Alex Miller has an excellent summarize of the new Java 7 Featuers:

Improved Modularity Support (superpackages)

   1. superpackage example.bar.lib {  
2.
3. // member packages
4. member package example.bar.lib;
5.
6. // member superpackages
7. member superpackage example.bar.lib.net, example.bar.lib.xml;
8.
9. // list of exported types
10. export example.bar.lib.Table;
11.
12. export superpackage example.bar.lib.net;
13. }

Java Module System
A distribution format and a repository for collections of Java code and related resources. It also defines the discovery, loading, and integrity mechanisms at runtime. Defines a new deployment archive called a JAM (Java Module). The Java Module is a JAR file that contains other JARs, resources, and metadata. JAMs can specify which portions of the module are public and which are hidden. JAMs can specify dependencies on other modules.

Java Kernel
Implement Java as a small kernel, then load the rest of the platform and libraries as needed, with the goal of reducing startup time, memory footprint, installation, etc. In the past this has also been referred to as “Java Browser Edition”.

NIO2
APIs for filesystem access, scalable asynchronous I/O operations, socket-channel binding and configuration, and multicast datagrams.

Date and Time API
A new and improved date and time API for Java. The main goal is to build upon the lessons learned from the first two APIs (Date and Calendar) in Java SE, providing a more advanced and comprehensive model for date and time manipulation.

Units and Quantities
one or more Java packages for the programmatic handling of physical quantities and their expression as numbers of units.

JCache API
Standardizes in process caching of Java objects in a way that allows an efficient implementation, and removes from the programmer the burden of implementing cache expiration, mutual exclusion, spooling, and cache consistency.

Concurrency Utilities
New kind of BlockingQueue called TransferQueue and a fine-grained parallel computation framework based on divide-and-conquer and work-stealing.

XQuery API
An API for executing XQuery calls and working with results. XQJ is to XQuery what JDBC is to SQL. At a glance, the XQJ API intentionally shares many high-level concepts from JDBC (DataSource, Connection, etc) but supports XQuery specific concepts such as static and dynamic phases, XML-oriented retrieval, etc.

Resource Consumption Management API
Allow for partitioning resources (constraints, reservations) among Java applications and for querying about resource availability (notifications). It will also provide means of exposing various kinds of resources. Example resources are heap memory size, CPU time, open JDBC connections, etc. The goal is to allow resources to be managed so that multiple applications might run simultaneously in a JVM and be given finite amounts of memory, cpu, etc. This JSR will build on top of JSR 121 Isolates.

Swing Application Framework
A simple application framework for Swing applications. It will define infrastructure common to most desktop applications. In so doing, Swing applications will be easier to create.

Beans Binding
An API that allows two properties of two beans to stay in sync. Formalizes an API for connecting JavaBeans.

Beans Validation
Define a meta-data model and API for JavaBeanTM validation based on annotations, with overrides and extended meta-data through the use of XML validation descriptors.

Java Media Components
Support for video to Java. It will initially address playback and ultimately will also cover capture and streaming. Video playback will provide support for both native players and a pure Java player.

JMX 2.0
Updates the JMX and JMX Remote APIs to improve usability of existing features and add new functionality such as annotations for ease of development, federated JMX servers, etc.

Web Services Connector for JMX
A connector for the JMX Remote API that uses Web Services to make JMX instrumentation available remotely. Clients do not have to be Java applications, but can be.

Javadoc Technology Update
New tags and generated Javadoc document representation aimed to increase readability, information richness, and make the Javadoc more approachable to developers learning and using the APIs.

Reified Generics
Currently, generics are implemented using erasure, which means that the generic type information is not available at runtime, which makes some kind of code hard to write. Generics were implemented this way to support backwards compatibility with older non-generic code. Reified generics would make the generic type information available at runtime, which would break legacy non-generic code. However, Neal Gafter has proposed making types reifiable only if specified, so as to not break backward compatibility.

Type Literals
   1. Type<List<String>> x = List<String>.class;   

Annotations on Java Types
enriches the Java annotation system. For example, it permits annotations to appear in more places than Java 6 permits; one example is generic type arguments (List<@NonNull Object>). These enhancements to the annotation system require minor, backward-compatible changes to the Java language and classfile format.

Type Inference
Constructor invocation can be changed from:
   1. Map<String, List<String>> anagrams = new HashMap<String, List<String>>();  
2. to
3. Map<String, List<String>> anagrams = new HashMap<>();

and explicit type references can now be inferred changing:
# timeWaitsFor(Collections.<Man>emptySet());  
# to
# timeWaitsFor(Collections.emptySet());

Closures
A closure is a function that captures the bindings of free variables in its lexical context. Closures support passing functions around and make many anonymous inner class use cases easier to use. In addition, they support additional high-level programming abstractions.

Automatic Resource Block Management
   1. // Start a block using two resources, which will automatically   
2. // be cleaned up when the block exits scope
3. do (BufferedInputStream bis = ...; BufferedOutputStream bos = ...) {
4. ... // Perform action with bis and bos
5. }

Language level XML support
   1. elt.appendChild(  
2. <muppet>
3. <name>Kermit</name>
4. </muppet>);

JavaBean Property Support
   1. public property String foo;   
2. a->Foo = b->Foo;

BigDecimal operator support
Support for manipulating BigDecimal objects with arithmetic operators (just like other numeric primitives) instead of via method calls.

Strings in switch statements
# static boolean booleanFromString(String s) {  
# switch(s) {
# case "true":
# return true;
# case "false":
# return false;
# }
# throw new IllegalArgumentException(s);
# }

Comparisons for Enums
   1. boolean isRoyalty(Rank r) {  
2. return rank >= Rank.JACK && rank != Rank.ACE;
3. }

Chained Invocation
   1. class Factory {  
2. void setSomething(Something something) { ... }
3. void setOther(Other other) { ... }
4. Thing result() { ... }
5. }
6.
7. Thing thing = new Factory()
8. .setSomething(something)
9. .setOther(other)
10. .result();

Extension methods
   1. import static java.util.Collections.sort;  
2. List<String> list = ...;
3. list.sort(); // looks like call to List.sort(), but really call to static Collections.sort().

Improved Catch Clause
# try {  
# return klass.newInstance();
# } catch (InstantiationException | IllegalAccessException e) {
# throw new AssertionError(e);
# }

invokedynamic
Introduces a new bytecode invokedynamic for support of dynamic languages.

Tiered compilation
Tiered compilation uses a mix of the client (JIT) and server hot spot compilers - the client engine improves startup time by using JIT over interpreted and the server hot spot compiler gives better optimization for hot spots over time.

G1 Garbage Collector
G1 (for “garbage first”) is a single collector that divides the entire space into regions and allows a set of regions to be collected, rather than split the space into an arbitrary young and old generation. Sun intends to make this new collector the default in Java 7. (This applies only to the Sun JDK.)

More Script Engine
additional language interpreters to JSE. The JavaScript Rhino engine was added in Java 6. Leading candidates for Java 7 are: JRuby, Jython, and Beanshell.

2008/04/20

GIS For Web Developers

Scott Davis on Seattle NFJS Java Conference


ACT1 - Free Data Available
- Raster Data, Google Satellite
- Vector Data, Google Map
- Hybrid, Google Satellite + Google Map Hybrid mode

Type of Vectors
- Point
- Line
- Polygon

Free Vectors: www.census.gov

Shapefile: well documented proprietary ESRI standard, .shp, .shx, .dbf

 

More data, please: http://www.nationalatlas.gov


For local data, most state and municipal government agencies offer free data as well. 

ACT2 - Projections

Earth is round but maps are flat, projections take fundamentally 3d data and portraying it in 2-dimensions, this procedure introduce error for distance, direction, shape, area. On a Cartesian Planes, X and Y lines form perfect squares, however, on a globe, only the line of Latitude are perfectly parallel, line of Longitude converge at the poles. Different projections attempt to minimize map distortions, common projections are "State Plane" and "UTM".

Reprojection Utility available: GDAL 


ACT3 - Spartial Databases

Why bothers with a database?
- Centralize many scattered files
- Provide security
- Indexing
- Cross dataset queries

PostgreSQL + PostGIS: http://postgis.refractions.net
Visualizing the Data: uDig

ACT4 - Web Services

- Google Maps, undeniably cool, proprietary interface
- Open set of standards available: Open Geospatial Consortium (OGC)
- OGC Features: GetCapabilities, DescribeFeatureType, GetFeature
- Data Service: WFS (Web Feature Service), WMS (Web Mapping Service)
- Open standard, any one can implement them: http://opengeospatial.org/resource/products
- GeoServer: Java based, runs in a Servlet container, provides OGC web services
- MapBuilder: Pure Javascript web client for OGC, included with GeoServer
- OpenLayers: Pure javascript web client, supports OGC, Google Maps, Yahoo Maps, MS Virtual Earth and WorldWind

J2EE Blogger

Well well... why another J2EE blog? I benefited from other people's technical blogs, and guess what, it's a good idea to contribute some of my works too. Hope it's helpful and useful, to all of your folks.