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

Dan*_*ana 7 java jboss jndi jboss5.x

我正在研究如何为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)
Run Code Online (Sandbox Code Playgroud)

数据源部署好了,我可以在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;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*ana 0

弄清楚了:

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

<security-domain>HsqlDbRealm</security-domain>
Run Code Online (Sandbox Code Playgroud)

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