First example on page http://commons.apache.org/dbcp/guide/jndi-howto.html is invalid, with this code every call of
InitialContext ic2 = new InitialContext();
DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
Connection conn = ds.getConnection();
ends with new datasource with unclosed connection in it becase new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null); reference creates new DataSource instead using created one while calling ic2.lookup("jdbc/basic").
At the end it ends with many opened connections to DB until JVM is ended.
Second example I didn't test, i use direct aproach with manualy creating SharedPoolDataSource and registring it in FSContext JNDI itself.