Chris,
Thanks for the help. On your code analysis, points well taken. I'll
implement you suggestions.
Now for the issue at hand. I turned off my Windows firewall and this did not
fix the issue.
I implemented the resource as you suggested (short hand method as follows):
WEB-INF\context.xml for the app now has
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" debug="5" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_CurrencyDB_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/CurrencyDB" auth="Container"
type="javax.sql.DataSource"
maxActive="1"
maxIdle="1"
maxWait="10000"
url="jdbc:mysql://localhost:3306/test"
username="javauser"
password="javadude"
driverClassName="com.mysql.jdbc.Driver"
removeAbandoned="true"
removeAbandonedTimeout="30"
logAbandoned="true"
testOnBorrow="true"
validationQuery="SELECT 1"
/>
</Context>
But this also did not help. Same exception:
SEVERE: Servlet.service() for servlet jsp threw exception
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at converters.ConversionDAO.<init>(ConversionDAO.java:23)
at org.apache.jsp.rates_jsp._jspService(rates_jsp.java:46)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
For some reason the driver manager can't get the driver even though the jar
is in <tomcat>\common\lib ???
Thanks,
Kevin
----- Original Message -----
From: "Christopher Schultz" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Sunday, February 24, 2008 1:30 PM
Subject: Re: Help getting MySQL connected to Tomcat
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Kevin,
[EMAIL PROTECTED] wrote:
| I tested mysql and it is using tcp/ip. "mysql -h 127.0.0.1 -u javauser
| -p" works fine.
| Also the MSQLAdmin program shows it is connecting to the correct port
3306
Okay, good. Any software firewalls enabled? The default firewall for
recent Windows versions does not prohibit any localhost traffic that I
know of.
| I see your point about purring the resource in conf/context.xml, I will
| move it.
| But from reading online forums, I have come across two suggestions:
| 1) put a context.xml in the WEB-INF dir of you webapp
| 2) put the <Context> in <tomcat>\conf\localhost\<webappname>.xml
|
| I would guess that 1) is correct, so that is what I did. Attached is my
| current context.xml from this webapp
Both are acceptable, but #1 is more straightforward and, IMO, easier to
maintain.
| So now Tomcat starts with no errors, but when I try to run my jsp which
| uses a DAO in java I get errors:
|
| org.apache.jasper.JasperException: Cannot create JDBC driver of class ''
| for connect URL 'null'
| [snip]
| So why can't it find a driver[?]
| I put the mysql-connector-java-5.1.5-bin.jar in <tomcat>\common\lib
For best result, you should put it in <tomcat>/common/lib, though
various other strategies are available if you have other requirements.
I'm not sure why there are all those 'null' strings in there... I think
either Tomcat or DBCP has a (small) bug that gives you all those nulls
when the JDBC library isn't available for whatever reason -- at least,
that's what seems to happen very frequently to posters on this list.
I think the problem might be the separation of <Resource> into
<Resource> and <ResourceParams>. I used to use these separately back in
TC 4.1.x, but it appears that there's a shorthand that may work better
for you. Here's what I have in my context.xml for our application.
Fill-in the appropriate values for your configuration. It's much easier
to read than all those nested elements, too.
~ <Resource name="@DATASOURCE_REF@"
~ auth="Container"
~ type="javax.sql.DataSource"
~ maxActive="1"
~ maxIdle="1"
~ maxWait="10000"
~ url="@DATABASE_URL@"
~ username="@DATABASE_USERNAME@"
~ password="@DATABASE_PASSWORD@"
~ driverClassName="@DATABASE_DRIVER@"
~ removeAbandoned="true"
~ removeAbandonedTimeout="30"
~ logAbandoned="true"
~ testOnBorrow="true"
~ validationQuery="SELECT 1"
~ />
Just a quick comment on programming style, etc.:
| public ConversionDAO() throws SQLException, NamingException
| {
| Context init = new InitialContext();
| Context ctx = (Context) init.lookup("java:comp/env");
| DataSource ds = (DataSource) ctx.lookup("jdbc/CurrencyDB");
|
| con = ds.getConnection(); // THIS IS LINE 23
| select = con.prepareStatement("SELECT rate FROM exchange
| WHERE src = ? and dst = ?");
| update = con.prepareStatement("UPDATE Exchange SET rate = ?
| WHERE src = ? NAD dst = ?");
| }
I highly recommend that you do /not/ do what you are doing, here.
Specifically, don't grab a JDBC Connection at object creation time and
hold it forever. This completely defeats the purpose of the connection
pool: to use connections only when necessary.
I see what you're trying to do: improve performance by preparing SQL
statements in advance and re-using them. The truth is:
1) Prepared statements are faster than non-prepared statements, but in
truth, you're not saving all that much time. Any performance benefit you
get from the above code is completely gone by hoarding a connection in
your DAO.
2) Both Connector/J and MySQL server can both cache prepared statements
for you. In that case, calling Connection.prepareStatement(some
statement) can be nice and fast even if you are technically repeating
the calls over and over again. Also, recent versions of Connector/J have
disabled server-side prepared statements because they simply do not work
properly, so a driver-based prepared statement cache is a nice feature.
Here's another reason not to do this: you may choose during runtime to
change something about the connection pool (like make all connections
read-only or something). If you don't grab a new connection each time,
the behavior will be unexpected.
Assuming you are writing lots of DAOs like this, I recommend moving the
code to grab a database connection into another utility object so all
the code is in the same place. Something like this:
public class DAOHelper
{
~ public static Connection getConnection()
~ throws SQLException, NamingException
~ {
~ Context init = new InitialContext();
~ Context ctx = (Context) init.lookup("java:comp/env");
~ DataSource ds = (DataSource) ctx.lookup("jdbc/CurrencyDB");
~ if(null == ds)
~ throw new NamingException("Could not find JNDI DataSource");
~ return ds.getConnection();
~ }
}
Then, all your DAOs can use this code to get a connection without having
to repeat that (albeit small) amount of code.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkfB4gkACgkQ9CaO5/Lv0PAyrQCdFKEMr+EgdHN6EQp8mGm2jJ7Z
7IkAn0OZIcynRy0KaYsXwW9bZy+3aDCu
=HWmr
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]