You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by sr...@gmail.com on 2015/02/18 15:25:46 UTC

Why is Class.forName required to load drivers in Tomcat 8 ?

I am writing a simple Servlet that uses JDBC to talk to MySQL and deploying it in Tomcat. Why do I need to add the line -:

Class.forName("com.mysql.jdbc.Driver");


to load the JDBC driver ? If I don’t do this I get an exception of -:


java.sql.SQLException: No suitable driver was found.


Why is it when I use a JDBC connection in Servlets and deploy it in Tomcat must the class be loaded dynamically ? Why can’t Tomcat do that for me when I have already added it to the build path ?


From Java 6 onwards there is no need for the JDBC Driver to be loaded dynamically. So why when using JRE 8 and Tomcat 8 must this be done ?


Cant it be loaded automatically like for normal apps ? It sure would be a lot more cleaner code.


Regards
Sreyan Chakravarty

Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by sr...@gmail.com.
What is the SYSTEM PATH that you are referring to ? Is it the JAVA_HOME or the PATH system variable ?


What is the CLASSPATH just before Tomcat properly starts up ? What is it after it starts up ? Is the same jar files below -:


bin/bootstrap.jar
bin/tomcat-juli.jar


Correct me if I am wrong that means that whatever dependencies I have in my project is not added to the SYSTEM CLASSPATH but is instead loaded by TOMCAT. So if a module searches only the SYSTEM CLASSPATH it won’t be able to find the dependencies. Am I right ?


Regards
Sreyan Chakravarty





From: Christopher Schultz
Sent: ‎Wednesday‎, ‎February‎ ‎18‎, ‎2015 ‎8‎:‎30‎ ‎PM
To: Tomcat Users List





-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

On 2/18/15 9:25 AM, sreyan32@gmail.com wrote:
> I am writing a simple Servlet that uses JDBC to talk to MySQL and 
> deploying it in Tomcat. Why do I need to add the line -:
> 
> Class.forName("com.mysql.jdbc.Driver");
> 
> to load the JDBC driver ? If I don’t do this I get an exception of 
> -:
> 
> java.sql.SQLException: No suitable driver was found.
> 
> Why is it when I use a JDBC connection in Servlets and deploy it
> in Tomcat must the class be loaded dynamically ? Why can’t Tomcat
> do that for me when I have already added it to the build path ?
> 
> From Java 6 onwards there is no need for the JDBC Driver to be
> loaded dynamically. So why when using JRE 8 and Tomcat 8 must this
> be done ?
> 
> Cant it be loaded automatically like for normal apps ? It sure
> would be a lot more cleaner code.

My guess is that DriverManager only searches the system CLASSPATH
which only contains the following JAR files when launching Tomcat
using the standard scripts:

bin/bootstrap.jar
bin/tomcat-juli.jar

The bootstrap class then configures a new ClassLoader (including
lib/*.jar) for the Tomcat server and Tomcat properly launches using
/that/.

I think your JDBC driver simply isn't visible to DriverManager's
auto-loading. But when you call Class.forName, the driver registers
itself with DriverManager, which is okay to do if DriverManager didn't
know about your driver.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU5KkJAAoJEBzwKT+lPKRYKxEP/RXZTiVXkiIGl2ALwgAgNL/C
BjekoBb7b9xoKqm0WBK4mInZ9WW1pEnAkvSDsXyRZ3nrqGBXFtIatehBYo4fFizL
W+e7vM8v3C+lruqqSSEPH2Mn2Dx0AO/zT/bu+di+N0Q1ZYWvgWukw1DVoKMacZAN
23dmkR62IbfwprnZ83hWhJimIe926EpooMJ+ZgaccKxTdIPHo6RNJBnsFdiTr2o4
ZqtF/8m4ShDipAyF8fmhsa4w3rEUYsgV1CN+tjDtSEpJ9irTMCEZY3tHlElUhNgR
rg9XlkHGH+9WufJeGujZEndG3x9ey0tZM8jxm5kmty2G9w3hq12EavncH9/JyjCk
fdUX/xMLgwZQWDMPvgqgZeH8+jgYhdbDY+RTPTTCLulu+Qy6uz+/ijjPGX/dUtez
NSIXFgMu7j0tzLU3Z2FIdTvZlI9SpotndQ34ddT4HBqo35ooJw7fwDCeh/SW3UMm
IaUxdzroRRksY5TU2ZqifcF3m2cKUxXX5O454nSa0z2DdWCBe7JM2cEKPP1NNn4L
uiEBxt/7VWCl4ZIrZcqjXn6IHG2lYyeTpYL+x+KUUpgEwOZnSlbOyWTje2FJ6UNb
K7RIR3ITGyMDXnpAgFnt45jStCSkqHilI+ru3WplD3aEJf3xN+c9i9t6EAZrq04C
eK2ta2Y6JGU3JuKoazol
=06fK
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org

Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

On 2/18/15 10:15 AM, sreyan32@gmail.com wrote:
> What is the SYSTEM PATH that you are referring to ? Is it the 
> JAVA_HOME or the PATH system variable ?

Neither of those. It's commonly referred to as the CLASSPATH
environment variable, which should pretty much never be set because
it's horribly confusing.

Instead, Tomcat uses "java -classpath [the classpath]" to launch the
JVM. The classpath used there includes two JAR files:

> 
> 
> What is the CLASSPATH just before Tomcat properly starts up ? What
> is it after it starts up ? Is the same jar files below -:
> 
> 
> 
> 
> Correct me if I am wrong that means that whatever dependencies I
> have in my project is not added to the SYSTEM CLASSPATH but is
> instead loaded by TOMCAT. So if a module searches only the SYSTEM
> CLASSPATH it won’t be able to find the dependencies. Am I right ?
> 
> 
> Regards Sreyan Chakravarty
> 
> 
> 
> 
> 
> From: Christopher Schultz Sent: ‎Wednesday‎, ‎February‎ ‎18‎,
> ‎2015 ‎8‎:‎30‎ ‎PM To: Tomcat Users List
> 
> 
> 
> 
> 
> Sreyan,
> 
> On 2/18/15 9:25 AM, sreyan32@gmail.com wrote:
>> I am writing a simple Servlet that uses JDBC to talk to MySQL and
>>  deploying it in Tomcat. Why do I need to add the line -:
> 
>> Class.forName("com.mysql.jdbc.Driver");
> 
>> to load the JDBC driver ? If I don’t do this I get an exception
>> of -:
> 
>> java.sql.SQLException: No suitable driver was found.
> 
>> Why is it when I use a JDBC connection in Servlets and deploy it
>> in Tomcat must the class be loaded dynamically ? Why can’t Tomcat
>> do that for me when I have already added it to the build path ?
> 
>> From Java 6 onwards there is no need for the JDBC Driver to be 
>> loaded dynamically. So why when using JRE 8 and Tomcat 8 must
>> this be done ?
> 
>> Cant it be loaded automatically like for normal apps ? It sure 
>> would be a lot more cleaner code.
> 
> My guess is that DriverManager only searches the system CLASSPATH 
> which only contains the following JAR files when launching Tomcat 
> using the standard scripts:
> 
> bin/bootstrap.jar bin/tomcat-juli.jar
> 
> The bootstrap class then configures a new ClassLoader (including 
> lib/*.jar) for the Tomcat server and Tomcat properly launches using
>  /that/.
> 
> I think your JDBC driver simply isn't visible to DriverManager's 
> auto-loading. But when you call Class.forName, the driver registers
>  itself with DriverManager, which is okay to do if DriverManager 
> didn't know about your driver.
> 
> -chris
> 
> ---------------------------------------------------------------------
>
>
> 
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU5Pl4AAoJEBzwKT+lPKRYZgYQALnUclLfhNuu+U9nEGTpOrBi
RbdNgBhQIO5cVvtSrlHRJY5+6V0ltSauDE9A7hdt7ULdo0f1RQ5KS4Km0CCuZX0G
X2/wqGl8Z5tEfhsSaYCMDRNBsH9ggyJ7araVJC702PfBhLMkhzOqk7aove3ZB8sp
1VAjQ0qS+l8WwwHx7oa8az4CO3HKYo4Ow7wClO9qZ1VI4jtOGOUR8KC8am12IB+q
/fu4SjAFafwpjP/WeEn4wD/Ut60dc1DllFKKTBNLj5euMGr3yyxALgxaai8gu0Y9
6tMFVWyRsmIg/D+uvibXMNBQa9vIIsPP2t+GpWhPUEbvsW0RQEbu19+fxjoVphhz
leDi77G4TNN3VamW+1IkvhySKJFi/UJNai32E58sH1LQgCi2A7toyctSsxb/D6bU
w+mX8YU3s97YQO4tNMGLtC5PTGlr31dAXFn7ysjrCtp5QyAGrsj9hFDq025oN+Fa
1njc2OlYFtu6opzG0eyVQJ4/15bR2NTw+Z7BfyzLpsZt8XvKyUwMI88GmQzXUuIE
KwGxv27XGSQz1nI/GtiFRof/RRzmXDAgLpgSu8Mzj07bmuiEJ+MNLU02UCgvlpLh
CbkcvbbwweANEKtxsP/c1Exhi0WTJ98ovvgtOH8B1hRXeBiQ9GCB+s/krIOoYWdl
yCvbirCURAzVP8qiv172
=eu3o
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

On 4/23/15 9:22 AM, Sreyan Chakravarty wrote:
> So what happens when I add something to the build path of a
> web-app?

The "build path" is an Eclipse concept that has no meaning after the
application has been built.

> The dependency is not registered?

What dependency? Where would it be registered? The only think Java
knows about is the current thread's context ClassLoader and what it
can see. And the DriverManager always uses the System classloader to
find drivers automatically. Have a look at the source code for
DriverManager. It should be in src.zip:java/sql/DriverManager.java.
The method you're looking for is "loadInitialDrivers" (at least in
Oracle's JRE.. it's not a public part of the API).

> Also does that mean that whatever dependency I need I will have to 
> put it in the /lib folder?

What other dependency?

> I just don't understand why it doesn't work if I add it to the
> Build Path from Eclipse like I do for a normal Java Application.

Because Tomcat does ClassLoading in a more complicated way than with a
standard Java application, where every library is just thrown into the
system classpath.

Tomcat does this to enforce certain security requirements, to provide
insulation against other components, to support embedded usage, and to
support plugability of certain features.

I'm sorry to say that you simply must call Class.forName to load your
driver. If you use either of Tomcat's built-in connection pools (one
based upon commons-dbcp, the other called tomcat-pool), you won't have
to call Class.forName because the pool will do that for you.

You don't have to put the JDBC driver into Tomcat's lib/ directory;
you can put it into your web application's WEB-INF/lib directory
instead, just like all the other libraries that your web application
requires. But be aware that *many* JDBC drivers are poorly-written,
and can cause all kinds of memory leaks when the application is
undeployed or re-loaded.

Hope that helps,
- -chris

> On Thu, Feb 19, 2015 at 2:19 AM, Christopher Schultz < 
> chris@christopherschultz.net> wrote:
> 
> Sreyan,
> 
> (Sorry, I sent way before I was done writing. Full intentional
> message follows.)
> 
> On 2/18/15 10:15 AM, sreyan32@gmail.com wrote:
>>>> What is the SYSTEM PATH that you are referring to ? Is it
>>>> the JAVA_HOME or the PATH system variable ?
> 
> Neither of those. It's commonly referred to as the CLASSPATH 
> environment variable, which should pretty much never be set
> because it's horribly confusing.
> 
> Instead, Tomcat uses "java -classpath [the classpath]" to launch
> the JVM. The classpath used there includes two JAR files:
> 
>>>> bin/bootstrap.jar bin/tomcat-juli.jar
> 
> So the only thing that javax.sql.DriverManager can scan is the
> boot classpath (that includes all the JVM internals) plus the
> system classpath (which includes just those two specific JARs
> above).
> 
>>>> What is the CLASSPATH just before Tomcat properly starts up?
> 
> The boot class path includes JVM stuff (rt.jar, etc.)
> 
> The system class path includes anything the JVM environment was 
> instructed to load when launched (either from the CLASSPATH 
> environment variable, or from the "-classpath" argument on the 
> command-line).
> 
> Tomcat's bootstrap class creates a new ClassLoader which includes 
> everything in lib/*.jar and then starts the Tomcat container using 
> *that* as the effective "class path".
> 
>>>> What is it after it starts up?
> 
> Well, Tomcat can see everything in the boot and system class paths 
> (ClassLoaders) plus everything in lib/*.jar.
> 
>>>> Correct me if I am wrong that means that whatever
>>>> dependencies I have in my project is not added to the SYSTEM
>>>> CLASSPATH but is instead loaded by TOMCAT.
> 
> Correct.
> 
>>>> So if a module searches only the SYSTEM CLASSPATH it won’t be
>>>> able to find the dependencies. Am I right ?
> 
> Correct. And DriverManager can't search *down* into ClassLoaders
> it doesn't know about -- including Tomcat's ClassLoader. So, 
> DriverManager can't load your JDBC driver using its standard
> detection algorithm.
> 
> So, you still need to call Class.forName to register the driver.
> 
> -chris
>> 
>> ---------------------------------------------------------------------
>>
>> 
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>> 
>> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJVOkZpAAoJEBzwKT+lPKRYxIoQAI4rf1nfa5RWsMWFHb98dIDi
/1KHw9EgjtUI0n+HP5AthmeHYUhXolrUsiFnx+gfmspViKF/wTs0iVUkyZjRTPF0
m1v3DNfy/VvvRpXiEyLUJbvSfzkLo+Axg3tH/licfcZbhKi16ubeGDUWYq9juSBm
O8GiYrifkJbYVNYU/FT1wlp68agvgQlYDqygrVz3bhv5rYVlCfX/ja5LUyZPlJeu
556cLNmV0tNAVG+dpK2wfz/uZoa6QrO9CR02GCdSqq+opBiQN2uxwweiY5Zl2REf
N3G37ixDEa2hTJLb0H3YBymIZfSB5jxe2AAOV+iVwTMMznl1wClR9matzF/w3NP8
RgJimcAgDOdAILE1I+J9p8Tl6DMPR13vJc9IF0GzUWoApiZ9MS+6Q4PUWFuFVxTY
oUTiXeeWY6FkBj4GdCx38tk4r2+6Vh3COnzK0E0wTxpAtJIC6P8zsFtXouS8zjm5
8hQl2aMKrumYE7ZC1RuKQnJ5QGWGMcyb+RbVEoXPOKjYDVl3sxZPjn23gd0zjlyd
DgsSKhH8xz44eVcRzBfrYTd59Lod2XxWmlbVJOLbfw6ntergPHc4eUplP7v8uppf
cZh4E0JaAOePo/XSU4R0GkIYym54dsci/adkakTaI/l00pai4XNUj0jORFnv2h18
HRDS3U/QXBKHN6aL8liJ
=p+Tj
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by Sreyan Chakravarty <sr...@gmail.com>.
So what happens when I add something to the build path of a web-app ? The
dependency is not registered ? Also does that mean that whatever dependency
I need I will have to put it in the /lib folder ? I just don't understand
why it doesn't work if I add it to the Build Path from Eclipse like I do
for a normal Java Application.

On Thu, Feb 19, 2015 at 2:19 AM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Sreyan,
>
> (Sorry, I sent way before I was done writing. Full intentional message
> follows.)
>
> On 2/18/15 10:15 AM, sreyan32@gmail.com wrote:
> > What is the SYSTEM PATH that you are referring to ? Is it the
> > JAVA_HOME or the PATH system variable ?
>
> Neither of those. It's commonly referred to as the CLASSPATH
> environment variable, which should pretty much never be set because
> it's horribly confusing.
>
> Instead, Tomcat uses "java -classpath [the classpath]" to launch the
> JVM. The classpath used there includes two JAR files:
>
> > bin/bootstrap.jar bin/tomcat-juli.jar
>
> So the only thing that javax.sql.DriverManager can scan is the boot
> classpath (that includes all the JVM internals) plus the system
> classpath (which includes just those two specific JARs above).
>
> > What is the CLASSPATH just before Tomcat properly starts up?
>
> The boot class path includes JVM stuff (rt.jar, etc.)
>
> The system class path includes anything the JVM environment was
> instructed to load when launched (either from the CLASSPATH
> environment variable, or from the "-classpath" argument on the
> command-line).
>
> Tomcat's bootstrap class creates a new ClassLoader which includes
> everything in lib/*.jar and then starts the Tomcat container using
> *that* as the effective "class path".
>
> > What is it after it starts up?
>
> Well, Tomcat can see everything in the boot and system class paths
> (ClassLoaders) plus everything in lib/*.jar.
>
> > Correct me if I am wrong that means that whatever dependencies I
> > have in my project is not added to the SYSTEM CLASSPATH but is
> > instead loaded by TOMCAT.
>
> Correct.
>
> > So if a module searches only the SYSTEM CLASSPATH it won’t be able
> > to find the dependencies. Am I right ?
>
> Correct. And DriverManager can't search *down* into ClassLoaders it
> doesn't know about -- including Tomcat's ClassLoader. So,
> DriverManager can't load your JDBC driver using its standard detection
> algorithm.
>
> So, you still need to call Class.forName to register the driver.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1
> Comment: GPGTools - http://gpgtools.org
>
> iQIcBAEBCAAGBQJU5PrwAAoJEBzwKT+lPKRY0/kP/Raf7VLP18ZpzXlLqK+ehPW5
> 1tDiJAG/uiz9L+v3/cOgmqcabHI7m4BSGdd8eeW2G8CYNpjRYDRTjq8dGVPkkzO6
> 4ZR3CX2Hgawxw7Qg8hnDh+Fjq+o7yOclY9T0rQFzcP4UnS4cRnRDRHGZtADycP9Y
> Fdgnq5ey/NloTYaUioAmdnL1wnj9pzhRkpzztI8fxhQ1q2AdkLojMUSOArlfg1X5
> N/aSTA0ULhB8Socxiln6TSnjAMxoe2/1PCNwLcIzqLKJcN3zA2rt4eAf5F2JbC50
> zJtfVNa5CEZa3W0Gj5Arl3YWp0rrBa/fNdv7j8iPoF7WZOJbACkZEYuQeS5U3SyS
> kyPhKAkE0b5wICzT+H7VUkNCRi0Gb02ER+QFg/UnoYyX91HHJPKSXfPLmD1fraq0
> YkrhmrXp2O7rRa7YiIDp0yL8d7tNppT7sObyzKzZ1TI7Bf5DXEFsx82aXXWQ7wO4
> VOJh14CuaaBhmxWei3w+pUsoLxe359Fmj2cGbAjfCxGUqJhyZgltjMWCvagXM119
> wg645c9nD71hpX1VpHiPQmougHB8a/w1ripd5hVwdT0NRnlYYG1oDKgT83dEYnkk
> 7yVnuGSA/upinWjdV+GjWCgLr1Kx8O0B4isX0yqBYXtQXh8m4/jDbFGx0V1mGFXW
> aMQS15YHd+kU8oB65ioP
> =sv9k
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

(Sorry, I sent way before I was done writing. Full intentional message
follows.)

On 2/18/15 10:15 AM, sreyan32@gmail.com wrote:
> What is the SYSTEM PATH that you are referring to ? Is it the 
> JAVA_HOME or the PATH system variable ?

Neither of those. It's commonly referred to as the CLASSPATH
environment variable, which should pretty much never be set because
it's horribly confusing.

Instead, Tomcat uses "java -classpath [the classpath]" to launch the
JVM. The classpath used there includes two JAR files:

> bin/bootstrap.jar bin/tomcat-juli.jar

So the only thing that javax.sql.DriverManager can scan is the boot
classpath (that includes all the JVM internals) plus the system
classpath (which includes just those two specific JARs above).

> What is the CLASSPATH just before Tomcat properly starts up?

The boot class path includes JVM stuff (rt.jar, etc.)

The system class path includes anything the JVM environment was
instructed to load when launched (either from the CLASSPATH
environment variable, or from the "-classpath" argument on the
command-line).

Tomcat's bootstrap class creates a new ClassLoader which includes
everything in lib/*.jar and then starts the Tomcat container using
*that* as the effective "class path".

> What is it after it starts up?

Well, Tomcat can see everything in the boot and system class paths
(ClassLoaders) plus everything in lib/*.jar.

> Correct me if I am wrong that means that whatever dependencies I 
> have in my project is not added to the SYSTEM CLASSPATH but is 
> instead loaded by TOMCAT.

Correct.

> So if a module searches only the SYSTEM CLASSPATH it won’t be able
> to find the dependencies. Am I right ?

Correct. And DriverManager can't search *down* into ClassLoaders it
doesn't know about -- including Tomcat's ClassLoader. So,
DriverManager can't load your JDBC driver using its standard detection
algorithm.

So, you still need to call Class.forName to register the driver.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU5PrwAAoJEBzwKT+lPKRY0/kP/Raf7VLP18ZpzXlLqK+ehPW5
1tDiJAG/uiz9L+v3/cOgmqcabHI7m4BSGdd8eeW2G8CYNpjRYDRTjq8dGVPkkzO6
4ZR3CX2Hgawxw7Qg8hnDh+Fjq+o7yOclY9T0rQFzcP4UnS4cRnRDRHGZtADycP9Y
Fdgnq5ey/NloTYaUioAmdnL1wnj9pzhRkpzztI8fxhQ1q2AdkLojMUSOArlfg1X5
N/aSTA0ULhB8Socxiln6TSnjAMxoe2/1PCNwLcIzqLKJcN3zA2rt4eAf5F2JbC50
zJtfVNa5CEZa3W0Gj5Arl3YWp0rrBa/fNdv7j8iPoF7WZOJbACkZEYuQeS5U3SyS
kyPhKAkE0b5wICzT+H7VUkNCRi0Gb02ER+QFg/UnoYyX91HHJPKSXfPLmD1fraq0
YkrhmrXp2O7rRa7YiIDp0yL8d7tNppT7sObyzKzZ1TI7Bf5DXEFsx82aXXWQ7wO4
VOJh14CuaaBhmxWei3w+pUsoLxe359Fmj2cGbAjfCxGUqJhyZgltjMWCvagXM119
wg645c9nD71hpX1VpHiPQmougHB8a/w1ripd5hVwdT0NRnlYYG1oDKgT83dEYnkk
7yVnuGSA/upinWjdV+GjWCgLr1Kx8O0B4isX0yqBYXtQXh8m4/jDbFGx0V1mGFXW
aMQS15YHd+kU8oB65ioP
=sv9k
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Why is Class.forName required to load drivers in Tomcat 8 ?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Sreyan,

On 2/18/15 9:25 AM, sreyan32@gmail.com wrote:
> I am writing a simple Servlet that uses JDBC to talk to MySQL and 
> deploying it in Tomcat. Why do I need to add the line -:
> 
> Class.forName("com.mysql.jdbc.Driver");
> 
> to load the JDBC driver ? If I don’t do this I get an exception of 
> -:
> 
> java.sql.SQLException: No suitable driver was found.
> 
> Why is it when I use a JDBC connection in Servlets and deploy it
> in Tomcat must the class be loaded dynamically ? Why can’t Tomcat
> do that for me when I have already added it to the build path ?
> 
> From Java 6 onwards there is no need for the JDBC Driver to be
> loaded dynamically. So why when using JRE 8 and Tomcat 8 must this
> be done ?
> 
> Cant it be loaded automatically like for normal apps ? It sure
> would be a lot more cleaner code.

My guess is that DriverManager only searches the system CLASSPATH
which only contains the following JAR files when launching Tomcat
using the standard scripts:

bin/bootstrap.jar
bin/tomcat-juli.jar

The bootstrap class then configures a new ClassLoader (including
lib/*.jar) for the Tomcat server and Tomcat properly launches using
/that/.

I think your JDBC driver simply isn't visible to DriverManager's
auto-loading. But when you call Class.forName, the driver registers
itself with DriverManager, which is okay to do if DriverManager didn't
know about your driver.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU5KkJAAoJEBzwKT+lPKRYKxEP/RXZTiVXkiIGl2ALwgAgNL/C
BjekoBb7b9xoKqm0WBK4mInZ9WW1pEnAkvSDsXyRZ3nrqGBXFtIatehBYo4fFizL
W+e7vM8v3C+lruqqSSEPH2Mn2Dx0AO/zT/bu+di+N0Q1ZYWvgWukw1DVoKMacZAN
23dmkR62IbfwprnZ83hWhJimIe926EpooMJ+ZgaccKxTdIPHo6RNJBnsFdiTr2o4
ZqtF/8m4ShDipAyF8fmhsa4w3rEUYsgV1CN+tjDtSEpJ9irTMCEZY3tHlElUhNgR
rg9XlkHGH+9WufJeGujZEndG3x9ey0tZM8jxm5kmty2G9w3hq12EavncH9/JyjCk
fdUX/xMLgwZQWDMPvgqgZeH8+jgYhdbDY+RTPTTCLulu+Qy6uz+/ijjPGX/dUtez
NSIXFgMu7j0tzLU3Z2FIdTvZlI9SpotndQ34ddT4HBqo35ooJw7fwDCeh/SW3UMm
IaUxdzroRRksY5TU2ZqifcF3m2cKUxXX5O454nSa0z2DdWCBe7JM2cEKPP1NNn4L
uiEBxt/7VWCl4ZIrZcqjXn6IHG2lYyeTpYL+x+KUUpgEwOZnSlbOyWTje2FJ6UNb
K7RIR3ITGyMDXnpAgFnt45jStCSkqHilI+ru3WplD3aEJf3xN+c9i9t6EAZrq04C
eK2ta2Y6JGU3JuKoazol
=06fK
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org