You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jordi Domingo <jd...@sopragroup.es> on 2003/05/04 14:13:13 UTC

HELP MySQL through JNDI (PLZ HELP!)

Hi! I'm trying to configure a datasource with tomcat 4.0.3 to access  mysql but i always get a null DataSource. I ve been looking but i cant find the problem. I can access mysql putting the code directly in the jsp.
Heres all my code and configuration (i ve put mysql.jar under  %tomcat_home%\common\lib)

Server.xml

<Context path="/usa_bbdd" debug="1" reloadable="true" crossContext="true">
 <Logger className="org.apache.catalina.logger.FileLogger" prefix="jordi_mysql." suffix=".txt" timestamp="true"/>
 <Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource"/>
 <ResourceParams name="jdbc/MySQL">
 <parameter>
     <name>factory</name>
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
 </parameter>
 
 <parameter>
     <name>maxActive</name>
     <value>5</value>
 </parameter>
 <parameter>
     <name>maxIdle</name>
     <value>3</value>
 </parameter>
 <parameter>
     <name>maxWait</name>
     <value>10000</value>
 </parameter>
 <parameter>
     <name>username</name>
     <value>noseya</value>
 </parameter>
 <parameter>
     <name>password</name>
     <value>mypass</value>
 </parameter>
 <parameter>
     <name>driverClassName</name>
     <value>org.gjt.mm.mysql.Driver</value>
 </parameter>
 <parameter>
     <name>url</name>
     <value>jdbc:mysql://localhost:3306/jordi?autoReconnect=true</value>
 </parameter>
 </ResourceParams>
</Context>

My web.xml

<web-app>
    <display-name>Aplicacion de demostracion</display-name>
    <description>
        demo
    </description>
 <resource-ref>
   <description>
  Per poder accedir a una factoria de instancies java.sql.Connection per comunicar amb una bbdd  configurada al fitxer server.xml.
   </description>
   <res-ref-name>
     jdbc/MySQL
   </res-ref-name>
   <res-type>
     javax.sql.DataSource
   </res-type>
   <res-auth>
     Container
   </res-auth>
 </resource-ref>
</web-app>

And my jsp..

<%@ page session="true" import="java.sql.*,javax.naming.*,javax.sql.*" %>
<% Context initCtx = new InitialContext();
 Context envCtx = (Context) initCtx.lookup("java:comp/env");
 DataSource ds = (DataSource)envCtx.lookup("jdbc/MySQL");
 Connection conn=null;
 
 try{
  conn = ds.getConnection();
 }catch(NullPointerException e){}
 
  new org.gjt.mm.mysql.Driver();
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jordi", "noseya", "mypass");
%>
<html>
<body>
<% try{ %>
<%= conn.getMetaData().getDatabaseProductVersion() %>
<% }catch(NullPointerException e){ %>
 Cant get connection<br>
<% } %>
<%= con.getMetaData().getDatabaseProductVersion() %>
<% con.close(); %>
<% if(conn!=null)conn.close(); %>
</body>
</html>

The output results in

Cant get connection
3.23.32

Thx all

Re: HELP MySQL through JNDI (PLZ HELP!)

Posted by Tarun Ramakrishna Elankath <ta...@webkadai.com>.
Hi Jordi,

It looks like you have everything ok. Can't make out what's wrong. Were
there any errors while starting up the server ?  (maybe in web.xml) ?

Could you remove the ?autoReconnect=true and try again ? (Just a wild
guess)

-Tarun

On Sun, 2003-05-04 at 17:43, Jordi Domingo wrote:
> Hi! I'm trying to configure a datasource with tomcat 4.0.3 to access  mysql but i always get a null DataSource. I ve been looking but i cant find the problem. I can access mysql putting the code directly in the jsp.
> Heres all my code and configuration (i ve put mysql.jar under  %tomcat_home%\common\lib)
> 
> Server.xml
> 
> <Context path="/usa_bbdd" debug="1" reloadable="true" crossContext="true">
>  <Logger className="org.apache.catalina.logger.FileLogger" prefix="jordi_mysql." suffix=".txt" timestamp="true"/>
>  <Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource"/>
>  <ResourceParams name="jdbc/MySQL">
>  <parameter>
>      <name>factory</name>
>      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
>  </parameter>
>  
>  <parameter>
>      <name>maxActive</name>
>      <value>5</value>
>  </parameter>
>  <parameter>
>      <name>maxIdle</name>
>      <value>3</value>
>  </parameter>
>  <parameter>
>      <name>maxWait</name>
>      <value>10000</value>
>  </parameter>
>  <parameter>
>      <name>username</name>
>      <value>noseya</value>
>  </parameter>
>  <parameter>
>      <name>password</name>
>      <value>mypass</value>
>  </parameter>
>  <parameter>
>      <name>driverClassName</name>
>      <value>org.gjt.mm.mysql.Driver</value>
>  </parameter>
>  <parameter>
>      <name>url</name>
>      <value>jdbc:mysql://localhost:3306/jordi?autoReconnect=true</value>
>  </parameter>
>  </ResourceParams>
> </Context>
> 
> My web.xml
> 
> <web-app>
>     <display-name>Aplicacion de demostracion</display-name>
>     <description>
>         demo
>     </description>
>  <resource-ref>
>    <description>
>   Per poder accedir a una factoria de instancies java.sql.Connection per comunicar amb una bbdd  configurada al fitxer server.xml.
>    </description>
>    <res-ref-name>
>      jdbc/MySQL
>    </res-ref-name>
>    <res-type>
>      javax.sql.DataSource
>    </res-type>
>    <res-auth>
>      Container
>    </res-auth>
>  </resource-ref>
> </web-app>
> 
> And my jsp..
> 
> <%@ page session="true" import="java.sql.*,javax.naming.*,javax.sql.*" %>
> <% Context initCtx = new InitialContext();
>  Context envCtx = (Context) initCtx.lookup("java:comp/env");
>  DataSource ds = (DataSource)envCtx.lookup("jdbc/MySQL");
>  Connection conn=null;
>  
>  try{
>   conn = ds.getConnection();
>  }catch(NullPointerException e){}
>  
>   new org.gjt.mm.mysql.Driver();
>   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jordi", "noseya", "mypass");
> %>
> <html>
> <body>
> <% try{ %>
> <%= conn.getMetaData().getDatabaseProductVersion() %>
> <% }catch(NullPointerException e){ %>
>  Cant get connection<br>
> <% } %>
> <%= con.getMetaData().getDatabaseProductVersion() %>
> <% con.close(); %>
> <% if(conn!=null)conn.close(); %>
> </body>
> </html>
> 
> The output results in
> 
> Cant get connection
> 3.23.32
> 
> Thx all


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org