Since last I wrote about integrating Spring with iBatis, and about using the very useful auto-rollback in Spring Test Framework, things have changed. The iBatis project has shifted to Google and is now called MyBatis, and includes numerous API changes. But, this was done after Spring 3.0 was released - so full integration with Spring was left undone at that time. However, a Spring-MyBatis integration component is now available, and here I'll write up how to use it to very simply use MyBatis, preserving the auto-rollback feature.
Thursday, February 17, 2011
Integrate MyBatis 3.x with Spring 3.x
Wednesday, February 9, 2011
Get a Random Value Within a Given Range
Just the code:
public int getRandomValue(long min, long max) {
return (int) Math.max(min, (int) (Math.random() * max) + 1);
}
Use this to (obviously) generate an integer value between (inclusively) the given minimum and maximum - which is useful also to fetch random elements e.g. from a List.
Tuesday, February 8, 2011
Getting Around the Double-Checked Locking Problem
This is just a summary of alternatives to the double-checked locking pattern, which has been shown to have potential problems. The alternatives are taken from a CERT wiki article. The motivations for the pattern come from three concerns:
- avoid constructing an object that is expensive to construct, unless-until it is needed
- avoid more than one instantiation of that object, and
- avoid the overhead of synchronization for the more common case of fetching the object.
Getting Around the Double-Checked Locking Problem
This is just a summary of alternatives to the double-checked locking pattern, which has been shown to have potential problems. The alternatives are taken from a CERT wiki article. The motivations for the pattern come from three concerns:
- avoid constructing an object that is expensive to construct, unless-until it is needed
- avoid more than one instantiation of that object, and
- avoid the overhead of synchronization for the more common case of fetching the object.
Thread-Safe Singleton Using ThreadLocal
Let's say a given component in your application has these constraints:
- it needs a reference to an object that is expensive to construct
- the expensive object state can be set just once, i.e. it never changes
- it executes in a multi-threaded context
- maximum throughput is required
Constraint #1 seems to call for an instance variable - i.e. you don't want to use a method-local field since the object construction is expensive, as such you don't want to do it each time the object is needed. Constraint #2 implies that a singleton will serve our needs. However, constraint #3 means the singleton must be used in a thread-safe manner - and constraint #4 tells us that we want to avoid synchronization if possible.
Wednesday, February 2, 2011
Get Notified When Your Periodic Task Throws an Exception
I've written up a how-to on a periodic task with a watchdog; now I want to handle exceptions and notify interested observers when exceptions occur. The watchdog is in place in case exceptions don't get handled at all, in which case subsequent executions of the scheduled thread will be suppressed. in this case, the watchdog knows that some problem occurred - but it doesn't know just what that was. As an alternative (or in addition) to the watchdog, we simply catch exceptions in the Runnable passed to our Executor, handling it by both logging the information and notifying interested listeners. This helps pass along exception information from a different stack of execution, since the problem occurs in a different thread:
Use Spring to Wire Up Varargs
This describes a technique to provide N objects to a constructor using Spring. Nothing real new or innovative here; just an idiom I wanted to save for future reference. Let's say we have a constructor that uses varargs:
public MockDao(SummaryData...summaries) {
// save the summary data for downstream use...
}
And, we want to use Spring to provide test summary data to our mocked-out DAO.
Encapsulate SSL TrustStore Configuration
Here I'll present the steps of a recent exercise I completed, around configuring SSL on the client side, encapsulating the configuration for transparency. For some background on SSL and the topics discussed in this article, here are some resources:
SSL Protocol Overview
SSL Certificates HOWTO
Transport Layer Security
The Security Now podcast series also has some excellent deep-dives into SSL and many other security-related topics.
Enable Assertions with JUnit Tests in IDEA
My test cases have recently been converted from TestNG to JUnit4, and to my surprise every single test I wrote from that point forward passed without a problem - if I ran the tests in my Intellij IDEA 9.0.4 IDE. Mysteriously, executing them via Maven did not have that same result (whether from command line or from IDEA). As it turns out - apparently JDK assertions are enabled by default when I use TestNG in IDEA, but not so much when using JUnit.
Fix this problem by setting the -ea VM argument for all existing tests, and setting that as a default for JUnit tests created from this point forward.
Arrrggghhhh.