tags:

views:

4245

answers:

2

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.

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.

Java code in question where the exception is thrown:

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>

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

A: 

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.

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

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

kdgregory
file - odd, none of the examples I've seen use that, it might be required for the jdbc DriverManager style of connection, changing it caused more exceptions
evil tabby cat
Thanks for the Log4J reminder, my nasty test code :-(
evil tabby cat
A: 

Figured it out:

The culprit was this in hedgehogdb-ds.xml :

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

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

evil tabby cat