-
What support does Websphere3.5 provide for connection pooling while using JSP's.
-
WebSphere 3.5 allows you to configure a DataSource object that will handle the pooling for you. The Admin console provides a wizard that will walk you through the process.
After you set it up, then you use JNDI to "lookup" the DataSource object, and ask it for a connection. It pools the connections(however many you specified) for you. When you close the Connection...it doesnt actually close, its just returned to the pool.
Cheers
-
Thanks.
I have made up a datasource name OraTest through Admin Console in Websphere and written the code given below. The problem is I am not able to compile it since its not finding the javax.sql.* packageg. If you can pls help:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
public class ConPool extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Properties prop = new Properties();
prop.put(DataSourceFactory.NAME, "OraTest");
prop.put(DataSourceFactory.DATASOURCE_CLASS_NAME, "oracle\jdbc\pool\OraclePooledConnection");
prop.put(DataSourceFactory.DESCRIPTION, " Connection Pooling using Oracle thin driver");
prop.put("pslndb","sample");
DataSource ds=DataSourceFactory.getDataSource(prop);
DataSourceFactory.bindDataSource(ds);
}
catch(Exception e){
e.getMessage();
}
try{
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.ejs.ns.jndi.CNInitialContextFactory");
javax.naming.Context ctx = new javax.naming.InitialContext(env);
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/OraTest");
conn=ds.getConnection("Smsadm","Sms$adm");
stmt=conn.createStatement();
rs=stmt.executeQuery("Select * from tb_users");
while(rs.next()){
out.println(rs.getString(1));
}
}
catch(Exception e){
out.println(e.getMessage());
}
}
}
-
When you have made a ConnectionPooled DataSource through WebSphere console itself then you do not have to write a redundant code of putting the DataSource again you just need the following :
try {
Properties env = new Properties();
env.put (javax.naming.Context.PROVIDER_URL, "iiop:///");
env.put (javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
javax.naming.Context jndiCtx = new javax.naming.InitialContext(env);
String dataSourceName = "jdbc/OraTest";
DataSource dataSource = (DataSource)jndiCtx.lookup(dataSourceName);
} catch (javax.naming.NamingException e) {
}
-
Make sure DB2JAVA.zip is in your classpath and use this one...
com.ibm.db2.jdbc.app.stdext.javax.sql.DataSource
-
Oops..forgot your using Oracle.
There should be a similar solution for the Oracle pool.
-
You probably just need to dig around and find the .jar file that contains the javax.sql.DataSource. Make sure its included in your classpath.
-
Thanks buddy!!! I got the thing working and really appreciate your help.
Balbir Singh
-
How to use Connection Pooling in an java application. I have succesfully used in Servlets/jsp, but having problems in using from a application. How does an application use the JNDI Naming Context.
Any help would be appreciated.
Thanks.
-
Same exact way. Just make sure you have the JNDI classes in your classpath and your good to go.
-
Now it is working fine for me. ThankYou Weston, you are right, the problem was JNDI not being in the classpath. Also, I was thinking how to use the same remotely. I mean a java application with a Websphere Test Environment trying to use the remote system (LAN) connection pooling JNDI.
-
Hi Balber singh,
how ru.Actual i'am new to websphere ,i know how to configure the datasource in webphere,but i don't know how to access that datasource.....can u plz send ur sample code that worked for u