2009-07-05 46 views
7

我正在学习如何为JBossAS 5.1.0构建java webapps,并且试图在JBossAS5上使用JNDI数据源构建非常基本的jsp Web应用程序来访问数据。无法使用JBoss上的JNDI数据源获取数据库连接

当试图打开我得到这个异常的连接:

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) 

数据源被部署好了,我可以看到它在越来越创建的数据库文件ok了JMX的控制台&。

问题

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> 

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

回答

0

想通了:

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

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

HsqlDbRealm被配置用于不同的DS &是造成连接失败。

0

看着你的代码,看起来你正确地得到了DataSource - 否则它将是空的。因此,当您尝试获取连接时会发生问题。

望着​​,似乎您的网址需要一个“file”部分:

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

而且,作为一个一般的编码评论,(1)使用一个标准的日志记录包,而不是自产自销“ (2)在记录一个异常时,使用logger和log4j(Commons Logging,可能是其他的)支持的异常作为参数(这样你可以得到完整的堆栈跟踪)。

+0

文件 - 奇,无我见过使用示例那可能是jdbc DriverManager的连接风格需要,改变它会导致更多的异常 – Dana 2009-07-06 04:15:54

1

它也可以在-ds.xml使用<应用程序管理的安全性/>而不是<安全域>,在租赁中Jboss6