Configuring a JNDI resource SQL in Jetty

安稳与你 提交于 2021-02-18 13:35:31

问题


I am having a lot of difficulty configuring a JNDI Resource in Jetty. I've gotten this to work in a Tomcat easily using context.xml and the Resource node. Attempts have been made to specify the resource in Jetty in both the jetty-env.xml and their version of a context.xml file but I end up getting the same exception. Here is the context.xml version of the resource definition:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/SQLDB</Arg>
        <Arg>
            <New class="com.microsoft.sqlserver.jdbc.SQLServerDriver">
                <Set name="serverName">SQLDB.domain.com</Set>
                <Set name="portNumber">1433</Set>
                <Set name="databaseName">DBName</Set>
                <Set name="userName">UName</Set>
                <Set name="password">PWord</Set>
            </New>
        </Arg>
    </New>
</Configure>

The exception being given when I try to start JNDI is:

java.lang.NoSuchMethodException: class com.microsoft.sqlserver.jdbc.SQLServerDriver.setServerName(class java.lang.String)
2013-02-01 16:57:39.061:WARN:oejd.DeploymentManager:Unable to reach node goal: started
java.lang.NoSuchMethodException: class com.microsoft.sqlserver.jdbc.SQLServerDriver.setServerName(class java.lang.String)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.set(XmlConfiguration.java:585)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:390)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:819)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1132)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:1035)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:783)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:398)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:349)
        at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:302)
        at org.eclipse.jetty.deploy.providers.ContextProvider.createContextHandler(ContextProvider.java:86)
        at org.eclipse.jetty.deploy.App.getContextHandler(App.java:100)
        at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:36)
        at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
        at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:494)
        at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:141)
        at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:145)
        at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:56)
        at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:609)
        at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:540)
        at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403)
        at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:337)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
        at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:121)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
        at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:555)
        at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:230)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
        at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:81)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58)
        at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:96)
        at org.eclipse.jetty.server.Server.doStart(Server.java:277)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
        at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1265)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1188)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jetty.start.Main.invokeMain(Main.java:468)
        at org.eclipse.jetty.start.Main.start(Main.java:616)
        at org.eclipse.jetty.start.Main.main(Main.java:92)

This exception seems counter intuitive because I can see in the Microsoft JDBC Driver 4.0 for SQL Server documentation that serverName is a listed as a valid property.

Also, I've run the below command to verify that all expected jar files seem to be included. Specifically those for jndi and sqljdbc.

java -jar start.jar --list-options

Below are links to tutorials and documentation I have used to get where I am.

http://wiki.eclipse.org/Jetty/Feature/JNDI

http://www.eclipse.org/jetty/documentation/current/jndi-datasource-examples.html

http://www.eclipse.org/jetty/documentation/current/jndi.html#configuring-env-entries

Any insight the community has would be greatly appreciated!


回答1:


You must follow these instructions for Jetty7. For older Jetty.

I see that you are trying to use Sql Server, so, you need to use net.sourceforge.jtds.jdbcx.JtdsDataSource as shown in the sample:

<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="DatabaseName">dbname</Set>
           <Set name="ServerName">localhost</Set>
           <Set name="PortNumber">1433</Set>
        </New>
     </Arg>
    </New>

Also, add to [jetty_home]/lib/ext the required library jTDS or using Maven: Maven dependency:




回答2:


Problem is not with serverName method. Method is not there. Neither portNumber, etc. on. You cannot use the driver directly.

If you take a look to the jetty doc, it suggests an alternative for Non-pooling DataSources. But problably you want a pool. So, this config.

<New class="org.apache.commons.dbcp.BasicDataSource">
    <Set name="driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
    <Set name="url">jdbc:jtds:sqlserver://<host>:<port>/<database_name></Set>
    <Set name="username">jdbc.user</Set>
    <Set name="password">jdbc.pass</Set>
</New>

Edited

Regarding your new problem. Not sure what it happening. It looks like jetty is facing problems creating the temporary directory. According with the doc, for Maven plugin the the temporary directory is ${basedir}/target. Review your jetty maven plugin configuration checking if temporal director is right and if jetty has permissions for that directory.



来源:https://stackoverflow.com/questions/14656442/configuring-a-jndi-resource-sql-in-jetty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!