Re: Refinement of a Java prog called by PHP
Rose wrote:
Finally I followed Donkey
Hot's advice by adding "-cp .:./mysql_jdbc.jar" and it works now. Indeed, I
don't know what the arguments are for, neither do I understand Lew's
comments on "driver, <-- do you mean the jar? indeed it is a file given to
me, i don't know where it comes from", "load the class once <-- how to tell
the prog to load it only once in a web environment?"
Excellent question. The 'Class.forName()' expression is what loads the
driver. Class loading is one of the Dark Arts. Suffice to say that, for any
given run of the JVM, you usually only have to refer to a class once to get
its magical class loader to load the definition of the class in memory.
In the case of JDBC drivers there's more than a little chicanery involved.
JDBC drivers have a set of secret promises to keep, which they do when they
are loaded into the JVM. Class.forName() forces a load of the named class,
which then goes about keeping all its secret promises inside its static
initializer. Its main job is to register itself with the DriverManager, but
it conveniently hides that fact from you, the programmer.
Once that is done, every subsequent call to Class.forName() for the same
driver does no further good. It doesn't really do any harm, either, except
spin a few cycles uselessly checking. The best way to handle it is to include
it in the static initializer of your own custom data-access base class, or the
init() of a servlet, or some such block of code that you can guarantee to run
before an actual database call.
I called this small matter a "side note" because it is not the most critical
issue that data-access code faces, but it is part of a larger "initialize,
prepare, execute, clean up" gestalt.
--
Lew