Java 无法在 JBoss 上使用 JNDI 数据源获取数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1083963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:23:21  来源:igfitidea点击:

can't get DB Connection using JNDI datasource on JBoss

javajbossjndijboss5.x

提问by Dana

I'm studying how to build java webapps for JBossAS 5.1.0 and I'm trying to build a very basic jsp web app on JBossAS5 using a JNDI datasource for data access.

我正在研究如何为 JBossAS 5.1.0 构建 java webapps,我正在尝试使用 JNDI 数据源在 JBossAS5 上构建一个非常基本的 jsp web 应用程序进行数据访问。

When trying to open a connection I get this exception:

尝试打开连接时,出现此异常:

21:42:52,834 ERROR [STDERR] Cannot get connection: org.jboss.util.NestedSQLException:
Unable to get managed connection for hedgehogDB; - nested throwable:
(javax.resource.ResourceException: Unable to get managed connection for hedgehogDB)

The datasource is deployed ok, I can see it in the jmx-console & the database files are getting created ok.

数据源部署正常,我可以在 jmx-console 中看到它,并且数据库文件正在创建正常。

Java code in question where the exception is thrown:

有问题的 Java 代码抛出异常:

static public Connection getHedgehogConnection()
{
    Connection result = null;
    try 
    {
        String DS_Context = "java:comp/env/jdbc/hedgehogDB";

        Context initialContext = new InitialContext();

        if ( initialContext == null)
            log("JNDI problem. Cannot get InitialContext.");

        DataSource datasource = (DataSource)initialContext.lookup(DS_Context);

        if (datasource != null)
            result = datasource.getConnection();
        else
            log("Failed: datasource was null");
    }
    catch(Exception ex)
    {
        log("Cannot get connection: " + ex);
    }

    return result;
}

web.xml:

网页.xml:

<web-app>
    <resource-ref>
    <res-ref-name>jdbc/hedgehogDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

jboss-web.xml:

jboss-web.xml:

<jboss-web>
    <resource-ref>
        <res-ref-name>jdbc/hedgehogDB</res-ref-name>
        <jndi-name>java:/hedgehogDB</jndi-name>
    </resource-ref>
</jboss-web>

hedgehogdb-ds.xml

刺猬数据库-ds.xml

<datasources>
   <local-tx-datasource>
      <jndi-name>hedgehogDB</jndi-name>
      <connection-url>jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}hedgehogDB</connection-url>
      <driver-class>org.hsqldb.jdbcDriver</driver-class>
      <user-name>sa</user-name>
      <password></password>
      <min-pool-size>5</min-pool-size>
      <max-pool-size>20</max-pool-size>
      <idle-timeout-minutes>0</idle-timeout-minutes>
      <track-statements/>
      <security-domain>HsqlDbRealm</security-domain>
      <prepared-statement-cache-size>32</prepared-statement-cache-size>
      <metadata>
         <type-mapping>Hypersonic SQL</type-mapping>
      </metadata>
      <depends>jboss:service=Hypersonic,database=hedgehogDB</depends>
   </local-tx-datasource>

   <mbean code="org.jboss.jdbc.HypersonicDatabase"
     name="jboss:service=Hypersonic,database=hedgehogDB">
     <attribute name="Database">hedgehogDB</attribute>
     <attribute name="InProcessMode">true</attribute>
   </mbean>

</datasources>

This is my first time in this environment and I suspect that I'm missing something really basic.

这是我第一次进入这种环境,我怀疑我错过了一些非常基本的东西。

采纳答案by Dana

Figured it out:

弄清楚了:

The culprit was this in hedgehogdb-ds.xml :

罪魁祸首是hedgehogdb-ds.xml 中的这个:

<security-domain>HsqlDbRealm</security-domain>

HsqlDbRealm was configured for a different DS & was causing the connection to fail.

HsqlDbRealm 被配置为不同的 DS & 导致连接失败。

回答by kdgregory

Looking at your code, it appears that you get the DataSource correctly -- otherwise it would be null. So the problem happens when you try to get the connection.

查看您的代码,您似乎正确获取了 DataSource —— 否则它将为空。因此,当您尝试获取连接时就会出现问题。

Looking at the HSQLDB docs, it seems that your URL needs a "file" component:

查看HSQLDB 文档,您的 URL 似乎需要一个“文件”组件:

jdbc:hsqldb:file:${jboss.server.data.dir}${/}hypersonic${/}hedgehogDB

And, as a general coding comment, (1) use a standard logging package, rather than a homegrown "log" method, and (2) when logging an exception, use the logger call (supported by both Log4J and Commons Logging, probably others) that takes an exception as a paramter (so that you get the full stack trace).

并且,作为一般编码注释,(1) 使用标准日志记录包,而不是自产的“日志”方法,以及 (2) 在记录异常时,使用记录器调用(由 Log4J 和 Commons Logging 支持,可能还有其他人支持) ) 将异常作为参数(以便您获得完整的堆栈跟踪)。

回答by zhimapi

it's also possible to in -ds.xml use < application-managed-security / > instead of < security-domain >, at lease in Jboss6

也可以在 -ds.xml 中使用 < application-managed-security /> 而不是 < security-domain >,至少在 Jboss6 中