rospcc
asked on
Connection pooling to get connection from JNDI
Hi,
I had a lot of issue with my application because I didn't manage to close the connection after calling JNDI. I combined our own connection pooling to get connection from JNDI. Is there anything wrong with the codes below? I can't seem to get connection. It always give me Null.pointer.exception. Please advise, thanks.
private Connection newConnection()
{
Connection con = null;
try
{
//System.out.println("Usin g JNDI");
InitialContext context = new InitialContext();
//Look up our data source
DataSource ds = (DataSource) context.lookup("jdbc/connp ool");
//Allocate and use a connection from the pool
con = ds.getConnection();
}
catch (SQLException e)
{
//log(e, "Can not create a new Connection for " + URL);
return null;
}
catch (Exception e)
{
e.printStackTrace();
}
return con;
}
I had a lot of issue with my application because I didn't manage to close the connection after calling JNDI. I combined our own connection pooling to get connection from JNDI. Is there anything wrong with the codes below? I can't seem to get connection. It always give me Null.pointer.exception. Please advise, thanks.
private Connection newConnection()
{
Connection con = null;
try
{
//System.out.println("Usin
InitialContext context = new InitialContext();
//Look up our data source
DataSource ds = (DataSource) context.lookup("jdbc/connp
//Allocate and use a connection from the pool
con = ds.getConnection();
}
catch (SQLException e)
{
//log(e, "Can not create a new Connection for " + URL);
return null;
}
catch (Exception e)
{
e.printStackTrace();
}
return con;
}
What happens when you print the exception stack trace in the first catch block?
have u set up properties correctly to find the initial context?
primarily the factory and provider url, plus anything you your particular factory needs
ASKER
It just printed this:
[#|2007-10-11T17:56:11.250 +0800|SEVE RE|sun-app server-pe8 .2|javax.e nterprise. system.con tainer.web |_ThreadID =12;|Stand ardWrapper Valve[jsp] : Servlet.service() for servlet jsp threw exception
java.lang.NullPointerExcep tion
at org.apache.jsp.testConn_js p._jspServ ice(testCo nn_jsp.jav a:103)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:105 )
(removed the continuation)
[#|2007-10-11T17:56:11.250
java.lang.NullPointerExcep
at org.apache.jsp.testConn_js
at org.apache.jasper.runtime.
(removed the continuation)
ASKER
I setup my JNDI in Sun App server in Admin console. I managed to ping successfully, which shows that the properties are correct.
can u post your config that maps your jndi resources
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I set up in Sun App Server. The domain.xml showing the properties of the JNDI connection:
<jdbc-resource enabled="true" jndi-name="jdbc/connpool" object-type="user" pool-name="connpool"/>
<jdbc-connection-pool connection-validation-meth od="auto-c ommit" datasource-classname="org. apache.der by.jdbc.Em beddedXADa taSource" fail-all-connections="fals e" idle-timeout-in-seconds="3 00" is-connection-validation-r equired="f alse" is-isolation-level-guarant eed="true" max-pool-size="32" max-wait-time-in-millis="6 0000" name="__TimerPool" pool-resize-quantity="2" res-type="javax.sql.XAData Source" steady-pool-size="8">
<property name="databaseName" value="${com.sun.aas.insta nceRoot}/l ib/databas es/ejbtime r"/>
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-meth od="auto-c ommit" datasource-classname="org. apache.der by.jdbc.Cl ientDataSo urce" fail-all-connections="fals e" idle-timeout-in-seconds="3 00" is-connection-validation-r equired="f alse" is-isolation-level-guarant eed="false " max-pool-size="32" max-wait-time-in-millis="6 0000" name="DerbyPool" pool-resize-quantity="2" res-type="javax.sql.DataSo urce" steady-pool-size="8">
<property name="PortNumber" value="1527"/>
<property name="Password" value="APP"/>
<property name="User" value="APP"/>
<property name="serverName" value="localhost"/>
<property name="DatabaseName" value="sun-appserv-samples "/>
<property name="connectionAttributes " value=";create=true"/>
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-meth od="auto-c ommit" datasource-classname="orac le.jdbc.po ol.OracleC onnectionP oolDataSou rce" fail-all-connections="fals e" idle-timeout-in-seconds="3 00" is-connection-validation-r equired="f alse" is-isolation-level-guarant eed="false " max-pool-size="32" max-wait-time-in-millis="6 0000" name="connpool" pool-resize-quantity="2" res-type="javax.sql.Connec tionPoolDa taSource" steady-pool-size="8">
<property name="url" value="jdbc:oracle:thin:@( descriptio n=(address =(host=Ser verSTG)(pr otocol=tcp )(port=152 1))(connec t_data=(SE RVER=DEDIC ATED)(serv ice_name=D BSTG1)))"/ >
<property name="user" value="USERNAME01"/>
<property name="password" value="password01"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="jdbc/connpool" object-type="user" pool-name="connpool"/>
<jdbc-connection-pool connection-validation-meth
<property name="databaseName" value="${com.sun.aas.insta
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-meth
<property name="PortNumber" value="1527"/>
<property name="Password" value="APP"/>
<property name="User" value="APP"/>
<property name="serverName" value="localhost"/>
<property name="DatabaseName" value="sun-appserv-samples
<property name="connectionAttributes
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-meth
<property name="url" value="jdbc:oracle:thin:@(
<property name="user" value="USERNAME01"/>
<property name="password" value="password01"/>
</jdbc-connection-pool>
ASKER
I managed to close the connection now. Decided not to use the pure JNDI without combining with our connection pooling method.
unless you specifically need it, jndi tends to just complicate things in our experience.
ASKER
sorry, mistyped in my previous thread. Decided to use pure JNDI. Not combining with our own connection pooling method. Ya, it was the requirement of our client to use JNDI.