2015-07-18 9 views
12

Ho un codice di esempio in questo modo:javax.naming.NameNotFoundException nel molo ma non in Tomcat. Come risolvere?

ConnectionPool.dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/murach"); 

e in webapp/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Context> 
<Resource name="jdbc/murach" 
      auth="Container" 
      type="javax.sql.DataSource" 
      username="root" 
      --- Rest of the text ---/> 
</Context> 

Quando schiero questa applicazione Web per Tomcat, connessione DB va bene , tuttavia quando provo con molo utilizzando Jetty Plugin con: pontile: run-guerra

<plugin> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>9.2.1.v20140609</version> 
    <configuration> 
     <scanIntervalSeconds>2</scanIntervalSeconds> 
     <httpConnector> 
      <port>8082</port> 
     </httpConnector> 
     <webApp> 
      <contextPath>/</contextPath> 
     </webApp> 
    </configuration> 
</plugin> 

sto ottenendo:

javax.naming.NameNotFoundException; remaining name 'jdbc/murach' 

Come posso fare questo lavoro con molo come bene?

Ho anche provato ad aggiungere

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

a web.xml, ma ora sto ottenendo:

java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default 
+0

Questa origine dati è definita per il server Jetty? [https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource](https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource) –

+0

@Koray Tugay Vedi la risposta di m.hassaballah sotto. Stai definendo la tua origine dati nel tuo file web.xml come una risorsa, non solo in Context.xml. –

+0

Voglio solo sapere ...La mia risposta è stata utile o ci sono dei problemi nell'usare l'approccio suggerito? – svaor

risposta

7

Il jetty deve essere configurato per utilizzare JNDI e distribuire la risorsa JNDI al suo interno.

tenta di aggiungere a Maven di configurazione del plugin:

<configuration> 
    <!-- ... --> 
    <jettyEnvXml>src/main/dev-path-to-jetty-env-xml/jetty-env.xml</jettyEnvXml> 
    <webXml>src/main/dev-path-to-web-xml/web.xml</webXml> 
</configuration> 

Il file jetty-env.xml assomiglia (solo come esempio, utilizzato bonecp origine dati):

<?xml version="1.0"?> 
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
    <New id="murachDataSource" class="org.eclipse.jetty.plus.jndi.Resource"> 
     <Arg>jdbc/murach</Arg> 

     <Arg> 
      <New class="com.jolbox.bonecp.BoneCPDataSource"> 
       <Set name="driverClass">org.h2.Driver</Set> 
       <Set name="jdbcUrl">jdbc:h2:~/murach/test</Set> 
       <Set name="username">sa</Set> 
       <Set name="password"/> 

       <Set name="partitionCount">4</Set> 
       <Set name="minConnectionsPerPartition">5</Set> 
       <Set name="acquireIncrement">5</Set> 
       <Set name="closeConnectionWatch">false</Set> 
      </New> 
     </Arg> 
    </New> 
</Configure> 

File web.xml dovrebbe contenere le linee:

<resource-ref> 
    <description>My DataSource Reference</description> 
    <res-ref-name>jdbc/murach</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 
3

avete aggiunto resource-ref a web.xml?

+0

Grazie per la risposta, ma sto ottenendo: java.lang.IllegalStateException: Niente da collegare per nome javax.sql.DataSource/default –

4

volevo aggiungere questo come commento, non è permesso a me ancora.
svaor La risposta è quella corretta, perché non è stato definito un luogo standard per inserire le risorse JNDI nella specifica J2EE.
Quindi, il file context.xml è specifico per tomcat e il molo non ne sa nulla.

http://docs.jboss.org/jbossweb/3.0.x/jndi-resources-howto.html

Lo standard J2EE fornisce un insieme standard di elementi nel file /WEB-INF/web.xml per fare riferimento a risorse; risorse a cui si fa riferimento in questi elementi devono essere definiti in una configurazione specifica per server delle applicazioni.

Problemi correlati