首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jetty uberjar jndi数据源配置

Jetty uberjar jndi数据源配置
EN

Stack Overflow用户
提问于 2013-01-24 11:55:41
回答 1查看 1.9K关注 0票数 0

更具体地说,我得到了包含所有依赖项的可执行war。但是,应用程序在没有jndi数据源的情况下启动,就像一个魅力,用jndi数据源运行它失败了。我认为在这样的配置中没有jetty.xml的位置,所以jettyenv.xml也不能运行,因为jetty默认不会读取它。我试图使用jettyweb.xml进行jndi数据源配置,但是jetty未能部署应用程序,返回503错误代码。我用的是Jetty9-M4。尝试使用tomcat池和BoneCP,结果是相同的。

入门班:

代码语言:javascript
运行
复制
import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.webapp.WebAppContext;


public final class Loader {

    public static void main(String[] args) throws Exception
    {
         String jetty_home = "..";
         int appli_port = 8080;
         Server server = new Server(appli_port);
         ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
         URL location = protectionDomain.getCodeSource().getLocation();
         WebAppContext webapp = new WebAppContext();
         webapp.setContextPath("/");
         webapp.setWar(location.toExternalForm());    
         server.setHandler(webapp);

         server.start();
         server.join();
    }
}

jetty-web.xml:

代码语言:javascript
运行
复制
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="testDS" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/testDS</Arg>
        <Arg>
            <New class="com.jolbox.bonecp.BoneCPDataSource">
                <Set name="driverClass">org.postgresql.Driver</Set>
                <Set name="jdbcUrl">jdbc:postgresql://localhost:5432/testDS</Set>
                <Set name="username">name</Set>
                <Set name="password">password</Set>
                <Set name="minConnectionsPerPartition">5</Set>
                <Set name="maxConnectionsPerPartition">50</Set>
                <Set name="acquireIncrement">5</Set>
                <Set name="idleConnectionTestPeriod">30</Set>
            </New>
        </Arg>
    </New>
</Configure>

我怀疑jndi在jndi发布到jndi(Jndi)到jndi的阶段或配置都是错误的。

抱歉英语不太好。

忘记提到我正在使用Hibernate作为ORM。

web.xml包含:

代码语言:javascript
运行
复制
<resource-ref>
    <description>Postgres datasource</description>
    <res-ref-name>jdbc/testDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Hibernate.cfg.xml包含

代码语言:javascript
运行
复制
<property name="hibernate.connection.datasource">java:comp/env/jdbc/testDS</property>

已尝试

代码语言:javascript
运行
复制
<property name="hibernate.connection.datasource">jdbc/testDS</property>

也不起作用。

请注意,放置在war的jetty-web.xml中的JNDI资源配置是可以的,默认情况下使用它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-24 12:27:22

jetty-env.xml文件应该自动从WEB/jetty-env.xml中获取,参见这里的http://www.eclipse.org/jetty/documentation/current/jetty-env-xml.html。我建议使用jetty-env.xml而不是jetty-web.xml

您还可以从应用程序中显示如何执行JNDI查找吗?也许JNDI数据源被正确初始化了,但是JNDI名称不正确?

这里有一些JNDI示例:http://www.eclipse.org/jetty/documentation/current/jndi-datasource-examples.html

更新:似乎Jetty在运行嵌入式时不会自动拾取它。试试这个:

代码语言:javascript
运行
复制
public static void main(String[] args) throws Exception
{
    String jetty_home = "..";
    int appli_port = 8080;
    Server server = new Server(appli_port);
    ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar(location.toExternalForm());

    // setup JNDI
    EnvConfiguration envConfiguration = new EnvConfiguration();
    URL url = new File("path to jetty-env.xml").toURI().toURL();
    envConfiguration.setJettyEnvXml(url);
    webapp.setConfigurations(new Configuration[] {new WebInfConfiguration(), envConfiguration, new WebXmlConfiguration()});

    server.setHandler(webapp); 
    server.start();
    server.join();
}

(见http://blog.armhold.com/2011/12/27/how-to-get-jndi-working-with-wicket-1-5-and-jetty-7-5/)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14500809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档