I recently have been working with Tyrex's MemoryContext
as a stub JNDI provider in my application, and have
found that it doesn't follow the behavior of the Tomcat
JNDI provider (quite probably other JNDI providers as
well).
What is happening is, on every lookup of a DataSource
that is bound in JNDI via a Reference, the data source
is created, returned to the caller and then immediately
discarded. The DataSource then gets garbage collected,
and closes its resources in the finalizer, which closes
the open connections that the application code is using
aburptly and asynchronously.
In order to implement a connection pool, the pool must
stay referenced in memory across JNDI invocations. The
way tomcat does this is on the first lookup, it
clobbers the Reference entry it has stored and replaces
it with the object that it referred to. This keeps the
pool alive for the duration of the life of the JNDI
provider.
The attached patch does this to the Tyrex
MemoryContext. The diff is against the head of CVS, and
has been tested fairly extensively and shown to work
correctly.
cache resolved references