facebook

Problems wiring embedded Tomcat server to MySql

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #295149 Reply

    Mary
    Member

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

    Using MySql 5.0
    MyEclipse 6.5 with it’s embedded Tomcat server 6
    Java 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 another one; 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 files.

    ==================================================================
    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>

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

    Here is my 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.)

    ProcessLoginServlet.doPost: User not found in the database!!!

    ( Note: I do realize this error message is incorrect; I never get the connection so, of course, the user would not be found in the database!)

    #295206 Reply

    Loyal Water
    Member

    If you set up a Tomcat connector in MyEclipse and try wiring that to MySQL, does it work without issues?

    The Embedded Tomcat has been provided to test your applications on one click and it has a lot of Tomcat functionality removed from it to make it as compact as possible. That could be the reason you are facing this issue. So I would suggest you setup a new Tomcat with MyEclipse and work with it.

    #295218 Reply

    Mary
    Member

    I have provided my web.xml, context.xml, code trying to create a connector and the resulting stack trace. Is there anyone who would have the time to investigate this code, which is not huge, and see if I am indeed missing something? I saw one place where a DataSource was created and given a variable name; but most places , in fact all the other ones, do not do that. Is that a necessary step? I have looked at so many examples and tutorials.

    Is there something anywhere describing what functionality is removed from the embedded server? Is there a document to read concerning that?:

    Thanks.

    #295233 Reply

    rmcvay
    Member

    What about outside of ME? If you deploy that app to a Tomcat 6 installed on the same PC does it work?

    #295234 Reply

    Mary
    Member

    I could change my approach; but I should tell you, with my limited experience, I would well introduce another set of problems unknowingly. Does that make sense?

    I would like to know if anyone has accessed a DataSource through the embedded Tomcat server; if someone has done it, then I can spend more time and figure it out. Perhaps even they could tell me how they did it. If it is not possible, then I can try deploying this app to an external server.

    I have spent a lot of time with other issues regarding the embedded server so before I toss all that effort aside, I’d like to know if this final step is possible or not. Perhaps someone on the MyEclipse development or testing teams could tell me if this has been tried and if it is possible.

    Additionally, perhaps someone who has worked with a DataSource could look at the code I have provided and let me know if I have missed a configuration step or a coding step.

    It might take me several days to deploy this app in an external server and get things working due to my lack of experience so before I start down a new path, to me, I”d like answers to some of these questions.

    Thank you so very much.

    #295267 Reply

    Brian Fernandes
    Moderator

    kentucky,

    Before we go further, have you managed to connect to this database directly using those same credentials? I see a connection timeout in our stack trace which seems to indicate that you do have everything setup correctly, but there may be a problem with your database or the credentials you are attempting to use.

    If you use the same username, password, driver and URL in our DB explorer to setup a connection to this same database, does it work? If it does not, then your problem is elsewhere.

    Setting up a datasource with the embedded tomcat server is certainly possible and has been done, but I do not have the details at hand.

    #295268 Reply

    Loyal Water
    Member

    kentucky_b,
    I have asked the dev team to look into this and get back to you with a reply but I’m afraid I cant help you much with this since I haven’t tried this myself.

    #295271 Reply

    Mary
    Member

    I have connecvted to the database with the same credentials through the DB explorer. I hope it is possible to find the details for setting up a DataSource with the embedded tomcat server. I’ve been stuck at this point for almost two weeks now, trying to figure it out. I would appreciate anyone who could look at my code snippets which I think contain all the relevant information. I would be happy to supply any other information requested also. Thank you so much.

    #295600 Reply

    rmcvay
    Member

    It really only takes a few minutes to install a Tomcat, tell ME about it and deploy your app to it. The reason I recommend doing that is to eliminate the possibility that your problem arises from the things stripped out of the ME Tomcat.

    #295605 Reply

    Mary
    Member

    It only takes a few minutes if you are comfortable doing it and have experience; some of us are not there yet!

    It may have been that I had a jre instead of a jdk; I was waiting for someone to ask me things like that, but I found that myself. I changed some other things, too. The answer many times is to reinstall or redeploy when you relaly need to learn the actual why of why somethign does not work. I would have thought creating a link to a DataSource had been verified one way or the other for the embedded Tomcat. When someone told me it would work, I really began to look at everything I could to find why mine was not working. Of course I had hoped someone would ask me the telling question before it took me weeks to figure it out; but I suppose this is one of those opportunities for growth that some of us could do without! 🙂

    #295619 Reply

    Loyal Water
    Member

    I’m glad to hear you got the issue resolved. Thanks for letting us know.

    #295642 Reply

    Mary
    Member

    Thanks to you and the staff there for being there for we newbies; I really appreciated having someone to support my efforts.

    #295698 Reply

    Loyal Water
    Member

    Your welcome.

Viewing 13 posts - 1 through 13 (of 13 total)
Reply To: Problems wiring embedded Tomcat server to MySql

This topic is marked as closed to new replies, however your posting capabilities still allow you to do so.

You must be logged in to post in the forum log in