Hi !
While writing a custom JNDI Resource factory, Tomcat 5 runs into an
exception i can't explain:
javax.naming.NamingException: Cannot create resource instance
at
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:132)
at
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
this exception will be thrown by the following code lines:
private void initDBConnectionPool(){
try {
Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
thePool = (DBConnectionPool)
envCtx.lookup("webarts/DBConnectionPool");
} catch (NamingException e) {
e.printStackTrace();
}
}
To configure my resource factory i've added the following lines into to
_server.xml_:
<Resource name="webarts/DBConnectionPool"
auth="Container" type="de.webarts.DBConnectionPool.DBConnectionPool"/>
<ResourceParams name="webarts/DBConnectionPool">
<parameter><name>factory</name><value>de.webarts.DBConnectionPool.DBConnectionPoolSingleton</value></parameter>
<parameter><name>driverClassName</name><value>oracle.jdbc.driver.OracleDriver</value></parameter>
<parameter><name>driverName</name><value>jdbc:oracle:thin:@################</value></parameter>
<parameter><name>user</name><value>######</value></parameter>
<parameter><name>password</name><value>######</value></parameter>
<parameter><name>poolSize</name><value>10</value></parameter>
<parameter><name>maxSize</name><value>30</value></parameter>
<parameter><name>initSize</name><value>5</value></parameter>
<parameter><name>expiryTime</name><value>18000</value></parameter>
<parameter><name>poolName</name><value>WebArts_DBConnectionPool</value></parameter>
</ResourceParams>
For declaring the custom resource factory i have added the following
lines into the _web.xml_
<resource-ref>
<description>DBConnectionPool Factory</description>
<res-ref-name>webarts/DBConnectionPool</res-ref-name>
<res-type>de.webarts.DBConnectionPool.DBConnectionPool</res-type>
<res-auth>Container</res-auth>
</resource-ref>
My factory class looks like that:
package de.webarts.DBConnectionPool;
import javax.naming.*;
import java.util.Enumeration;
import java.util.Hashtable;
import de.webarts.DBConnectionPool.*;
public final class DBConnectionPoolSingleton implements ObjectFactory {
private static DBConnectionPool theStaticPool = null;
private DBConnectionPoolSingleton(){
}
public static DBConnectionPool createDBConnectionPool(String name,
int poolSize, int maxSize, int initSize, int expiryTime, String url,
String passwd, String user){
if (theStaticPool != null) {
theStaticPool = new DBConnectionPool(name, poolSize,
maxSize, initSize, expiryTime, url, passwd, user);
}
return theStaticPool;
}
public Object getObjectInstance(Object obj, Name name, Context
nameCtx, Hashtable environment) throws Exception {
Reference ref = (Reference) obj;
Enumeration enumRef = ref.getAll();
String poolName = "WebArts DBConnectionPool";
// "poolName"
String driverClassName = null; //
"driverClass"
String driverName = null;
// "driverName"
String user = null;
// "user"
String password = null;
// "password"
int poolSize = 0; // "poolSize"
int maxSize = 0; // "maxSize"
int initSize = 0; // "intSize"
int expiryTime = 18000; // "expiryTime"
// extract the values for the parameters specified in server.xml
// and create a database connection pool as singleton.
while (enumRef.hasMoreElements()) {
RefAddr refAddr = (RefAddr) enumRef.nextElement();
String type = refAddr.getType();
String value = (String)refAddr.getContent();
if (type.equals("poolName")) {
poolName = value;
} else
if (type.equals("driverClassName")) {
driverClassName = value;
} else
if (type.equals("driverName")) {
driverName = value;
} else
if (type.equals("user")) {
user = value;
} else
if (type.equals("password")) {
password = value;
} else
if (type.equals("poolSize")) {
poolSize =
Integer.getInteger(value).intValue();
} else
if (type.equals("maxSize")) {
maxSize =
Integer.getInteger(value).intValue();
} else
if (type.equals("initSize")) {
initSize =
Integer.getInteger(value).intValue();
} else
if (type.equals("expiryTime")) {
expiryTime =
Integer.getInteger(value).intValue();
}
} // end while
return createDBConnectionPool(poolName, poolSize, maxSize,
initSize, expiryTime, driverName, password, user);
}
Anybody has a clue of what I might be doing wrong?
Every hint or idea is very welcome.
Best regards,
Claus