繁体   English   中英

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

[英]can't get DB Connection using JNDI datasource on JBoss

我正在研究如何为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;
}

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

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

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

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

查看代码,看起来您正确获取了DataSource - 否则它将为null。 因此,当您尝试获取连接时会发生此问题。

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

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

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

弄清楚了:

罪魁祸首是hedgehogdb-ds.xml:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM