An example of a resource limitation Moving things to a singleton resource object handles this problem

6.2.1.3 An example of a resource limitation

Suppose youre in a situation where you want to log information about requests that are made. For example, you might want to use a log file for one or more of the following activities: • Debugging servers during initial deployment and testing • Recording information about actual server failures in long-standing systems • Providing detailed data for analyzing network traffic patterns • Providing tracking information in case a transaction is later disputed • Providing a local persistent store to help servers recover from crashes The first two of these are important but dont really concern us. But the last three can be quite important and useful. [ 5] If you have a large number of server objects residing inside a single JVM, and each of them wants to use a log file, you can wind up running out of file descriptors very quickly. [ 6] [ 5] Indeed, a quick search on the Internet reveals the existence of at least 12 companies selling products that analyze web-server logs to help web-site owners spot traffic patterns and fix problems. [ 6] Another example of this would be a mail server which stores each persons mail in a separate file and has multiple servers which implement the Mailbox interface.

6.2.1.4 Moving things to a singleton resource object handles this problem

Of course, now that weve discussed log files in detail, youve probably guessed what the real issue is. Its not the log aspect, its the file aspect that causes problems. If each of the servers, for example, needed to open different files based on client requests, then changing our design to use a single server wouldnt help us. We would still need to open all those files, and that would still use up our file descriptors. The real issue is that we could have gotten away with a single log file, but instead we used many log files. Hence, there is a solution to this problem: if we need to log client requests, and we want to have several small-scale, semi-independent servers in the same JVM, then we can do one of two things. First, each server can attempt to open a single, shared log file when it needs to record information. If the attempt fails, it will wait and then try again. Having a single log file solves the resource issue™we no longer use a plethora of file descriptors for what is a secondary piece of functionality. However, having each account open and close the same file is incredibly inefficient. The same file winds up getting opened and closed repeatedly. Moreover, since accounts can now block each other i.e., when one has the file open, the others have to wait, we run headlong into threading and timing issues. Well deal with threading in Chapt er 11 and Chapt er 12 . Instead of having each server open and close the same file, it makes more sense to implement a singleton object, a logger, residing in the JVM. Rather than attempting to open the file directly, the servers simply call methods on the logger, which handles the details of the filesystem interaction, as shown in Figur e 6- 2 . Figure 6-2. A singleton logger can often minimize consumption of scarce resources The heuristic that comes from this discussion is that if our design starts relying too heavily on shared resources, then were implicitly creating dependencies and resource contention among our servers. This means we should encapsulate those resources inside code that manages access to them. Shared singleton resource is a design pattern that applies in many cases. The most common case is database connection pools. Recall that database connection objects are incredibly expensive to create. You can usually have only a small number of them; otherwise, youll run out of database cursors. To solve this problem, most programmers create a connection pool: a set of database connection objects that are repeatedly used. A server grabs a connection from the pool, uses it, and then returns it to the pool so other servers can use it.

6.2.1.5 Applying this to Bank versus Accounts