[英]can't get DB Connection using JNDI datasource on JBoss
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. 我正在研究如何为JBossAS 5.1.0构建java webapps,我正在尝试使用JNDI数据源在JBossAS5上构建一个非常基本的jsp web应用程序来进行数据访问。
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. 数据源部署好了,我可以在jmx-console中看到它并且数据库文件被创建好了。
Java code in question where the exception is thrown: 有问题的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.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.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 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. 这是我第一次在这种环境中,我怀疑我错过了一些非常基本的东西。
也可以在-d.xml中使用<application-managed-security />而不是<security-domain>,至少在Jboss6中使用
Looking at your code, it appears that you get the DataSource correctly -- otherwise it would be null. 查看代码,看起来您正确获取了DataSource - 否则它将为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: 查看HSQLDB文档 ,您的URL似乎需要一个“文件”组件:
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). 并且,作为一般编码注释,(1)使用标准日志包,而不是本地“日志”方法,以及(2)在记录异常时,使用记录器调用(Log4J和Commons Logging都支持,可能是其他)将异常作为参数(以便获得完整的堆栈跟踪)。
Figured it out: 弄清楚了:
The culprit was this in hedgehogdb-ds.xml : 罪魁祸首是hedgehogdb-ds.xml:
<security-domain>HsqlDbRealm</security-domain>
HsqlDbRealm was configured for a different DS & was causing the connection to fail. HsqlDbRealm配置为不同的DS并导致连接失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.