Cannot get DB connection using JNDI data source on JBoss

I am learning how to create java webapps for JBossAS 5.1.0, and I am trying to create a very simple jsp web application on JBossAS5 using a JNDI data source to access data.

When I try 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 data source is deployed normally, I see it in the jmx console, and the database files are created normally.

Java code that throws an exception:

 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 am missing something really elementary.

+7
java jboss jndi
source share
3 answers

It revealed:

The challenger was in hedgehogdb-ds.xml:

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

HsqlDbRealm was configured for different DS and caused a connection failure.

0
source share

it is also possible to use in-ds.xml <application-managed-security / "> instead of <security-domain>, for rent in Jboss6

+1
source share

Looking at your code, it seems that you are getting the DataSource correctly, otherwise it will be null. Therefore, the problem occurs when you try to get a connection.

Looking at the HSQLDB docs , it seems like your url needs a file component:

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

And, as a general comment on the encoding, (1) use the standard logging package rather than the home method "log" and (2) when registering the exception, use a logical call (supported by Log4J and Commons Logging, probably others), which accepts the exception in as a parameter (so that you get a full stack trace).

0
source share

All Articles