在Jetty中配置JNDI资源SQL

时间:2013-02-01 22:52:33

标签: jdbc jetty jndi

我在Jetty中配置JNDI资源有很多困难。我已经使用context.xml和Resource节点轻松地在Tomcat中工作了。已尝试在jetty-env.xml及其context.xml文件版本中指定Jetty中的资源,但最终得到相同的异常。以下是资源定义的context.xml版本:

<?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>

当我尝试启动JNDI时给出的异常是:

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)

这个异常似乎反直觉,因为我可以在Microsoft JDBC Driver 4.0 for SQL Server documentation中看到serverName被列为有效属性。

另外,我运行以下命令来验证是否包含所有预期的jar文件。特别是那些jndi和sqljdbc。

java -jar start.jar --list-options

以下是我过去常常使用的教程和文档的链接。

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

非常感谢社区的任何见解!

2 个答案:

答案 0 :(得分:1)

您必须按照Jetty7的说明进行操作。 对于年龄较大的Jetty

我看到您正在尝试使用Sql Server,因此,您需要使用 net.sourceforge.jtds.jdbcx.JtdsDataSource ,如示例所示:

<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>

此外,添加 [jetty_home] / lib / ext 所需的库jTDS或使用Maven:Maven dependency

答案 1 :(得分:0)

问题不在于serverName方法。方法不存在。没有portNumber等等。您不能直接使用驱动程序。

如果你看一下jetty doc,它就建议了非汇集数据源的替代方案。但可能你想要一个游泳池。所以,这是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>

<强>被修改

关于你的新问题。不确定它发生了什么。看起来码头正面临创建temporary directory的问题。根据{{​​3}},对于Maven插件,临时目录为${basedir}/target。检查您的jetty maven插件配置,检查时间控制器是否正确以及jetty是否具有该目录的权限。