• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

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;
            }
0
rospcc
Asked:
rospcc
  • 5
  • 5
  • 2
2 Solutions
 
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
 
objectsCommented:
primarily the factory and provider url, plus anything you your particular factory needs
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
CEHJCommented:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now