Link to home
Start Free TrialLog in
Avatar of eexcel
eexcel

asked on

JNDI DataSource unable to work !!

Hi guys,


/usr/local/tomcat5/webapps/Advanced/WEB-INF/web.xml
===========================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>mySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/BooksDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
===========================================================

/usr/local/tomcat5/conf/server.xml
===========================================================
.............

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
suffix=".txt" timestamp="true"/>
<ResourceParams name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.3.57:3306/books</value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
===========================================================

/usr/local/tomcat5/webapps/Advanced/connect.jsp
===========================================================
<html>
<head>
<title>Connection Test</title>
</head>
<body>

<%
com.wrox.library.Connect con = new com.wrox.library.Connect();
con.init();
%>

<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
===========================================================

/usr/local/tomcat5/webapps/Advanced/WEB-INF/classes/com/wrox/library/Connect.java
===========================================================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Connect {

String stat = "Not Connected";

public void init() {
try {
stat = "1";
Context ctx = new InitialContext();
stat = "2";
if(ctx == null )
throw new Exception("Oops - No Context");
stat = "3";
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";
if (ds != null) {
Connection conn = ds.getConnection();
stat = "5";
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}

public String getstat() {
return stat;
}
}
===========================================================

When I tried to display connect.jsp, what I got display on IE6 is as follow:
=================================================
Connection Results
4
==================================================

Which means that the Connect.class unable to get the DataSource, for which the DataSource is NULL. Pls help !!
Avatar of eexcel
eexcel

ASKER

pls help guys .....thanks !!
Avatar of eexcel

ASKER

Hi applekanna,

    So what's wrong with my codes above?? Why can't it get connected ?
Why
ds == NULL at the code below ?

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";


Pls help !!
Are u using any IDE like JBUilder or IntelliJ etc...
Avatar of eexcel

ASKER

Hi Apple,

    I m using mysql-4.0.14b-win.zip installed under Windows 2K Server and mysql-connector-java-3.0.8-stable-bin.jar (placed under /usr/local/tomcat5/common/lib) and I also put
1) commons-dbcp.jar
2) commons-pool.jar
3) commons-collections.jar

to /usr/local/tomcat5/common/lib

I have also modified server.xml file as follow:

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
       suffix=".txt" timestamp="true"/>
<Resource name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/BooksDB">
<parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<parameter>
                <name>maxActive</name>
      <value>100</value>
</parameter>
<parameter>
      <name>maxIdle</name>
      <value>30</value>
</parameter>
<parameter>
      <name>maxWait</name>
      <value>10000</value>
</parameter>
<parameter>
                <name>removeAbandoned</name>
                <value>true</value>
</parameter>
<parameter>
               <name>removeAbandonedTimeout</name>
               <value>180</value>
</parameter>
<parameter>
                        <name>logAbandoned</name>
                        <value>true</value>
</parameter>
<parameter>
      <name>username</name>
      <value>booksuser</value>
</parameter>
<parameter>
      <name>password</name>
      <value>bookspass</value>
</parameter>
<parameter>
      <name>driverClassName</name>
                <value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
      <name>url</name>
      <value>jdbc:mysql://192.1.3.139:3306/books?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

and I still can't get it to connect. I still stucked at
ds == NULL

Pls..................help...................
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is your sql server running now. I will try to connect
eexcel.

I used your code om my machine and it works fine. These are the differences in my system.
I use com.mysql.jdbc.Driver.
I use my db.

Other than that no changes to your code except db changes.

I suspect the following

1. you might have not granted access to your database use "bookuser"

2. Extract from MySQL website
> "Versions of MySQL and the mm.mysql JDBC driver when have been reported to work:
>
>MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha
>mm.mysql 2.0.14 (JDBC Driver)
>Please let us know if you have tested the new MySQL mm.mysql 3.0 driver."

Tyr using the older veriosn of the driver.

3. go back to
A step by step tutorial
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example

and do that on ur machine. It will definately work

Does this help !
Cheers!


oops i forgot
check if you have a databse called "books" in your MySQL databse
I also tried connecting to your database

I get the following error
java.sqlException. BorrowFailed. Is there a MySQLserver running on the machine.

Check if you have started the mysql server

to start mysql server

C:\mysql\bin\mysqld
   
will start the server.

Cheers!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eexcel

ASKER

Yeah, it works now !!
I have changed

<parameter>
     <name>username</name>
     <value>root</value>
</parameter>
<parameter>
     <name>password</name>
     <value>13467982</value>
</parameter>

The question is I m very sure that there is booksuser/bookspass in mysql db. I suppose that as long as there is a user "booksuser" in mysql table (user), I can connect in. Why is that I used booksuser/bookspass and can't even connect ? Thanks for u guys help !! Thanks a lot !!
>The question is I m very sure that there is booksuser/bookspass in mysql db.

is it is a user for the database or a user in the database?
if you choose a comment for an answer. This question would move to the solved section helping all

:)
Cheers!
always check you tomcat log, those kind of error, like user/password is not correct, should show up in you tomcat log.