Under META-INF/context.xml

<Resource name=”jdbc/myoracle” auth=”Container”
type=”javax.sql.DataSource” driverClassName=”oracle.jdbc.OracleDriver”
url=”jdbc:oracle:thin:@//localhost:1521/ORAXE”
username=”scott” password=”tiger” maxActive=”20″ maxIdle=”10″
maxWait=”5″
validationQuery=”select 1 from dual”
/>

*validationQuery: Check connection health before return it to caller. If this option is not enabled you might found that your application is waiting for corrupted connection.

Under web.xml

<resource-ref>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Java Code – Get connection from datasource

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup(“java:/comp/env”);
DataSource ds = (DataSource) envContext.lookup(“jdbc/myoracle”);
Connection conn = ds.getConnection();