RE: Help getting MySQL connected to Tomcat
Yes, David, you are correct. After some reading, I realized that configuration was recommended by my application's vendor and is indeed erroneous. Thank you for the clarification explanation. ~LZM~ -Original Message- From: David Smith [mailto:[EMAIL PROTECTED] Sent: Sunday, February 24, 2008 5:10 AM To: Tomcat Users List Subject: Re: Help getting MySQL connected to Tomcat java.net.ConnectException: Connection refused: connect Some distributions of mysql configure it by default to use named pipes w/o a port listening at 3306. It's mainly for security reasons, but can be a real PITA when your application requires a TCP/IP port. Check the parameters MySQL was started with against the list available at http://dev.mysql.com/doc/refman/5.0/en/server-options.html The netstat command should also be of help in showing whether mysql is listening on a TCP/IP port or not. Individual systems vary, but to help you get started this works on my Mandriva system: netstat -tl. Once you have that fixed and restart mysql, tomcat should be able to connect. BTW don't use the autoReconnect=true url option as suggested by another poster. It only tells the driver to attempt to reestablish the connection if it fails for some reason, but not until you've successfully gotten a connection from the pool, tried it and then have received an error. Your better off configuring your Resource / element w/ a validatonQuery attribute like validationQuery=select 1. With that the pool will test the connection just before your code receives a connection, insuring it's always good. --David [EMAIL PROTECTED] wrote: Hello, I'm trying to get Tomcat and MySQL to connect. I have the following installations: Tomcat V5.5.26 MySQL V5.0 mysql connector 5.1.5 from the mysql site (copied the jar to tomcat\common\lib I've tried following the directions from the tomcat dist and from the mysql dist but I always get can't connect exceptions when I start tomcat. In mysql I've created a user=javauser with password=javadude and I created a database called javatest with one table and some data in the table. I can connect to the database from the console using C: mysql -u javauser -p and I can then connect to the javatest DB and select * from the table works fine. Then I added the following to in the tomcat/conf/context.xml file Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context And I stop tomcat, and when I restart tomcat I get the following in the catalina log: ... INFO: Stopping Coyote HTTP/1.1 on http-8080 Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Failed shutdown of Apache Portable Runtime Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer SEVERE: Catalina.stop: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:520) at java.net.Socket.connect(Socket.java:470) at java.net.Socket.init(Socket.java:367) at java.net.Socket.init(Socket.java:180) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) So why the connection refused? Any help is appreciated. Thanks, Kevin - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.21.1/1297 - Release Date: 2/25/2008 9:22 AM No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.21.1/1297 - Release Date: 2/25/2008 9:22 AM
Re: Help getting MySQL connected to Tomcat
java.net.ConnectException: Connection refused: connect Some distributions of mysql configure it by default to use named pipes w/o a port listening at 3306. It's mainly for security reasons, but can be a real PITA when your application requires a TCP/IP port. Check the parameters MySQL was started with against the list available at http://dev.mysql.com/doc/refman/5.0/en/server-options.html The netstat command should also be of help in showing whether mysql is listening on a TCP/IP port or not. Individual systems vary, but to help you get started this works on my Mandriva system: netstat -tl. Once you have that fixed and restart mysql, tomcat should be able to connect. BTW don't use the autoReconnect=true url option as suggested by another poster. It only tells the driver to attempt to reestablish the connection if it fails for some reason, but not until you've successfully gotten a connection from the pool, tried it and then have received an error. Your better off configuring your Resource / element w/ a validatonQuery attribute like validationQuery=select 1. With that the pool will test the connection just before your code receives a connection, insuring it's always good. --David [EMAIL PROTECTED] wrote: Hello, I'm trying to get Tomcat and MySQL to connect. I have the following installations: Tomcat V5.5.26 MySQL V5.0 mysql connector 5.1.5 from the mysql site (copied the jar to tomcat\common\lib I've tried following the directions from the tomcat dist and from the mysql dist but I always get can't connect exceptions when I start tomcat. In mysql I've created a user=javauser with password=javadude and I created a database called javatest with one table and some data in the table. I can connect to the database from the console using C: mysql -u javauser -p and I can then connect to the javatest DB and select * from the table works fine. Then I added the following to in the tomcat/conf/context.xml file Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context And I stop tomcat, and when I restart tomcat I get the following in the catalina log: ... INFO: Stopping Coyote HTTP/1.1 on http-8080 Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Failed shutdown of Apache Portable Runtime Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer SEVERE: Catalina.stop: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:520) at java.net.Socket.connect(Socket.java:470) at java.net.Socket.init(Socket.java:367) at java.net.Socket.init(Socket.java:180) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) So why the connection refused? Any help is appreciated. Thanks, Kevin - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help getting MySQL connected to Tomcat
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Lessie, Lessie Z. Mitch wrote: | url=jdbc:mysql://localhost:3306/javatest?autoReconnect=true Don't use autoReconnect=true... that parameter has been deprecated for years, and the effect is better achieved by using testOnBorrow=true along with validationQuery=SELECT 1. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkfBk0gACgkQ9CaO5/Lv0PBuvwCgt7C9G9ZMKxN+rFk+RLxAstZu f8gAnRjBG313IvJjQhpnC7LG3+1ibenB =VFAp -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]
Re: Help getting MySQL connected to Tomcat
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Kevin, [EMAIL PROTECTED] wrote: | I've tried following the directions from the tomcat dist and from the | mysql dist but I always get can't connect exceptions when I start tomcat. Connection refused usually means that everything is configured properly on the Java/Tomcat side (except maybe the port number, though yours is correct) and MySQL isn't picking up the phone for some reason. David's reply regarding the networking options for MySQL is a good place to start. Check your my.ini (or mysql.ini, or whatever it happens to be) and make sure that the skip-networking option is not currently enabled. I'm not sure how MySQL fares on win32 without networking, but presumably the mysql client can connect via a named pipe rather than using TCP/IP. To see if MySQL is using networking (which is required by MySQL Connector/J), try this: C:\whatever mysql -h 127.0.0.1 -u javauser -p This will force the command-line tool to use TCP/IP instead of other means to connect to the server (note that localhost won't work, since the tool is documented to use the named pipe if the string localhost is used. Using the IP address will avoid this behavior). If the connection fails, then you have disabled networking in MySQL and need to enable it. | I can connect to the database from the console using C: mysql -u | javauser -p and I can then connect to the javatest DB and select * from | the table works fine. | | Then I added the following to in the tomcat/conf/context.xml file This is not your problem, but you really shouldn't put your Resource element in tomcat/conf/context.xml; instead, you should create an application-specific context.xml and put it in there. What you are doing will expose that DataSource to all applications deployed in your container. | Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true You should not define docBase in context.xml, ever. Also, I'm not entirely sure, but I don't think you can set a path in the default context.xml... it will probably be ignored. Again, these issues aren't your (current) problem, but you should fix them, anyway. | |Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource | |maxActive=100 maxIdle=30 maxWait=1 | |username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver | |url=jdbc:mysql://localhost:3306/javatest/ | | /Context | | | | And I stop tomcat, and when I restart tomcat I get the following in the catalina log: | | | | ... | | INFO: Stopping Coyote HTTP/1.1 on http-8080 | | Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent | | INFO: Failed shutdown of Apache Portable Runtime | | Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer | | SEVERE: Catalina.stop: | | java.net.ConnectException: Connection refused: connect | | at java.net.PlainSocketImpl.socketConnect(Native Method) | | at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) | | at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) | | at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) | | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) | | at java.net.Socket.connect(Socket.java:520) | | at java.net.Socket.connect(Socket.java:470) | | at java.net.Socket.init(Socket.java:367) | | at java.net.Socket.init(Socket.java:180) | | at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) | | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | | at java.lang.reflect.Method.invoke(Method.java:585) | | at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) | | at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) | | | | | | So why the connection refused? | | | | Any help is appreciated. | | | | Thanks, | | Kevin | | | | | -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkfBlhoACgkQ9CaO5/Lv0PD/GQCcCv1o2CwggigU3W5gu2VYRXGn HfYAoJS5O+gITnVRYbNML7J+jP7CG+pA =Snl6 -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]
Re: Help getting MySQL connected to Tomcat
Hi Kevin- is there a MySQL listener installed on port 3306..you can verify with netstat -a | grep 3306? If so can you connect using mySQL client? M- - Original Message - From: [EMAIL PROTECTED] To: users@tomcat.apache.org Sent: Saturday, February 23, 2008 9:00 PM Subject: Help getting MySQL connected to Tomcat Hello, I'm trying to get Tomcat and MySQL to connect. I have the following installations: Tomcat V5.5.26 MySQL V5.0 mysql connector 5.1.5 from the mysql site (copied the jar to tomcat\common\lib I've tried following the directions from the tomcat dist and from the mysql dist but I always get can't connect exceptions when I start tomcat. In mysql I've created a user=javauser with password=javadude and I created a database called javatest with one table and some data in the table. I can connect to the database from the console using C: mysql -u javauser -p and I can then connect to the javatest DB and select * from the table works fine. Then I added the following to in the tomcat/conf/context.xml file Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context And I stop tomcat, and when I restart tomcat I get the following in the catalina log: .. INFO: Stopping Coyote HTTP/1.1 on http-8080 Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Failed shutdown of Apache Portable Runtime Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer SEVERE: Catalina.stop: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:520) at java.net.Socket.connect(Socket.java:470) at java.net.Socket.init(Socket.java:367) at java.net.Socket.init(Socket.java:180) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) So why the connection refused? Any help is appreciated. Thanks, Kevin - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help getting MySQL connected to Tomcat
help. Kevin - Original Message - From: Christopher Schultz [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Sunday, February 24, 2008 8:06 AM Subject: Re: Help getting MySQL connected to Tomcat -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Kevin, [EMAIL PROTECTED] wrote: | I've tried following the directions from the tomcat dist and from the | mysql dist but I always get can't connect exceptions when I start tomcat. Connection refused usually means that everything is configured properly on the Java/Tomcat side (except maybe the port number, though yours is correct) and MySQL isn't picking up the phone for some reason. David's reply regarding the networking options for MySQL is a good place to start. Check your my.ini (or mysql.ini, or whatever it happens to be) and make sure that the skip-networking option is not currently enabled. I'm not sure how MySQL fares on win32 without networking, but presumably the mysql client can connect via a named pipe rather than using TCP/IP. To see if MySQL is using networking (which is required by MySQL Connector/J), try this: C:\whatever mysql -h 127.0.0.1 -u javauser -p This will force the command-line tool to use TCP/IP instead of other means to connect to the server (note that localhost won't work, since the tool is documented to use the named pipe if the string localhost is used. Using the IP address will avoid this behavior). If the connection fails, then you have disabled networking in MySQL and need to enable it. | I can connect to the database from the console using C: mysql -u | javauser -p and I can then connect to the javatest DB and select * from | the table works fine. | | Then I added the following to in the tomcat/conf/context.xml file This is not your problem, but you really shouldn't put your Resource element in tomcat/conf/context.xml; instead, you should create an application-specific context.xml and put it in there. What you are doing will expose that DataSource to all applications deployed in your container. | Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true You should not define docBase in context.xml, ever. Also, I'm not entirely sure, but I don't think you can set a path in the default context.xml... it will probably be ignored. Again, these issues aren't your (current) problem, but you should fix them, anyway. | |Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource | |maxActive=100 maxIdle=30 maxWait=1 | |username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver | |url=jdbc:mysql://localhost:3306/javatest/ | | /Context | | | | And I stop tomcat, and when I restart tomcat I get the following in the catalina log: | | | | ... | | INFO: Stopping Coyote HTTP/1.1 on http-8080 | | Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent | | INFO: Failed shutdown of Apache Portable Runtime | | Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer | | SEVERE: Catalina.stop: | | java.net.ConnectException: Connection refused: connect | | at java.net.PlainSocketImpl.socketConnect(Native Method) | | at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) | | at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) | | at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) | | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) | | at java.net.Socket.connect(Socket.java:520) | | at java.net.Socket.connect(Socket.java:470) | | at java.net.Socket.init(Socket.java:367) | | at java.net.Socket.init(Socket.java:180) | | at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) | | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | | at java.lang.reflect.Method.invoke(Method.java:585) | | at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) | | at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) | | | | | | So why the connection refused? | | | | Any help is appreciated. | | | | Thanks, | | Kevin | | | | | -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkfBlhoACgkQ9CaO5/Lv0PD/GQCcCv1o2CwggigU3W5gu2VYRXGn HfYAoJS5O+gITnVRYbNML7J+jP7CG+pA =Snl6 -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] ?xml version=1.0 encoding=ISO-8859-1? web-app xmlns=http://java.sun.com/xml/ns/j2ee; xmlns:xsi=http://www.w3.org
Re: Help getting MySQL connected to Tomcat
[EMAIL PROTECTED] wrote: Thanks for the help. I still don't have this working completely, but tomcat is now working. 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 Context init = new InitialContext(); Context ctx = (Context) init.lookup(java:comp/env); DataSource ds = (DataSource) ctx.lookup(jdbc/CurrencyDB); In the previous posts, you have used jdbc/TestDB in your resource defintion. I presume you have changed id? Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help getting MySQL connected to Tomcat
Yes, In the context.xml I have ResourceParams name=jdbc/CurrencyDB Also in web.xml I have resource-ref res-ref-namejdbc/CurrencyDB/res-ref-name res-typejavax.sql.DataSource/res-type res-authContainer/res-auth /resource-ref Thanks, Kevin - Original Message - From: Alan Chaney [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Sunday, February 24, 2008 9:11 AM Subject: Re: Help getting MySQL connected to Tomcat [EMAIL PROTECTED] wrote: Thanks for the help. I still don't have this working completely, but tomcat is now working. 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 Context init = new InitialContext(); Context ctx = (Context) init.lookup(java:comp/env); DataSource ds = (DataSource) ctx.lookup(jdbc/CurrencyDB); In the previous posts, you have used jdbc/TestDB in your resource defintion. I presume you have changed id? Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context - 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]
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=1 ~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
Re: Help getting MySQL connected to Tomcat
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=1 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
Re: Help getting MySQL connected to Tomcat
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Kevin, [EMAIL PROTECTED] wrote: | Now for the issue at hand. I turned off my Windows firewall and this did | not fix the issue. Yeah, that was unlikely to change anything. Make sure you turn that back on! | I implemented the resource as you suggested (short hand method as follows): [snip] |url=jdbc:mysql://localhost:3306/test |driverClassName=com.mysql.jdbc.Driver These look fine. | java.sql.SQLException: No suitable driver [snip] | For some reason the driver manager can't get the driver even though the | jar is in tomcat\common\lib ??? The last thing I can think of is that you might have the JDBC library in more than one place. Check to make sure that you /only/ have it in tomcat/common/lib -- especially check your application's WEB-INF/lib directory. If there are multiple copies of the library, things don't work our properly (I'm unclear as to exactly why, just that things generally fall apart if you have multiple copies). Hope that helps, - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkfCApQACgkQ9CaO5/Lv0PARzgCgrL34JaXskQYe6yHFozL/U525 6M0An3U3kOZSgQYStr2az1sEFB0qo8Nz =YIFV -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]
Re: Help getting MySQL connected to Tomcat
On Sun, Feb 24, 2008 at 2:59 PM, [EMAIL PROTECTED] wrote: I implemented the resource as you suggested (short hand method as follows): WEB-INF\context.xml for the app now has Is that a typo or really what you did? Your webapp's context.xml goes in META-INF/ , not WEB-INF/ ... -- Hassan Schroeder [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]
Re: Help getting MySQL connected to Tomcat
Ok, so here is what I am. I have moved the connection to the doGet() method. And if I do the following (no pooling) it works fine, I get the connection and I can query the DB. Class.forName(com.mysql.jdbc.Driver); con = DriverManager.getConnection(jdbc:mysql://localhost:3306/test, javauser, javadude); But if I try to to pooling (replace the above with) I can't get the connection try { Context init = new InitialContext(); Context ctx = (Context) init.lookup(java:comp/env); dataSource = (DataSource) ctx.lookup(jdbc/CurrencyDB); } catch (NamingException ex) { throw new ServletException(cannot retrieve java:comp/env/jdbc/CurrencyDB, ex); } The dataSource lookup throws a NamingException. My context.xml file has the following: 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=1 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 My impression is that the Resource can have any name as long as you use this to look it up. So in my example the Resource is names jdbc/CurrencyDB and in the java code I'm calling ctx.lookup(CurrencyDB); 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=1 ~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
Re: Help getting MySQL connected to Tomcat
Are you sure? I thought the webapp context.xml went in WEB-INF. At any rate I tried moving it to META-INF and it did not help. Thanks, Kevin - Original Message - From: Hassan Schroeder [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org; [EMAIL PROTECTED] Sent: Sunday, February 24, 2008 4:14 PM Subject: Re: Help getting MySQL connected to Tomcat On Sun, Feb 24, 2008 at 2:59 PM, [EMAIL PROTECTED] wrote: I implemented the resource as you suggested (short hand method as follows): WEB-INF\context.xml for the app now has Is that a typo or really what you did? Your webapp's context.xml goes in META-INF/ , not WEB-INF/ ... -- Hassan Schroeder [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] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help getting MySQL connected to Tomcat
Well, I'm happy to say that you are correct. It's very strange. I tried moving the file to META-INF, restarting tomcat and it did not work. Next I tried deleting the webapp directory and war file. Moving the context.xml file in my source, rebuild war and redeploy. Not it works! Lesson learned, always rebuild war and redeploy. Thanks for all the help! Kevin - Original Message - From: [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Sunday, February 24, 2008 4:35 PM Subject: Re: Help getting MySQL connected to Tomcat Are you sure? I thought the webapp context.xml went in WEB-INF. At any rate I tried moving it to META-INF and it did not help. Thanks, Kevin - Original Message - From: Hassan Schroeder [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org; [EMAIL PROTECTED] Sent: Sunday, February 24, 2008 4:14 PM Subject: Re: Help getting MySQL connected to Tomcat On Sun, Feb 24, 2008 at 2:59 PM, [EMAIL PROTECTED] wrote: I implemented the resource as you suggested (short hand method as follows): WEB-INF\context.xml for the app now has Is that a typo or really what you did? Your webapp's context.xml goes in META-INF/ , not WEB-INF/ ... -- Hassan Schroeder [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] - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Help getting MySQL connected to Tomcat
Try: url=jdbc:mysql://localhost:3306/javatest?autoReconnect=true ~LZM~ -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Saturday, February 23, 2008 4:01 PM To: users@tomcat.apache.org Subject: Help getting MySQL connected to Tomcat Hello, I'm trying to get Tomcat and MySQL to connect. I have the following installations: Tomcat V5.5.26 MySQL V5.0 mysql connector 5.1.5 from the mysql site (copied the jar to tomcat\common\lib I've tried following the directions from the tomcat dist and from the mysql dist but I always get can't connect exceptions when I start tomcat. In mysql I've created a user=javauser with password=javadude and I created a database called javatest with one table and some data in the table. I can connect to the database from the console using C: mysql -u javauser -p and I can then connect to the javatest DB and select * from the table works fine. Then I added the following to in the tomcat/conf/context.xml file Context path=/db-servlet docBase=db-servlet debug=5 reloadable=true crossContext=true Resource name=jdbc/TestDB auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=javauser password=javadude driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/javatest/ /Context And I stop tomcat, and when I restart tomcat I get the following in the catalina log: ... INFO: Stopping Coyote HTTP/1.1 on http-8080 Feb 23, 2008 8:31:19 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Failed shutdown of Apache Portable Runtime Feb 23, 2008 8:37:40 AM org.apache.catalina.startup.Catalina stopServer SEVERE: Catalina.stop: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:520) at java.net.Socket.connect(Socket.java:470) at java.net.Socket.init(Socket.java:367) at java.net.Socket.init(Socket.java:180) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:395) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:344) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:435) So why the connection refused? Any help is appreciated. Thanks, Kevin No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.20.9/1294 - Release Date: 2/22/2008 6:39 PM No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.20.9/1294 - Release Date: 2/22/2008 6:39 PM - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]