Best Practices – JDBC

Continuing from the previous entry, I’ll discuss JDBC. Although there was a section on Servlets, the recommendations are more decision-levels such as deciding on a framework, mad caching and hiring a UI designer. So here I go…

Use DataSource Whenever Possible

I don’t fully agree with this yet, since I still don’t see the absolute benefit when using the DataSource technique. The book explains that using the old Class.forName() and DriverManager.getConnection() contains references to specific implementation classes.

I don’t agree because these information are Strings, which can be read from a config file and drivers can be loaded dynamically. Java programmers have always also used generic JDBC interface to deal with the underlying database (as I mentioned in a previous post).Therefore I see the same level of portability in using both methods, especially when a ConnectionManager is used to retrieve connections (encapsulation of connection logic).

Pool Database Connections

Enough said. If you don’t understand this ask me or Google.

Separate Application, Persistence and JDBC logic.

As per most apps, beans are used to model entity information and are already separate from JDBC logic. As a plus, using a DAO interface model can further isolate your persistence logic from the application, allowing your underlying persistence implementation to switch to non-JDBC.

This is already a common pattern in EJB, where entity beans rely on a DAO to perform stores, loads and finds.

Do not Rely on Built-in Key Generation

For portability sake, this is a must. Besides difficulty in locating the record just inserted, other databases might not have this feature.

The book also provide a sequencing method to generate unique keys, though I think it can be more easily implemented with a SELECT MAX (only for trivial apps) or a separate table storing the current sequence. The method presented offers better performance than my methods, so do an evaluation before deciding which to use.

Use PreparedStatement

I would say not only use it, but use it correctly. The purpose of using PREPARED statements is that it can be executed repeatedly efficiently. Therefore if you prepare a statement, execute it once, and prepare the same statement again next time and execute it once, you aren’t really using PreparedStatement correctly.

The preferred way to use PreparedStatements is have it prepared once, and executed many times. Be it a static query or with ? parameters, these “prepared” statements will offer better performance.

Reference Columns By Number

This is actually two problems in one. The first is that access to metadata information is expensive, and referencing column by name requires metadata. Most programmers access columns by name now due to the flexibility and convenience of not needing to remember column numbers as well as able to change the sequence in the SELECT without affecting the code.

However, just like column numbers can change, column names can change as well when the SQL is changed. The book says that rs.getLong(1) is as bad as rs.getLong("personID")

The “correct” solution is to externalize both the SQL and the sequence number so that the query can be changed without affecting the code

new PreparedStatement(PERSON_QUERY);
rs.getLong(PERSON_QUERY_PERSON_ID);

Close Statements and ResultSets

Despit JDBC specifications, some drivers do not clean up as cleanly as specified. To clean up completely, use the finally clause. In fact, this template should always be used.

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = ..;
stmt = conn.prepareStatement(..);
stmt.set..(..);
rs = stmt.executeQuery();
while (rs.next())
// ..
} catch (SQLException sqlE) {
// handle exception
} finally {
if (rs != null)
try { rs.close() } catch (SQLException sqlE) {}
if (stmt != null)
try { stmt.close() } catch (SQLException sqlE) {}
if (conn != null)
try { conn.close() } catch (SQLException sqlE) {}
}

Thread JDBC code in Swing Applications

In general I think this should be “Thread time-consuming task in GUI applications”. The threading, though it complicates the code, is necessary to provide a responsive GUI to the user. When time consuming tasks are performed in the GUI event thread, the event thread is unable to perform other tasks such as redrawing the UI or getting input from the user through the UI. This includes unable to cancel the event if the user chooses to.

At the same time, care should be taken when accessing GUI elements from threads. See the wiki for more information on this.

Do Not Use Primary Keys with Real-World Meaning

This tip effectively contradicts with the perfect data model of the primary key. The fact it is listed as a best practice in a published book demonstrates the vast difference between academic theory and industry practice. However for practical purposes, it is still better to issue a meaningless identifier to the data row.

You might argue some data like your IC number don’t change, but they might be entered incorrectly into the system in the first place (data entry, human/machine error) and needs to be modified.

Java Enterprise Best Practices – EJB

This book from O’Reilly provides “Expert Tips & Tricks for Java Enterprise Programmers”. Apart from pure EJB stuff, the book covers tips for servlets, JDBC, XML, etc. I’ll be wrapping up the tips in a few short notes, espcially those I’ve seen/read around. First I discuss EJB tips.

Use ValueObjects for bulk data transfer.

Instead of making a get call for each property in your remote object, use a session bean to grab all necessary properties in a single call (thus also using the session facade)

Handle Exception in EJB Code Correctly.

My opinion about this is you should handle exceptions correctly EVERYWHERE! This involves understanding of when to throw, catch or wrap an exception. For EJB the logic is:

RemoteException: declared on all remote interfaces exposed by EJB. Used to indicate a network problem. You should not throw it yourself. If you call another remote object and receive a remote exception, wrap it in an EJBException.

EJBException: Thrown by developer and caught by container. Indicates major error to signal container to perform a transactional rollback. Like a NullPointerException, it’s a runtime exception and should not be caught by a developer. A common use is wrapping SQLExceptions into EJBException.

Application-level Exception: Should be thrown/caught by developer as part of application logic. Must be serializable (the exception will travel across the network). Do remember Exception Basics: Don’t use exceptions to indicate normal flow.

Using Business Delegates for Clients

This may look like pure redundancy to new-to-EJB programmers, however this cleanly separates client logic and EJB logic. One way to see this problem is when importing EJB packages in servlet classes. If the back end changes to non-EJB or the front end changes to non-webbased you get a problem. By introducing another layer into the already-many-layers you improve scalability and maintainability of your application (theoratically).

Create a Build Environment.

Even if you have a fantastic IDE that can automatically do all these for you, it’s useful to be able to make small changes to your app and re-building it without firing up and twiddling with the (likely-heavy) IDE. This is even more useful if you really understand your deployment. (and useful for people to learn about the actual deployment)

Displaytag taglib

The displaytag library is a useful JSP taglib for generating html tables containing data in lists. By putting JavaBeans in an iterable list, the tag library can format the list into a HTML table, complete with the following features:

  • Mapping columns to bean properties
  • Built-in Pagination
  • Built-in Sorting
  • External sorting/pagination
  • I18n
  • Styles
  • Exporting to PDF/Excel
  • Integration with struts
  • Grouping (never tried)
  • Integration with webwork, sping framework (never tried)

Rating 4 out of 5. Excellent features, easy to use. 1 missing for un-organized documentation.

http://displaytag.sourceforge.net/11/

Apache Jakarta Struts (Action 1)

For the past two weeks I have actually been looking at a lot of Struts, but i’m still not well-versed enough to document it down, also because of the amount of information needed to document it. Seems like it’ll take a few pages.

Briefly going through it, Struts is a web application framework that enforces the MVC model. The sequence generally goes like this:

  1. The user makes a HTTP request through the web (e.g. Employee.do?method=retrieve&empId=23)
  2. The web server receives the request. By servlet mapping in web.xml, *.do requests are handled by the struts ActionServlet. The HTTP request is passed to the ActionServlet, which works as a controller dispatcher.
  3. The ActionServlet receives the request, and checks the name of the .do (Employee). It looks up the action mapping configured in struts-config.xml (or a corresponding module config file). Information regarding the action’s form bean, Action class, validation, mapping forwards are retrieved.
  4. The form bean declares the request parameters as Java data types, allowing automatic mapping to be performed, instead of retrieving all parameters as Strings.
  5. If validation is required, validation is performed based on the form bean types, and any validation.xml as configured in the Validator plug-in. The configuration file allows basic checking such as required fields, less-than/more-than, regex, as well as custom validation providers. If validation fails, the request is immediately forwarded to the validation failure page.
  6. Once validation passes, the Form Bean class is created and populated with the corresponding values in the request. The Action class is then called to perform business logic, using the information in the form bean class. The Action then redirects to the appropriate mapping forwards as defined in the action configuration.
  7. Control returns to the ActionServlet, which looks up the actual URL of the mapping forward in the config file. All these lookups allow complete separation of URLs between the view and model.
  8. The actual URL content is served to the client.

The framework is highly configurable, such as splitting the configuration into modules, using dynamic forms without creating actual form beans, tag libraries that easily display and retain information in form beans, etc.

Apart from Action 1, Struts also evolved into Action 2 and Shale Frameworks, which I’m unsure of the differences.

ClassCastException on RemotePortableObject.narrow

Been working on EJB with JBoss and Eclipse. I must say the XDoclet (with Ant) was a greate help in generating beans and doing packaging. Despite great tools, I met with a major error that had me stumped for 4 hours.

InitialContext ic = new InitialContext();
Object obj = ic.lookup("...");
SomeHome home = (SomeHome)PortablRemoteObject.narrow(obj, SomeHome.class);

A simple lookup like this, with a ClassCastException on the 3rd line. A class cast means the lookup was successful, someHomeObj is not null, but is not of type SomeHome (which happens to be an interface).

It usually means you’re casting to the wrong type, probably a typo or type confusion. But for my case I checked and rechecked and it was no error. No matter what I did couldn’t make the exception go away. This includes reflecting on the class, superclass and the interfaces of the object, to convince myself the types were correct.

// prints $Proxy157
sop(obj.getClass());

// prints java.lang.reflect.Proxy
sop(obj.getClass().getSuperClass());

// prints SomeHome, Handle
for (Class c : obj.getClass().getInterfaces())
    sop(c);

!!! The class actually implements SomeHome!!! But fails the narrow!!! *fumes* I even tested each line inside the narrow() implementation, and despite having implemented SomeHome, it cannot be cast into or assigned as SomeHome. (instanceof SomeHome == false, isAssignableFrom(SomeHome.class) == false, instanceof org.omg.CORBA.Object == false)

After hours of twiddling I finally found out it was because there were TWO of this interfaces being deployed, one in my ejb .jar, the other in my .war classes. The lookup returned the interface from the ejbjar, and was trying to cast it into the war interface. Therefore even though they had the same interface name (from the same package), they were 2 different classes.

To fix this i adjusted my package build, to EXCLUDE the interface classes from my war. A simple rebuild and the narrow works.

Sun Java goes Open Source!

After 2 years of pressure from the open-source community, Sun has finally decided to open-source Java. Sun was previously worried about compatibility and forking, since people could make their own versions or move towards other goals, making Java lose focus. The prime example was Linux. Some say Sun was afraid of losing control of Java. Whether they are really convinced now that open source is the way to go, or to pull up their stagnant stock prices…

In one of the issues in 2004, the president of the Open Source Initiative (OSI) dropped an open letter to then Sun CEO Scott McNealy touting that Sun is just using open-source as marketing but not really supporting it [1]. He compared Java and Sun to Tiger Woods and his dad, who supported his child by recognizing and letting him achieve his full potential from an early age. He says Sun is restricting Java’s potential, and it would not help even if Sun releases it later, as Woods would not have done it too if he started only later.

The saga went on with Sun spokesman replying to the media (but not to the sender) rebutting each of his points in exact quotes, and OSI president writing a second open letter in response…

Sun have announced that they’re doing it for sure, but its CEO is seeking advice on HOW it should be done.

[1] http://www.catb.org/~esr/writings/let-java-go.html
[2] http://news.zdnet.com/2100-9593_22-6072760.html
[3] http://www.vnunet.com/vnunet/news/21562 … ource-java
[4] http://www.iht.com/articles/2006/05/17/business/sun.php

Java Puzzles

Nothing new for a week, but saw a set of puzzles which attempt to promote “good” programming and awareness of bugs that may crop up when bad programming practices are applied.

Some of the morals in the 8 puzzles include:

  • Avoid mixed-arithmetic computation
  • Use parenthesis to make clear your comparison intention
  • Use .equals() for object comparison instead of ==
  • Do not depend on interned string for program correctness
  • Do not assign to a single variable twice in a single statement
  • The postfix ++ operator increments the value, but returns the old value.
  • Document your intention when using & and |. Usually && and || should be used.
  • Never use exceptions for normal flow.
  • Avoid complicated initialization sequences, choose EITHER lazy or eager init, but never both.
  • int arithmetic overflows silently.
  • Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE
  • If you can’t see a method, you cannot override it.
  • Don’t use background threads in initialization – leads to lock-free deadlock!

http://www.javalobby.org/eps/more-puzzlers/

SWT & JFace – Standard Widget Toolkit

SWT is an alternative to AWT and Swing, supported by the Eclipse community. Together with JFace it can be used to build RICH clients. SWT use native widgets, yet code is portable to other platforms. This is achieved by a JNI call-through to the OS library [1]. Widgets are simply Components and JComponents in AWT/Swing, which includes text boxes, push buttons, etc.

JFace provides a higher-level API for SWT widgets, such as an ApplicationWindow or Tables and Trees backed with a data model. JFace also allows direct access to the underneath SWT widgets, so that developers can easily change whats beneath. The whole API is very exposed, in fact, even the threading model and event loop is totally exposed. Whatever thread that created the display becomes the GUI thread, and other resource extensive tasks have to be thread-spawned by the programmer hiimself. The good thing is SWTException is thrown if UI manipulation was not done in the GUI thread.

For existing Swing developers, [2] provides an extremely simple introduction, with several tables to show the relationship between Swing components and SWT Widgets. Also see [3] for a nice list of widget screenshots.

[1] http://www.eclipse.org/articles/Article … ign-1.html
[2] http://www.javaworld.com/javaworld/jw-0 … jface.html
[3] http://www.eclipse.org/swt/widgets/

RCP – Rich Client Platform

Thats the new kid on the block… RCP is a framework that allows developers to quickly assemble rich client components like menus, toolbars, drag/drop functionality, etc into a client application. Currently the most popular RCP platforms are Eclipse and Netbeans.

Basically the IDEs allow developers to build application much like the same way in the old VB4/VB6 days. They still lose to VS.Net when it comes to ease of development. However, consider that a step forward for the Java community.

The idea sounds good as a big picture, but it’s going to be a big headache for those implementing it. These real developers are stepping into the same (dung) as Microsoft programmers. For one, performing tasks are not so straightforward. To get a button on the screen to display a tab becomes a wild-goose chase in the API for which is the correct function to call. More spaghetti code. Second, debugging becomes just like Microsoft: developers don’t know what’s going on behind the scenes and those chain of method calls. Only one in a thousand will have the curiousity (and TIME) to really figure out how things run behind. That leaves the other 999 using trial and error to fix bugs, ugly workarounds as long it works… pastamania code.

Rich clients are not to be confused with thin/thick clients. Before 2003 [1], rich clients meant a thick or fat client. But now a client may be thin AND rich at the same time — having the interactive features of a rich client, yet processing and business logic are handled in a remote server.

[1] http://en.wikipedia.org/wiki/Rich_client

JACOB – Call COM objects from Java

backdating this to when i spoke to KK. He was trying to call an obsolete COM component from Java, and was using JACOB [1]. I’ve not used it myself, but it sounds like a good way if you REALLY REALLY REALLY need to call COM. I hate it, cos once you have JNI with COM, you can’t really run it on non-Windows platform without a change.

If REALLY necessary, eg. interacting with a hardware device, I recommend building an adapter interface (pattern) for the component, so that another adapter implementation can be written when you port it to another OS, and a driver for that OS exists.

[1] http://danadler.com/jacob/