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("Using JNDI");
                  InitialContext context = new InitialContext();
                  //Look up our data source
                  DataSource ds = (DataSource) context.lookup("jdbc/connpool");
                  //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;
            }
rospccAsked:
Who is Participating?
 
CEHJCommented:
What happens when you print the exception stack trace in the first catch block?
0
 
objectsCommented:
have u set up properties correctly to find the initial context?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
objectsCommented:
primarily the factory and provider url, plus anything you your particular factory needs
0
 
rospccAuthor Commented:
It just printed this:
[#|2007-10-11T17:56:11.250+0800|SEVERE|sun-appserver-pe8.2|javax.enterprise.system.container.web|_ThreadID=12;|StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
      at org.apache.jsp.testConn_jsp._jspService(testConn_jsp.java:103)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
(removed the continuation)
0
 
rospccAuthor Commented:
I setup my JNDI in Sun App server in Admin console. I managed to ping successfully, which shows that the properties are correct.
0
 
objectsCommented:
can u post your config that maps your jndi resources
0
 
CEHJConnect With a Mentor Commented:
Make sure your DataSource is not null
0
 
rospccAuthor Commented:
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-method="auto-commit" datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" max-pool-size="32" max-wait-time-in-millis="60000" name="__TimerPool" pool-resize-quantity="2" res-type="javax.sql.XADataSource" steady-pool-size="8">
      <property name="databaseName" value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"/>
    </jdbc-connection-pool>
    <jdbc-connection-pool connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="false" max-pool-size="32" max-wait-time-in-millis="60000" name="DerbyPool" pool-resize-quantity="2" res-type="javax.sql.DataSource" 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-method="auto-commit" datasource-classname="oracle.jdbc.pool.OracleConnectionPoolDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="false" max-pool-size="32" max-wait-time-in-millis="60000" name="connpool" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" steady-pool-size="8">
      <property name="url" value="jdbc:oracle:thin:@(description=(address=(host=ServerSTG)(protocol=tcp)(port=1521))(connect_data=(SERVER=DEDICATED)(service_name=DBSTG1)))"/>
      <property name="user" value="USERNAME01"/>
      <property name="password" value="password01"/>
    </jdbc-connection-pool>
0
 
rospccAuthor Commented:
I managed to close the connection now. Decided not to use the pure JNDI without combining with our connection pooling method.
0
 
objectsCommented:
unless you specifically need it, jndi tends to just complicate things in our experience.
0
 
rospccAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.