facebook

Problem Defining MySql datasource

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #223898 Reply

    ladams
    Member

    Using MyElcipse 3.0.1 Tomcat 5.0.15 and MySQL
    Able to do mysql queries so database is set up correctly.

    Trying to learn JSTL and need to be able to define a datasource. The following jsp gives the error
    javax.servlet.ServletException: Unable to get connection, DataSource invalid: “java.sql.SQLException: Communication link failure: java.io.IOException,
    underlying cause: Unexpected end of input stream

    booklist.jsp
    <%@ page language=”java”%>
    <%@ taglib uri=”http://jakarta.apache.org/struts/tags-html&#8221; prefix=”html” %>
    <%@ taglib uri=”/WEB-INF/c.tld” prefix=”c” %>
    <%@ taglib uri=”/WEB-INF/sql.tld” prefix=”sql” %>

    <sql:setDataSource var=”datasource”
    driver=”com.mysql.jdbc.Driver”
    url=”jdbc:mysql://localhost/publish”
    user=”publishuser” password=”secret” />

    <sql:query var=”books” dataSource=”${datasource}”>
    SELECT id, title, price, FROM book
    </sql:query>

    <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
    <html:html locale=”true”>
    <head>
    <html:base />
    <title>First JSP Database</title>
    </head>

    <body>
    <table border=”1″>
    <tr>
    <td>id</td><td>title</td><td>price</td>
    </tr>
    <c:forEach items=”${books.rows}” var=”row”>
    <tr>
    <td><c:out value=”${row.id}” /></td>
    <td><c:out value=”${row.title}” /></td>
    <td><c:out value=”${row.price}” /></td>
    </tr>
    </c:forEach>
    </table>
    </body>
    </html:html>

    I tried with and without the following datasource definition in struts-config.xml
    <data-sources>
    <data-source key=”publish”
    type=”org.apache.commons.dbcp.BasicDataSource”>
    <set-property property=”description” value=”Book Database”/>
    <set-property property=”driverClassName” value=”com.mysql.jdbc.Driver”/>
    <set-property property=”username” value=”publishuser”/>
    <set-property property=”password” value=”secret”/>
    <set-property property=”url”
    value=”jdbc:mysql://localhost/publish”/>
    </data-source>
    </data-sources>

    If I define a datasource in struts-config how do I define it in the JSP tried:
    <sql:setDataSource var=”publish”/>
    <sql:query var=”books” dataSource=”${datasource}”>
    SELECT id, title, price, FROM book
    </sql:query>
    and that didnt work.

    Do I need to define the datasource in TomCat? If so do I also need to define it in struts-config and the JSP page. Thanks for any help.

    #223931 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft dev, this isn’t related to ME.

    Do I need to define the datasource in TomCat?

    Yes, for all intents and purposes, ignore the fact that Struts supports a datasource. Your app server should provide your data source, besides your JSTL tags have no knowledge of where or how to get Struts-defined datasources.

    If so do I also need to define it in struts-config and the JSP page.

    Nope, just define it in Tomcat as a datasource, then it is availble to your webapp like magic, no need to do anything else. It’s very slick.

    #223995 Reply

    ladams
    Member

    Thanks for the tip went to apache site and tried to implement their steps but I get the following error:
    javax.servlet.ServletException: Unable to get connection, DataSource invalid: “org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class ‘com.mysql.jdbc.Driver’, cause: com.mysql.jdbc.Driver”

    I do have Connector/J in my lib folder.

    server.xml
    <Context path=”/dbTest” docBase=”dbTest”
    debug=”5″ reloadable=”true” crossContext=”true”>

    <Logger className=”org.apache.catalina.logger.FileLogger”
    prefix=”localhost_DBTest_log” suffix=”.txt” timestampe=”true”/>

    <Resource name=”jdbc/TestDB”
    auth=”Container”
    type=”javax.sql.DataSource”/>

    <ResourceParams name=”jdbc/TestDB”>
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!– Maximum number of db connections in pool. Make sure you configure your mysqld
    max_connections large enought to handle all of your db connections
    Set to 0 for no limit –>
    <parameter>
    <name>maxActive</name>
    <value>100</value>
    </parameter>

    <!– Maximum number of idle db connections to retain in pool.
    Set to -1 for no limit See also the DBCP documentation on this and
    the minEvictableIdleTimeMillis configuration parameter. –>
    <parameter>
    <name>maxIdle</name>
    <value>30</value>
    </parameter>

    <!– Maximum time to wait for a db connection to become available
    in ms. In this example 10 seconds. An exception is thrown if
    this timeout is exceeded. Set to -1 to wait indefinitely.–>
    <parameter>
    <name>maxWait</name>
    <value>10000</value>
    </parameter>

    <!– MySql db username and password for db connections –>
    <parameter>
    <name>username</name>
    <value>javauser</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value>javadude</value>
    </parameter>

    <!– Class name for MySQL Connector/J driver –>
    <parameter>
    <name>driverClassName</name>
    <value>com.mysql.jdbc.Driver</value>
    </parameter>

    <!– The JDBC connection url for connecting to your MySQL db.
    The autoReconnect=true argument to the url makes sure that the
    mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
    connection. mysqld by default closes idle connections after 8 hours –>
    <parameter>
    <name>url</name>
    <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
    </parameter>
    </ResourceParams>
    </Context>

    web.xml
    <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>

    test jsp

    <%@ page language=”java”%>

    <%@ taglib uri=”http://jakarta.apache.org/struts/tags-html&#8221; prefix=”html” %>
    <%@ taglib uri=”/WEB-INF/c.tld” prefix=”c” %>
    <%@ taglib uri=”/WEB-INF/sql.tld” prefix=”sql” %>

    <sql:query var=”rs” dataSource=”jdbc/TestDB”>
    select id, foo, bar from testdata
    </sql:query>

    <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
    <html:html locale=”true”>
    <head>
    <html:base />
    <title>Database Test</title>
    </head>

    <body>
    <h2>Results</h2>
    <c:forEach var=”row” items=”${rs.rows}”>
    Foo ${row.foo}<br/>
    Bar ${row.bar}<br/>
    </c:forEach>
    </body>
    </html:html>

    Any ideas on what I’ve missed. … Thanks.

    #223996 Reply

    Riyad Kalla
    Member

    Cannot load JDBC driver class ‘com.mysql.jdbc.Driver’, cause: com.mysql.jdbc.Driver”

    It needs to be in common/lib, not server/lib. Is it in common/lib?

    #224015 Reply

    ladams
    Member

    Yes it is but I also had it in WEB-INF/lib which I removed. It now looks like I’m getting an error while trying to create the connection pool. I have commons-pool-1.1.jar, commons-collections.jar, commons-dbcp-1.2.1.jar and commons-el.jar in the lib directory. Heres the error:
    javax.servlet.ServletException: Unable to get connection, DataSource invalid: “org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Server connection failure during transaction.
    Attempted reconnect 3 times. Giving up.)”

    #224016 Reply

    Riyad Kalla
    Member

    Even if you have them in your project, the app server needs them on startup, they are most likely in your server/lib dir and need to be copied into your common/lib dir.

    #274941 Reply

    amir55
    Participant

    hi all

    I try to get a replacement to the JBoss that killed me time to sort out errors in getting jsf integratted to spring and hibrernate. so I chosed Tomcat 6 that comes enabled in MyEclipse 6 is it a best substitution ?BUT no way to find how to configure to mysql. the site on the net talks aobut version 5.5 not 6 but still I followed the example with no luck.

    thanks

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Problem Defining MySql datasource

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