Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to create Data Source in Tomcat

 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am unable to cofigure datasource in tomcat 6.0 server.After solving all exceptions,Nw iam unable to get Data Source object.
I have configured contect.xml file in C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf directory.

<Context path="/DiaryProj" crossContext="true" reloadable="true">
<Resource name="jdbc/diary" auth="Container"
type="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
description="User database that can be updated and saved"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
pathname="conf/tomcat-users.xml"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/diary"
username="root"
password="root"
/>
</Context>


In my diary project application web.xml file i've configured
<resource-ref>
<description>Connection Pool</description>
<res-ref-name>jdbc/diary</res-ref-name>
<res-type>org.apache.tomcat.dbcp.dbcp.BasicDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


This is my ConnectionManager class which contains my getConnection()



import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionManager {

private Connection con = null;
private DataSource ds = null;
private Context ctx = null;
private InitialContext ic = null;
private static ConnectionManager cm = null;
private ConnectionManager(){

}
public static ConnectionManager getInstance(){
if(cm==null)
cm = new ConnectionManager();
return cm;
}
public Connection getConnection(){
try {
ctx = new InitialContext();
// ctx.lookup("java:comp/env");

System.out.println("context object"+ctx);
try {
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/diary");
System.out.println("data source object--->"+ds);
if(ds!=null)
con = ds.getConnection();
System.out.println("Connection object"+con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}

}

Please Ignore If any spelling mistakes and formatting mistakes...
Could anyone please suggest me to get data source...

Thanks in Advance,,
 
Juanjo Cuadrado
Greenhorn
Posts: 18
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vipul,

I don't know if you be able to read in Spanish but here there is a post that explain the proccess to use JDBCRealm in a Tomcat Server... If you don't be able read in Spanish, you can try it with Google Translater... It will be always better than my translation


http://finger-in-the-eye.blogspot.com/2009/01/login-de-un-usuario-mediante-realm-en.html
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
I have read the content in the site.It is related to security.Mine problem is not concerned to that...
I am not able to get data source in tomcat 6...
Please give explanation clearly...
Thanks in Advance.
 
Juanjo Cuadrado
Greenhorn
Posts: 18
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... I did not put the correct link

http://finger-in-the-eye.blogspot.com/2007/09/configurar-el-pool-de-datos-de-tomcat.html
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks For your reply...
I read the content and configured the same in my application.
Still Iam getting data source object null value.

My previous configuration is similar to what i configured now..

Please any one help me to get data source object..
Iam using Tomcat 6.0 ,struts and mysql database..

context object javax.naming.InitialContext@1a70b8
data source object--->null
Connection object null

Thanks in Advance.
 
shivanand Pawar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you shoud keep your context.xml file in the META-INF folder of your web application.
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks For Your Reply.
i have tried that too.Kept context.xml in META-INF folder.
Still gettting null value for data source object...
Please guide me in getting data source object.
 
shivanand Pawar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing.
keep the context.xml in Meta-inf folder and change the
type="javax.sql.DataSource"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic