java - javax.naming.NameNotFoundException in Jetty but not in Tomcat. How to solve? -
i have sample code this:
connectionpool.datasource = (datasource) initialcontext.lookup("java:comp/env/jdbc/murach");
and 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 text ---/> </context>
when deploy web app tomcat, db connection fine, when try jetty using jetty plugin in with: jetty:run-war
<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>
i getting:
javax.naming.namenotfoundexception; remaining name 'jdbc/murach'
how can make work jetty well?
i tried adding
<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>
to web.xml, getting:
java.lang.illegalstateexception: nothing bind name javax.sql.datasource/default
jetty should configured use jndi , deploy jndi resource it.
try add maven plugin configuration:
<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>
the file jetty-env.xml
looks (only example; used bonecp datasource):
<?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
should contain lines:
<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>
Comments
Post a Comment