getting new d/b connection each time (urgent)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mahesh_d
Posted On:   Wednesday, June 26, 2002 10:24 AM

JSP Page getting new connection each time insted of getting connection from pool Hi, I am using oracle connection pool ...I am getting new connection each time when i run jsp page ...... It is not getting the connection from pool ............. When i try to print connection object it is giving different object each time ....................... What is the wrong in my code .............. Is any one using oracle connection pool ................ Please help me ... Here is my code: <%@page contentType="text/html"%> <%@ page language="java" import="java.sql.*,ora   More>>
JSP Page

getting new connection each time insted of getting connection from pool


Hi,



I am using oracle connection pool ...I am getting new connection each time when i run jsp page ......


It is not getting the connection from pool .............


When i try to print connection object it is giving different object each time .......................


What is the wrong in my code ..............


Is any one using oracle connection pool ................


Please help me ...


Here is my code:




<%@page contentType="text/html"%>


<%@ page language="java" import="java.sql.*,oracle.jdbc.pool.*"%>






<%


Connection con= null;


DbConnectionPool dbcp=null;


dbcp=(DbConnectionPool)application.getAttribute("dbcp");


try{


if(dbcp == null)


{
dbcp=new DbConnectionPool();


application.setAttribute("dbcp",dbcp);


}
else


{
dbcp=(DbConnectionPool)dbcp;


}



con=dbcp.getDatabaseConnection();


Statement stmt=con.createStatement();


ResultSet rs=stmt.executeQuery("select * from checkout_checkin");


while(rs.next())


{
out.println(rs.getString(1)+"
");


}


stmt.close();


rs.close();


dbcp.closeDatabaseConnection(con);


}
catch (Exception e){System.out.println("eeeeeee==="+e);}

%>




*********************************


package oracle.jdbc.pool;



import oracle.jdbc.*;


import java.sql.*;



public class DbConnectionPool


{
OracleConnectionCacheImpl myConnectionPool;


OracleConnectionPoolDataSource myDataSource;




public DbConnectionPool()


{
try{


//Create & configure datasource


myDataSource = new OracleConnectionPoolDataSource();


myDataSource.setDriverType("thin");


myDataSource.setNetworkProtocol("tcp");


myDataSource.setServerName ("ipaddr");


myDataSource.setDatabaseName("xxxx");


myDataSource.setPortNumber (1521);


myDataSource.setUser ("xxxx");


myDataSource.setPassword ("xxxx");


//Create & configure pool


myConnectionPool = new OracleConnectionCacheImpl(myDataSource);


myConnectionPool.setMaxLimit(20);


myConnectionPool.setMinLimit(2);


myConnectionPool.setCacheScheme(OracleConnectionCacheImpl.DYNAMIC_SCHEME);


}catch (Throwable ex)
{
System.out.println("initConnectionPool() - Exception catched:"+ex);


}
}
public Connection getDatabaseConnection()


{
try{


System.out.println("oracleconnectionfrompool===="+myConnectionPool.getConnection());


return (Connection)myConnectionPool.getConnection();


}
catch (Throwable ex){ System.out.println("getDatabaseConnection()=="+ex); return null; }


}


public void closeDatabaseConnection(Connection connection)
{


try{


connection.close();
}catch (Throwable ex){


System.out.println("closeDatabaseConnection(Connection) - Exception catched: " +ex);


}


}


}




Thanks.

   <<Less

Re: getting new d/b connection each time (urgent)

Posted By:   Bif_Macmullen  
Posted On:   Thursday, June 27, 2002 03:05 PM

The simple way to make sure that you create a connection pool as a singleton is to write a servlet that instantiates it at startup and places it in the application context. Override the init() method of the servlet, create the pool and set it into the application context. Configure your deployment descriptor to load this servlet first on startup. In your JSP page, you then simply have to call something like:


ConnectionPool pool = (ConnectionPool) application.getAttribute("connPool");

Connection conn = pool.getConnection();



whenever you need to retrieve a connection from it.
About | Sitemap | Contact