Saturday, 30 April 2011

Null Return Values and the Special Case Pattern

My previous blog talked about the problems associated with nulls being somewhat awkward to deal with and how this problem can be mitigated using exceptions. From my last blog, you’ll also probably guess that I believe that even using exceptions isn’t great a idea and that the best approach is to force your objects to do even more work. This blog uses the same contrived employee details scenario to implement Martan Fowler’s Special Case Pattern.

Friday, 29 April 2011

Dealing with Null Return Values

One of the things most of us do is to return null from their methods without questioning the validity of the technique. It should be widely excepted, but isn’t, that returning null is a bad idea as it forces any and all client code to constantly check method return values and take special action when they are null. Not only does your client code need to constantly check for null there is also the increased risk of a NullPointerException as it’s easy to miss a out a check altogether, which from experience, is a common occurrence.

Thursday, 28 April 2011

Oracle EZCONNECT

Oracle EZCONNECT is a database connection technique that does away with the need to look up service names in tnsnames.ora when connecting via a network. It provides for connection by enhancing the original connection string format of:
CONNECT username/password@service_name
The updated format allows for the use of a connection URL to enhance the service_name:
CONNECT username/password@//host[:port][/service_name]
Where host is either the host name or IP address of the database, port is an optional port number defaulting to 1521, whilst service_name is the database service name, which defaults to the host name.

For example:
CONNECT Scott/tiger@//10.101.0.45/employee.world
A complete description of this is available on wikipedia.

Wednesday, 27 April 2011

Using an Oracle Function Index as a Constraint

Two previous blogs of mine have talked about Oracle’s Function Based Indexes:

In what should be the final blog on the subject I’m going to cover a more complex use of function based indexes to enforce a rule-based constraint.

Tuesday, 26 April 2011

The Difference Between Method Overloading and Method Overriding

When taking the Oracle Certified Professional, Java SE 6 Programmer exam, one of the tricks used to try to catch you out is to try to confuse you over the meanings of overriding and overloading methods and I guess that this is because they have similar sounding names. This blog clarifies all that - take a look at the code...

Monday, 25 April 2011

hashCode() Tips.

Did you know that a hash code MUST be a positive value and that String uses the hashCode() method inherited from Object.

Sunday, 24 April 2011

Exception Ordering

Exceptions caught in a catch block must be caught in order of precedence, starting with the most specific and ending with the most general. The following code will compile:

Saturday, 23 April 2011

The Evolving switch Statement

One thing about working in programming is that things change; you can count on it, new things are written, tools become in vogue and techniques go in and out of fashion. One thing that changes somewhat less often is the Java language itself, though it is evolving and for the better.

Consider the humble switch statement: at the time of J2SE 1.4 you could make a choice based upon an int value:

Friday, 22 April 2011

chars and shorts: useful stuff to know?

It seems logical that the compiler should be able to automatically convert a char into a short, after-all they’re both 16 bits wide...

Thursday, 21 April 2011

Qualifying @Ejb Annotation to Resolve Injection Problems

A few days ago, I wrote a blog about simplifying EJB3 and Spring by using Spring’s SpringBeanAutowiringInterceptor which allows you to dispense with a certain amount of boilerplate code. In writing my sample code (remember the golden rule: never use a technique, api, pattern or algorithm professionally until you’ve used it to write some sample code) I did some rough refactoring of an existing EJB3 sample with the result that I had, in my JAR file, two implementations of an @Remote interface.

Wednesday, 20 April 2011

Floating Point and Divide By Zero

Did you know that dividing a double by zero will not throw an ArithmeticException? This seems rather inconsistent to me as dividing an int by zero will.

Tuesday, 19 April 2011

Subversion Error: Failed to get lock on destination repos

During a regular copy of my main Subversion repository to a mirror I got the following inexplicable error message:

Monday, 18 April 2011

Using a Spring ContextSingletonBeanFactoryLocator

When writing EJBs using Spring you often bump up against the ContextSingletonBeanFactoryLocator, a class that extends SingletonBeanFactoryLocator. As the name suggests, it’s used to create a singleton Spring context, and this is useful in EJB development as it seems preferable for all EJB instances in your bean pool to share the same Spring context: indeed I have fixed bugs in EJBs that were the result of loading one Spring context per EJB instance.

Sunday, 17 April 2011

Auto-wiring EJB3 Beans with the Spring SpringBeanAutowiringInterceptor

The usual technique for creating an EJB using Spring is to extend AbstractStatelessSesstionBean and then to use the ContextSingletonBeanFactoryLocator to load your configuration via a beanRefContext.xml file. You would then load your Spring beans by overriding onEjbCreate():

Saturday, 16 April 2011

Unexpected Spring Exception

One of my golden rules of coding is never to use a technique, api, pattern or algorithm until I’ve used it to write some sample code. With this in mind I took a simple EJB3 stateless session bean I’d previously written with the aim of adding a Spring SpringBeanAutowiringInterceptor, which I’d never used before. This shouldn’t have been too difficult as the code built, ran and passed all its tests. So, I wrote the code and created the simplest of JUnit tests that loaded my Spring context from a simple XML file:

Friday, 15 April 2011

Code Optimization

Two rules of code optimization: 

  1. Don't do it.
  2. For experts only: don't do it yet. -- M. A. Jackson 1975


Thursday, 14 April 2011

Using Spring JmsTemplate

One very useful reoccurring Spring pattern is the use of template helpers that take hard work out of some very boring day to day stuff. The guys are Spring have provided a considerable number of template classes including JdbcTemplate, JdoTemplate and SqlMapClientTemplate. One of the most useful is the JmsTemplate, which is a way of managing JMS connections. It is incredibly flexible as it works in collaboration with several other helper classes including JndiTemplate, JndiObjectFactoryBean and JndiDestinationResolver.

Wednesday, 13 April 2011

Setting Null Values in Spring Config

There are some occasions when you need the ensure that null values are passed to objects defined in Spring XML. Now, with you own objects you don’t usually need to bother with this as you can design then to ensure that attributes are initialised to null during their creation.

Tuesday, 12 April 2011

Comments in Your Code

Are comments a good thing? I know I used to believe that they were for years, starting with my days of writing Basic on an Apple IIe, through C, C++ and finally Java. But technology changes, techniques evolve and somehow, in today’s world comments in your code usually aren’t all they’re cracked up to be.

Monday, 11 April 2011

More Spring Java based DI

Yesterday’s blog covered the new Java based dependency injection (DI) feature of Spring 3 demonstrating how to create and instantiate a simple factory class using @Configuration annotation. So far so good, but those clever chaps at Springsource have thought this through quite thoroughly and provided much more flexibility than I demonstrated yesterday.

This blog goes into slightly more depth than yesterday’s demonstrating how to use some of those additional features.

Sunday, 10 April 2011

Spring's Java Based Dependency Injection

One of the new features of Spring 3 is the ability to configure your good old POJOS using a new Java based dependency injection (DI) feature. This was formerly known as Spring JavaConfig and, since Spring 3 now only supports JSE 5 and above, it’s been incorporated into the core Spring Framework. This is an annotation based DI where the beans or POJOs that you’re linking together remain untouched by annotations and, being Java based, you get a lot more programmatic control over object instantiation.

Saturday, 9 April 2011

Checking for Well-Formed XML

Checking For Well-Formed XML

In several SOA projects I’ve worked on the test team generate their test XML by hand, which is a long winded, error prone business. The problem with this is that stuffing lots of broken XML into your code generates lots of invalid bugs. A top tip to negate this problem is to always check that any XML passed as arguments to your code is well-formed - and do this first. The code below demonstrates how to do this using a validator class.

Friday, 8 April 2011

Double.NaN Definition

The Double class defines a little understood value of NaN - or ‘Not A Number’. This sounds pretty simple at first glance, after-all an alphanumeric String is not a number - isn’t it?

Thursday, 7 April 2011

Comparing StringTokenizer and String.split(...)

People often say that String.split(...) and the older StringTokenizer are interchangeable and that these days you should use Sting.split(...) in preference to StringTokenizer. But, are they as interchangeable as they’re made out to be or are there some input conditions where there results different? The code below tests this theory using four input strings:

Wednesday, 6 April 2011

Different Ways to Create Objects

There are at least four ways to create a new object:

1) Using new

Tuesday, 5 April 2011

Oracle JDBC Connection Properties

There are several things to remember when configuring an Oracle JDBC driver connection. Firstly, you need to specify the driver. The driver class is oracle.jdbc.driver.OracleDriver located in ojdbc14.jar or ojdbc14_g.jar (the debug version) archive files.

Monday, 4 April 2011

MySQL JDBC Connection Properties

There are several things to remember when configuring a MySQL JDBC driver connection. Firstly, you need to specify the driver and the driver class is com.mysql.jdbc.Driver located in the mysql-connector-java-5.x.x.jar archive file and available for download from http://dev.mysql.com/downloads/connector/j/.

Sunday, 3 April 2011

Enabling Oracle Function Based Indexes

A few days ago, I described a simple example of how to create and use Oracle Function Based Indexes. However, and this is a big ‘however’, before you can use Oracle Function Based Indexes, there are several tasks to complete in setting up your database correctly.

Saturday, 2 April 2011

Using MethodInvokingFactoryBean to call setters with multiple arguments

There is the odd occasion when you want to load a class in your Spring configuration and call an initialisation method that takes two or more parameters. The whole point of Spring is that you use it to create simple POJO beans, so having to call an initialisation or ‘setter’ method with more than one argument is unusual, but not impossible.

Friday, 1 April 2011

Moving to the Spring 3 BeanFactory

My previous blog on Updating to Spring 3 covered a few points about upgrading existing projects to use the Spring 3 libraries. This included Maven Pom file changes, highlighted links to Spring resources and documented the Spring 3 schema declaration.