Hi!,
I have a tomcat web server
I have been trying the dbcp to implement but finding some issues
related on how to retrieve a connection from the pool after making the
pre-requisite set up for dbcp
such as setting up the respective files for the jndi datasource
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
context.XML-->
The first context tag was there by default,
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
This one i added->
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/java?
autoReconnect=true"/>
</Context>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
web.XML of my web application-->
Added this tag after all the servlet-mapping tags in the file
<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>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I have AllocateResources.java i have said load-on-startup 1 for this
which tries to retrieve the connection obj & put it int ServletContext
AllocateResources.java-->
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.sql.DataSource;
public class AllocateResources extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet
{
Connection connection=null;
static final long serialVersionUID = 1L;
public void init() throws ServletException
{
try
{
Context initContext = new InitialContext();
Context envContext =
(Context)initContext.lookup("java:/comp/
env");
DataSource ds =
(DataSource)envContext.lookup("jdbc/TestDB");
synchronized (ds)
{
try
{
connection = ds.getConnection();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
ServletContext context=getServletContext();
context.setAttribute("dbConnection",connection);
System.out.println("db connection established &
connection object
available in the servlet context");
}
catch (NamingException ex)
{
throw new ServletException("Cannot retrieve
java:comp/env/jdbc/
conversion",ex);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And haviing Try.java where i am trying to get the pooled obj to do bd
activity
Try.java-->
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Try extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet
{
static final long serialVersionUID = 1L;
Connection con;
public void init() throws ServletException
{
ServletContext context=getServletContext();
con=(Connection)context.getAttribute("dbConnection");
}
protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException
{
try
{
Statement stmt=con.createStatement();
ResultSet result = stmt.executeQuery("Select *
from testdata");
while(result.next())
{
String res=result.getString(2);
System.out.println(res);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
but after retrieving the datasource and try to get a connection object
I get Exceptions as follows-->
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
driver of class '' for connect URL 'null'
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:
1150)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:
880)
at AllocateResources.init(AllocateResources.java:25)
at javax.servlet.GenericServlet.init(GenericServlet.java:254)
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:
1161)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:
981)
at
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:
4045)
at
org.apache.catalina.core.StandardContext.start(StandardContext.java:
4351)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:
1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:
1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:
443)
at
org.apache.catalina.core.StandardService.start(StandardService.java:
516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:
710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source)
at java.sql.DriverManager.getDriver(Unknown Source)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:
1143)
... 20 more
db connection established & connection object available in the servlet
context
Sep 27, 2008 10:37:27 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-9090
Sep 27, 2008 10:37:27 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Sep 27, 2008 10:37:27 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/32 config=null
Sep 27, 2008 10:37:27 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 452 ms
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
unable to find the solution to the problem
Hoping for some guidance.
Thanks & Regards,
komal
On Sep 15, 11:08 pm, walden <[EMAIL PROTECTED]> wrote:
> Consider instead using Connection Pooling with Tomcat or JBoss or
> whatever EE container you happen to have.
>
> On Sep 15, 3:03 am, Komal Goyal <[EMAIL PROTECTED]> wrote:
>
> > i want to set up the db connection first before the application is
> > viewed by the user. to do this i need a servlet to do this job but the
> > problem is to integrate this servlet into a gwt project which was
> > created.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---