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.)