wood burning stoves 2.0*
The moose likes JDBC and the fly likes Missing something creating a DataSource, MySql 5.0, MyEclipse 6.5, Tomcat 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Missing something creating a DataSource, MySql 5.0, MyEclipse 6.5, Tomcat 6" Watch "Missing something creating a DataSource, MySql 5.0, MyEclipse 6.5, Tomcat 6" New topic
Author

Missing something creating a DataSource, MySql 5.0, MyEclipse 6.5, Tomcat 6

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Using MySql 5.0
MyEclipse 6.5 with it's embedded Tomcat server 6
Java 5


Goal: Access MySql 5 through the embedded Tomcat 6 in MyEclipse 6.5.

I'm trying to access a MySql 5 database and having problems creating the connection. I want to use a username and password to login; however, the BasicDataSource does not have a constructor with any parameters. I don't know where the BasicDataSource is being chosen over a DataSource; I am not writing that code. I notice Tomcat is trying to create a BasicDataSource connection even though I am specifying a DataSource connection in the web.xml and the context.xml. I am a rookie creating a DataSource so that may show in what I have done. I have also not used a context.xml previously. The examples on the web do not all contain the same things so I'm not sure if I need to do more to create this DataSource; most of the examples do not, but one example had something I don't have yet.


I don't have log4j or other logging configured yet so there are a few "println" statements; sorry about that.


I don't know why the connection is timing out.

=====================================================================================================

Here is the entry in the web.xml file:

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

==============================================================================================
Here is the context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
username="tuser"
password="tpwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/TestDB?autoReconnect=true"
validationQuery="select count(*) from registered_users"
maxActive="10"
maxIdle="4"/>
</Context>

=====================================================================================================

Java code to obtain a database connection:

public static Connection getDBConnection()
{
method = "getDBConnection";
System.out.print("\n\n" + className + "." + method);
try
{
Context initCtx = new InitialContext();
System.out.print("\n\n initCtx = " + initCtx.toString());
Context envCtx = (Context) initCtx.lookup("java:comp/env");
System.out.print("\n\n envCtx = " + envCtx.toString());
DataSource ds = (DataSource)envCtx.lookup("jdbc/TestDB");

System.out.print("\n\n ds = " + ds.toString());
conn = ds.getConnection();
System.out.print("\n\n conn = " + conn.toString());
return conn;
}
catch (Exception e)
{
System.out.print("\n\n" + className + "." + method + " catch block: \n\n" + e.toString());
conn = null;
}
return conn;
}


=====================================================================================================

Finally, here is the stack trace from the exception thrown when trying to obtain a db connection:

UtilitiesServlet.getDBConnection catch block:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Server connection failure during transaction. Due to underlying exception: 'java.net.ConnectException: Connection timed out: connect'.

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection timed out: connect

STACKTRACE:

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2921)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at MyProject.Utilities.getDBConnection(Utilities.java:53)
at MyProject.Utilities.findUserInDatabase(Utilities.java:139)
at MyProject.ProcessLoginServlet.doPost(ProcessLoginServlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)


** END NESTED EXCEPTION **


Attempted reconnect 3 times. Giving up.)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
There is a hardware or software configuration failure. For example: DB server is down, DB server doesn't accept TCP/IP connections, JDBC driver is buggy, hostname/port is wrong, a firewall is blocking connections, etcetera.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Are you or anyone experienced enough to provide some specific items to check? The database is up; I can execute sql against it on the command line. Maybe I have the driver in the wrong place? But it is attempting a connection; it is not saying the driver is missing so that's probably not the problem.

Does the container create the DataSource object? Do I need some code somewhere to accomplish that?

If you can ask me some specific questions, I can answer them; perhaps we can solve this problem.

Thanks.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
When one says "everything is up and correct", then most common mistakes are the URL and the firewall.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Oh, I didn't mean to say everything was correct! I wouldn't need assistance if it were all correct.

I am running Norton so maybe there is a firewall issue there although I am not sure how to trouble shoot that. I will see what I can figure out. I am running Norton 360. My neighbor may have locked something down here that I don't know about. I don't know where to look for the firewall in Norton 360; I'll google and see what I can find.

So you did not see anything wrong or missing in my context.xml, web.xml, or code to obtain the connection? I hadn't thought about a firewall issue so thanks for alerting me to that. I just assumed my code was incorrect.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Where is the url you are referring to designated? The MyEclipse Database Explorer works fine. I don't see where I need a url in the web.xml or context.xml so where should I check? wiring components together is not my most experienced area as you can possibly tell...
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Could there be a firewall problem with Vista ?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Maybe. Who knows. I can't take a look in your PC.

But have you ever Googled the exception?

http://www.google.com/search?q=mysql+%22java.net.ConnectException%3A+Connection+timed+out%3A+connect%22
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Mary Taylor wrote:Where is the url you are referring to designated?
I was talking about the JDBC URL by the way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Missing something creating a DataSource, MySql 5.0, MyEclipse 6.5, Tomcat 6
 
Similar Threads
Eclipse+Tomcat+JNDI
Cannot create JDBC driver
Need help with JNDI connection to tomcat
resouce config problem
Configuring a MySQL DataSource in Tomcat5