# Datasource JNDI -Fehlermeldung



## ak (9. Mrz 2005)

Hallo Leute,

ich habe nach folgender Anleitung ein Oracle Connectionpooling über JNDI versucht aufzubauen:
http://www.microdeveloper.com/html/JNDI_Orcl_Tomcat.html#Configuration

Ich kenne auch die folgende Anleitung, damit klappts aber auch nicht:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/printer/jndi-datasource-examples-howto.html

Ich verwende den Tomcat 5.0 auf localhost, JDK 1.4.2 und Win XP und Oracle 10g.

Der relevante Teil der Server.xml sieht folgendermaßen aus (wie im erstgenannten Link beschrieben):

```
<GlobalNamingResources>
  ... 
      <Resource name="jdbc/db1"
	   auth="Container"
	   type="oracle.jdbc.pool.OracleDataSource"
	   driverClassName="oracle.jdbc.driver.OracleDriver"
	   factory="oracle.jdbc.pool.OracleDataSourceFactory"
	   url="jdbc:oracle:thin:@ak:1521:kurse"
	   user="scott"
	   password="tiger"
	   maxActive="20"
	   maxIdle="10"
	   maxWait="-1" />
  </GlobalNamingResources>
```

Der zugefügte Teil meiner web.xml sieht so aus:

```
<resource-ref>
   <description>Oracle Development Datasource</description>
   <res-ref-name>jdbc/db1</res-ref-name>
   <res-type>oracle.jdbc.pool.OracleDataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
```
Dann habe ich noch in der Datei C:\Programme\Apache\Tomcat_5.0\conf\META-INF\context.xml folgenden Inhalt:

```
<Context>
	 
	 <WatchedResource>WEB-INF/web.xml</WatchedResource>
	 <WatchedResource>META-INF/context.xml</WatchedResource>
		
	 <ResourceLink global="jdbc/db1" name="jdbc/db1"   
          type="oracle.jdbc.pool.OracleDataSource"/>
 </Context>
```
Diese Datei ist außerdem noch in folgendem Ordner:
C:\Programme\Apache\Tomcat_5.0\conf\

Aufgerufen wird die Datasource folgendermaßen:

```
public boolean openDatasource(){
    try {
      Context initContext = new InitialContext();
      Context envContext = (Context) initContext.lookup("java:/comp/env");
      OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/db1");

      Connection conn = ds.getConnection("scott","tiger");
   
    } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch(SQLException e1){
      e1.printStackTrace();
    }
    
    return true;
  }
```

Ich bekomme aber folgende Fehlermeldung:


```
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)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:791)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:138)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:779)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:151)
	at first_Struts.DB_Handling.openDatasource(DB_Handling.java:111)
	at first_Struts.DB_LookAction.insert(DB_LookAction.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:324)
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
	at org.apache.struts.actions.LookupDispatchAction.execute(LookupDispatchAction.java:162)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
	at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
	at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
	at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
	at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
	at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:534)
```

Den Oracle-JDBC-Treiber habe ich wie beschrieben nur in folgendem Verzeichnis:
C:\Programme\Apache\Tomcat_5.0\common\lib
Die url zur DB sollte stimmen, da ich mit dem Drivermanager mich mit dieser url einwandfrei connecten kann.

Ich habe jetzt schon eine Menge ausprobiert aber es will einfach nicht funktionieren. Habt ihr evtl. eine Ahnung woran das liegen kann?

Gruß


----------



## Guest (10. Mrz 2005)

hier mal der ganz Log, vielleicht kann ja jemand etwas damit anfangen:


```
10.03.2005 18:51:18 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-80
10.03.2005 18:51:19 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 5247 ms
10.03.2005 18:51:19 org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SCHWERWIEGEND: Exception processing Global JNDI Resources
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)
   at org.apache.naming.NamingContext.lookup(NamingContext.java:791)
   at org.apache.naming.NamingContext.lookup(NamingContext.java:151)
   at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:155)
   at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:160)
   at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:125)
   at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:97)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:2306)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:287)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:425)
10.03.2005 18:51:19 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
10.03.2005 18:51:19 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.0.28
10.03.2005 18:51:19 org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
10.03.2005 18:51:19 org.apache.catalina.core.StandardHost getDeployer
INFO: Create Host deployer for direct deployment ( non-jmx )
10.03.2005 18:51:19 org.apache.catalina.core.StandardHostDeployer install
INFO: Processing Context configuration file URL file:C:\Programme\Apache\Tomcat_5.0\conf\Catalina\localhost\admin.xml
10.03.2005 18:51:25 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
10.03.2005 18:51:25 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
10.03.2005 18:51:26 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.webapp.admin.ApplicationResources', returnNull=true
10.03.2005 18:51:30 org.apache.catalina.core.StandardHostDeployer install
INFO: Processing Context configuration file URL file:C:\Programme\Apache\Tomcat_5.0\conf\Catalina\localhost\balancer.xml
10.03.2005 18:51:30 org.apache.catalina.core.StandardHostDeployer install
INFO: Processing Context configuration file URL file:C:\Programme\Apache\Tomcat_5.0\conf\Catalina\localhost\manager.xml
10.03.2005 18:51:30 org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /myweb from URL file:C:/Programme/Apache/Tomcat_5.0/webapps/myweb
10.03.2005 18:51:31 org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /struts-blank from URL file:C:/Programme/Apache/Tomcat_5.0/webapps/struts-blank
10.03.2005 18:51:33 org.apache.struts.tiles.TilesPlugin initDefinitionsFactory
INFO: Tiles definition factory loaded for module ''.
10.03.2005 18:51:33 org.apache.struts.validator.ValidatorPlugIn initResources
INFO: Loading validation rules file from '/WEB-INF/validator-rules.xml'
10.03.2005 18:51:33 org.apache.struts.validator.ValidatorPlugIn initResources
INFO: Loading validation rules file from '/WEB-INF/validation.xml'
10.03.2005 18:51:34 org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /struts-documentation from URL file:C:/Programme/Apache/Tomcat_5.0/webapps/struts-documentation
10.03.2005 18:51:35 org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /struts-examples from URL file:C:/Programme/Apache/Tomcat_5.0/webapps/struts-examples
```


----------



## ak (15. Mrz 2005)

Sooo nachdem ich mir nochmal im Web einige Varianten angeschaut und ausprobiert habe, hier nun meine Lösung:

Ich entferne aus der server.xml alles was zur datasource gehört, also die ganzen Params im <GlobalNamingResources>-Element.
Ich brauche im Prinzip nur einträge in zwei Dateien. Zuerst erstelle ich im Ordner 
"C:\Programme\Apache\Tomcat_5.0\conf\Catalina\localhost" 
eine XML-Datei welche genauso heisst wie die webapp, bei mir ist es die "strutsweb1.xml". In diese kommt folgender Inhalt:

```
<Context path="/strutsweb1" docBase="C:/Programme/Apache/Tomcat_5.0/webapps/strutsweb1"  debug="0" >

<Resource name="jdbc/myoracle" auth="Container"
              type="javax.sql.DataSource"/>

	<ResourceParams name="jdbc/myoracle">
	  <parameter>
		<name>factory</name>
		<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
	  </parameter>
	  <parameter>
		<name>driverClassName</name>
		<value>oracle.jdbc.driver.OracleDriver</value>
	  </parameter>
	  <parameter>
		<name>url</name>
		<value>jdbc:oracle:thin:@ak:1521:kurse</value>
	  </parameter>
	  <parameter>
		<name>username</name>
		<value>scott</value>
	  </parameter>
	  <parameter>
		<name>password</name>
		<value>tiger</value>
	  </parameter>
	  <parameter>
		<name>maxActive</name>
		<value>20</value>
	  </parameter>
	  <parameter>
		<name>maxIdle</name>
		<value>10</value>
	  </parameter>
	  <parameter>
		<name>maxWait</name>
		<value>-1</value>
	  </parameter>
	</ResourceParams>

</Context>
```

Anschließend folgender Eintrag in die web.xml:


```
<resource-ref>
     <description>Oracle Datasource example</description>
     <res-ref-name>jdbc/myoracle</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
  </resource-ref>
```

Aufgerufen wird die Datasource folgendermaßen:


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

thats all  ;-)


----------

