In this chapter we will examine the different ways that a developer can implement the singleton design pattern, its common usage and pitfalls. We start by using static members and methods and demonstrate the problems this causes in multi-threaded environments.
We will see the advances that were made in Java SE 5 with the introduction of the enum type and how it can be used to create thread safe singletons. Then we will discuss the use of the @Singleton annotation in Java EE and how this has radically changed the way we implement the singleton pattern in session beans.
We will introduce the use of BEAN and CONTAINER managed concurrency and we will see how the use of the @LockType annotation controls access to business methods. We finish the chapter with a discussion of the main issues that have dogged the singleton pattern and why it is considered an anti-pattern and has fallen out of favour.