Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When one says "everything is up and correct", then most common mistakes are the URL and the firewall.
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could there be a firewall problem with Vista ?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mary Taylor wrote:Where is the url you are referring to designated?

I was talking about the JDBC URL by the way.
 
please buy this thing and then I get a fat cut of the action:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic