Solved

JNDI DataSource unable to work !!

Posted on 2003-10-22
17
461 Views
Last Modified: 2010-04-01
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 !!
0
Comment
Question by:eexcel
  • 9
  • 4
  • 2
17 Comments
 

Author Comment

by:eexcel
ID: 9599166
pls help guys .....thanks !!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9599601
0
 

Author Comment

by:eexcel
ID: 9603502
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 !!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:applekanna
ID: 9603531
Are u using any IDE like JBUilder or IntelliJ etc...
0
 

Author Comment

by:eexcel
ID: 9604177
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...................
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 63 total points
ID: 9604442
can you check your tomcat log and see if you get any error when startup?
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604484
is your sql server running now. I will try to connect
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604512
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!


0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604517
oops i forgot
check if you have a databse called "books" in your MySQL databse
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604531
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!
0
 
LVL 3

Assisted Solution

by:applekanna
applekanna earned 62 total points
ID: 9604540
Becoz of the error stated above this is what I get on my JSP
I get

Connection Result
4

which I guess is what you get :)
0
 

Author Comment

by:eexcel
ID: 9604715
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 !!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604724
>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?
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9605212
if you choose a comment for an answer. This question would move to the solved section helping all

:)
Cheers!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9607352
always check you tomcat log, those kind of error, like user/password is not correct, should show up in you tomcat log.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
Read our guide on how to survive being on-call.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question