Firstly, create a method to get a connection from the pool
private static Connection getJNDIConnection(){ String DATASOURCE_CONTEXT = "java:comp/env/jdbc/airtime"; Connection result = null; try { Context initialContext = new InitialContext(); if ( initialContext == null){ errorMsg = "ERROR: JNDI problem. Cannot get InitialContext."; return null; } DataSource datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT); if (datasource != null) { result = datasource.getConnection(); } else { errorMsg = "ERROR: Failed to lookup datasource."; return null; } } catch ( Exception ex ) { errorMsg = "ERROR: Cannot get connection: " + ex; return null; } return result; } |
Secondly, in your “sun-web.xml” and “web.xml” deployment descriptors, add a reference to the resource
sun-web.xml :
<resource-ref> <res-ref-name>jdbc/airtime</res-ref-name> <jndi-name>jdbc/airtime</jndi-name> </resource-ref> |
web.xml :
<resource-ref> <res-ref-name>jdbc/airtime</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> |

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 
Doesn’t appear to work. When the …lookup(DATASOURCE_CONTEXT) line executes, it throws this error:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
heres the method i created to get a connection