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