Dear all,
I have an issue with creating a connection pool within a web application in order to be used by several servlets. I appreciate if you could kindly give me a hand.
I'm using:
Web server: Apache-tomcat: 6.0.18
Oracle Database 11g Enterprise: 11.1.0.6.0 - Production
Operating system: Linux (ubuntu 8.10)
IDE: Sun Netbeans
Oralce JDBC Drivers: 11.1.0.7.0-Production (ojdbc6.jar and orai18n.jar)
JDK 1.6
Usually, just for creating a connection to my database (without using a connection pool), I proceed in the following way:
String dbURL = "jdbc:oracle:thin:@localhost:1521:database01";
String username = "scott";
String user_password = "tiger";
String userSqlQuery = "SELECT * FROM mytable";
Connection connection = DriverManager.getConnection (dbURL, username, user_password);
Statement statement = connection.createStatement();
statement.executeUpdate(query_text);
This works pretty well allowing to communicate with my oracle database. By this method I have run several projects
with different queries (SELECT, INSERT, UPDATE, DELETE ,etc.) and each time it worked without any problem and
returned the result of the query. Therefore I think that there is no problem with JDBC drivers
However, when I want to create a connection pool, it seems that the lookup method cannot locate Oracle JDBC drivers. That is,
try
{
InitialContext ic = new InitialContext();
Context envContext = (Context)ic.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/oracle11gEnterprise");
Connection connection = dataSource.getConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
Just after calling dataSource.getConnection() the java.lang.NullPointerException is thrown.
Any idea?
Thanks in advance,
And for your information, here are context.xml and web.xml files for my project
Here is my context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/WebApplication1">
<Resource name="jdbc/oracle11gEnterprise"
scope="Shareable"
type="javax.sql.DataSource"
auth="Container"
description="Oracle Database 11g Enterprise"
maxActive="100"
maxIdle="30"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
url="jdbc:oracle:thin:@localhost:1521:database01"
username="scott"
password="tiger" />
</Context>
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<ConsumeOutput>false</ConsumeOutput>
<resource-ref>
<res-ref-name>jdbc/oracle11gEnterprise</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--
.
. And here as usual I write servlet definition and url mappings
.
-->