Link to home
Start Free TrialLog in
Avatar of rospcc
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("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;
            }
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of rospcc
rospcc

ASKER

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)
Avatar of rospcc

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rospcc

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-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>
Avatar of rospcc

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.
Avatar of rospcc

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.