Re: Detecting multiple class loaders
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Thu, 20 Mar 2008 00:36:04 -0500
Matt Humphrey wrote:
"Chris" <spam_me_not@xxxxxxxxxx> wrote in message news:47e12e73$0$29056$9a6e19ea@xxxxxxxxxxxxxxxxxxxxxxx
Is there any way to detect when a class has been loaded by two different class loaders?
Our app has a class that reads and writes a file on disk. The file gets corrupt if more than one process gets at it at once. We can detect multiple processes by locking the file on disk and throwing an error; no problem. We can control access by multiple threads using synchronization. So far, so good.
The problem is Websphere. When this class gets used in a webapp, Websphere, like most other app servers, creates a classloader just for that webapp. But it does more than that; it creates multiple classloaders
Have the writing code allocate a server socket on a rarely used port. If successful, the file write can proceed. Otherwise, someone else has the socket and is writing. Afer writing, release the socket. Note that the socket is never actually used. Sockets are a global resource independent of classloaders and allocation qualifies as atomic test-and-set.
Alternatively, I have never tried using it, but from the javadocs
it would appear that java.nio.channels.FileChannel's lock() could
maybe do the locking in a global way. That method returns a
FileLock, and the documentation for FileLock says,
This file-locking API is intended to map directly to
the native locking facility of the underlying operating
system. Thus the locks held on a file should be visible
to all programs that have access to the file, regardless
of the language in which those programs are written.
That would certainly imply the scope of the lock goes beyond
the classloader and beyond the JVM as well.
The advantages of this over the socket method is that (a) you don't
grab a serversocket port that somebody might actually *need*, and
(b) you don't have to worry about maintaining some mapping between
pathnames and port numbers. The disadvantage is that the behavior
is platform-specific and not exactly completely guaranteed to do
what you want, maybe.
- Logan
.
- Follow-Ups:
- Re: Detecting multiple class loaders
- From: Matt Humphrey
- Re: Detecting multiple class loaders
- References:
- Detecting multiple class loaders
- From: Chris
- Re: Detecting multiple class loaders
- From: Matt Humphrey
- Detecting multiple class loaders
- Prev by Date: Re: Sell - IDE IntelliJidea 7.0 personal license
- Next by Date: Re: Looking for advice on GlassFish and JEE
- Previous by thread: Re: Detecting multiple class loaders
- Next by thread: Re: Detecting multiple class loaders
- Index(es):
Relevant Pages
|