7

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

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

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)

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

引发异常的有问题的 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;
}

网页.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>
    <resource-ref>
        <res-ref-name>jdbc/hedgehogDB</res-ref-name>
        <jndi-name>java:/hedgehogDB</jndi-name>
    </resource-ref>
</jboss-web>

刺猬数据库-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>

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

4

3 回答 3

1

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

于 2011-04-15T12:39:58.690 回答
0

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

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

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

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

于 2009-07-05T13:04:03.310 回答
0

弄清楚了:

罪魁祸首是在hedgehogdb-ds.xml 中:

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

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

于 2009-07-06T04:17:11.563 回答