0

I have copied tomcat-dbcp.jar from tomcat lib dir to jdk's jre\lib\ext dir and in context.xml file of my application is coded like below:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Conn_Pool">

<Resource name="jdbc/murach" auth="Container"
  maxActive="100" maxIdle="30" maxWait="10000"
  username="root" password="1234"
  driverClassName=" com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/murach"
  logAbandoned="true" removeAbandoned="true"
  removeAbandonedTimeout="60" type="javax.sql.DataSource" 
/>

</Context>

and a class ConnectionPool.java like below:

package data;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;


public class ConnectionPool 
{
private static ConnectionPool pool=null;
private static DataSource dataSource=null;

private ConnectionPool()
{
    try
    {
        InitialContext ic = new InitialContext();
        dataSource = (DataSource)ic.lookup("java:/comp/env/jdbc/murach");
    }
    catch(Exception e)
     {
         e.printStackTrace();
     }
}


public static ConnectionPool getInstance()
{
    if(pool==null)
    {
        pool= new ConnectionPool();
    }

    return pool;
}

public Connection getConnection()
{
    try
    {
        return dataSource.getConnection();
    }

    catch(SQLException sqle)
            {
                sqle.printStackTrace();
                return null;
            }
}

public void freeConnection(Connection c)
{
    try{
        c.close();
    }
    catch(SQLException sqle){
        sqle.printStackTrace();
    }
}
}

but public Connection getConnection() isn't working.
Its not returning dataSource.getConnection(); inside try block . instead its going inside the catch block

Shaon Hasan
  • 730
  • 7
  • 17
  • 1
    do you get any error / exception ? does these settings work with plane jdbc ? do you have a `jdbc-jar` for `MySQL` inside `CATALINA_HOME/lib` ? did you define a `resource-ref` in `web.xml` ? – A4L Apr 17 '13 at 18:36
  • 1
    As a side note: actually it was not necessary to copy tomcat-dbcp.jar from *TOMCAT_HOME/lib* to jdk's *jre\lib\ext*. You are running your app in Tomcat, so the container has access to its libs anyways... – informatik01 Apr 17 '13 at 19:05
  • As I can see the code is from the book ["Murach's Java Servlets and JSP (2nd Edition)"](http://www.amazon.com/Murachs-Java-Servlets-JSP-Edition/dp/1890774448/ref=sr_1_1?ie=UTF8&qid=1366226000&sr=8-1&keywords=murach+servlets). So the code itself is fine. What you are saying is that the `getConnection()` method throws an `SQLException` exception. Something's wrong with the configurations. Like not finding MySQL JDBC driver, or the placement of *context.xml* etc – informatik01 Apr 17 '13 at 19:13
  • Also I see **the closing `` tag is missing** in *context.xml* file (maybe it was just copy/paste issue, but just in case). Plus the make sure to put your *context.xml* in the */META-INF* folder – informatik01 Apr 17 '13 at 19:22
  • actually i am having problem with jdbc connection. here is the link for my problem: http://stackoverflow.com/questions/16078978/connection-with-mysql-with-netbeans-for-jsp – Shaon Hasan Apr 18 '13 at 09:23

0 Answers0